diff --git a/packages/schemas/alterations/next-1716291265-create-pre-configured-m-api-role.ts b/packages/schemas/alterations/next-1716291265-create-pre-configured-m-api-role.ts index a47e7ffd2..0db971318 100644 --- a/packages/schemas/alterations/next-1716291265-create-pre-configured-m-api-role.ts +++ b/packages/schemas/alterations/next-1716291265-create-pre-configured-m-api-role.ts @@ -1,9 +1,10 @@ -import { generateStandardId } from '@logto/shared/universal'; import { yes } from '@silverhand/essentials'; import { sql } from '@silverhand/slonik'; import type { AlterationScript } from '../lib/types/alteration.js'; +import { generateStandardId } from './utils/1716643968-id-generation.js'; + const isCi = yes(process.env.CI); const defaultTenantId = 'default'; diff --git a/packages/schemas/alterations/utils/1716643968-id-generation.ts b/packages/schemas/alterations/utils/1716643968-id-generation.ts new file mode 100644 index 000000000..1728ad3a0 --- /dev/null +++ b/packages/schemas/alterations/utils/1716643968-id-generation.ts @@ -0,0 +1,46 @@ +/** + * This file is forked from `@logto/shared` 3.1.0 to avoid alteration scripts to depend on outer packages. + */ +import { customAlphabet } from 'nanoid'; + +const lowercaseAlphabet = '0123456789abcdefghijklmnopqrstuvwxyz'; +const alphabet = `${lowercaseAlphabet}ABCDEFGHIJKLMNOPQRSTUVWXYZ` as const; + +type BuildIdGenerator = { + /** + * Build a nanoid generator function uses numbers (0-9), lowercase letters (a-z), and uppercase letters (A-Z) as the alphabet. + * @param size The default id length for the generator. + */ + (size: number): ReturnType; + /** + * Build a nanoid generator function uses numbers (0-9) and lowercase letters (a-z) as the alphabet. + * @param size The default id length for the generator. + */ + // eslint-disable-next-line @typescript-eslint/unified-signatures + (size: number, includingUppercase: false): ReturnType; +}; + +const buildIdGenerator: BuildIdGenerator = (size: number, includingUppercase = true) => + customAlphabet(includingUppercase ? alphabet : lowercaseAlphabet, size); + +/** + * Generate a standard id with 21 characters, including lowercase letters and numbers. + * + * @see {@link lowercaseAlphabet} + */ +export const generateStandardId = buildIdGenerator(21, false); + +/** + * Generate a standard short id with 12 characters, including lowercase letters and numbers. + * + * @see {@link lowercaseAlphabet} + */ +export const generateStandardShortId = buildIdGenerator(12, false); + +/** + * Generate a standard secret with 32 characters, including uppercase letters, lowercase + * letters, and numbers. + * + * @see {@link alphabet} + */ +export const generateStandardSecret = buildIdGenerator(32); diff --git a/packages/schemas/package.json b/packages/schemas/package.json index bd2a8a1fa..7f45d18ec 100644 --- a/packages/schemas/package.json +++ b/packages/schemas/package.json @@ -84,7 +84,8 @@ "@logto/phrases": "workspace:^1.10.1", "@logto/phrases-experience": "workspace:^1.6.1", "@logto/shared": "workspace:^3.1.1", - "@withtyped/server": "^0.13.6" + "@withtyped/server": "^0.13.6", + "nanoid": "^5.0.1" }, "peerDependencies": { "zod": "^3.22.4" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2328e08bf..d7cecbeb9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3862,6 +3862,9 @@ importers: '@withtyped/server': specifier: ^0.13.6 version: 0.13.6(zod@3.22.4) + nanoid: + specifier: ^5.0.1 + version: 5.0.7 zod: specifier: ^3.22.4 version: 3.22.4 @@ -10361,11 +10364,6 @@ packages: mute-stream@0.0.8: resolution: {integrity: sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==} - nanoid@3.3.6: - resolution: {integrity: sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==} - engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} - hasBin: true - nanoid@3.3.7: resolution: {integrity: sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} @@ -21450,8 +21448,6 @@ snapshots: mute-stream@0.0.8: {} - nanoid@3.3.6: {} - nanoid@3.3.7: {} nanoid@4.0.2: {} @@ -22114,7 +22110,7 @@ snapshots: postcss@8.4.31: dependencies: - nanoid: 3.3.6 + nanoid: 3.3.7 picocolors: 1.0.0 source-map-js: 1.0.2