Skip to content

Commit

Permalink
fix: add id generation utils for alteration scripts (#5921)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun committed May 26, 2024
1 parent e200cfe commit 07fdd85
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
46 changes: 46 additions & 0 deletions packages/schemas/alterations/utils/1716643968-id-generation.ts
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 4 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07fdd85

Please sign in to comment.