mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix: add id generation utils for alteration scripts (#5921)
This commit is contained in:
parent
e200cfe681
commit
07fdd8557c
4 changed files with 54 additions and 10 deletions
|
@ -1,9 +1,10 @@
|
||||||
import { generateStandardId } from '@logto/shared/universal';
|
|
||||||
import { yes } from '@silverhand/essentials';
|
import { yes } from '@silverhand/essentials';
|
||||||
import { sql } from '@silverhand/slonik';
|
import { sql } from '@silverhand/slonik';
|
||||||
|
|
||||||
import type { AlterationScript } from '../lib/types/alteration.js';
|
import type { AlterationScript } from '../lib/types/alteration.js';
|
||||||
|
|
||||||
|
import { generateStandardId } from './utils/1716643968-id-generation.js';
|
||||||
|
|
||||||
const isCi = yes(process.env.CI);
|
const isCi = yes(process.env.CI);
|
||||||
|
|
||||||
const defaultTenantId = 'default';
|
const defaultTenantId = 'default';
|
||||||
|
|
|
@ -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<typeof customAlphabet>;
|
||||||
|
/**
|
||||||
|
* 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<typeof customAlphabet>;
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
|
@ -84,7 +84,8 @@
|
||||||
"@logto/phrases": "workspace:^1.10.1",
|
"@logto/phrases": "workspace:^1.10.1",
|
||||||
"@logto/phrases-experience": "workspace:^1.6.1",
|
"@logto/phrases-experience": "workspace:^1.6.1",
|
||||||
"@logto/shared": "workspace:^3.1.1",
|
"@logto/shared": "workspace:^3.1.1",
|
||||||
"@withtyped/server": "^0.13.6"
|
"@withtyped/server": "^0.13.6",
|
||||||
|
"nanoid": "^5.0.1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
|
|
|
@ -3862,6 +3862,9 @@ importers:
|
||||||
'@withtyped/server':
|
'@withtyped/server':
|
||||||
specifier: ^0.13.6
|
specifier: ^0.13.6
|
||||||
version: 0.13.6(zod@3.22.4)
|
version: 0.13.6(zod@3.22.4)
|
||||||
|
nanoid:
|
||||||
|
specifier: ^5.0.1
|
||||||
|
version: 5.0.7
|
||||||
zod:
|
zod:
|
||||||
specifier: ^3.22.4
|
specifier: ^3.22.4
|
||||||
version: 3.22.4
|
version: 3.22.4
|
||||||
|
@ -10361,11 +10364,6 @@ packages:
|
||||||
mute-stream@0.0.8:
|
mute-stream@0.0.8:
|
||||||
resolution: {integrity: sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==}
|
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:
|
nanoid@3.3.7:
|
||||||
resolution: {integrity: sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==}
|
resolution: {integrity: sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==}
|
||||||
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
|
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
|
||||||
|
@ -21450,8 +21448,6 @@ snapshots:
|
||||||
|
|
||||||
mute-stream@0.0.8: {}
|
mute-stream@0.0.8: {}
|
||||||
|
|
||||||
nanoid@3.3.6: {}
|
|
||||||
|
|
||||||
nanoid@3.3.7: {}
|
nanoid@3.3.7: {}
|
||||||
|
|
||||||
nanoid@4.0.2: {}
|
nanoid@4.0.2: {}
|
||||||
|
@ -22114,7 +22110,7 @@ snapshots:
|
||||||
|
|
||||||
postcss@8.4.31:
|
postcss@8.4.31:
|
||||||
dependencies:
|
dependencies:
|
||||||
nanoid: 3.3.6
|
nanoid: 3.3.7
|
||||||
picocolors: 1.0.0
|
picocolors: 1.0.0
|
||||||
source-map-js: 1.0.2
|
source-map-js: 1.0.2
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue