-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add id generation utils for alteration scripts (#5921)
- Loading branch information
Showing
4 changed files
with
54 additions
and
10 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
packages/schemas/alterations/next-1716291265-create-pre-configured-m-api-role.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/schemas/alterations/utils/1716643968-id-generation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.