-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(console): extract type definition gen process
extract the type definition gen step to the build time. As typescript is not available at run time.
- Loading branch information
Showing
7 changed files
with
101 additions
and
48 deletions.
There are no files selected for viewing
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
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,12 @@ | ||
#!/bin/sh | ||
|
||
# Clean up | ||
rm -rf scripts-js/ | ||
# build the jwt-customizer-type-definition generate script | ||
pnpm exec tsc -p tsconfig.scripts.gen.json | ||
# clean up the existing generated jwt-customizer-type-definition file | ||
rm -f src/consts/jwt-customizer-type-definition.ts | ||
# run script | ||
node scripts-js/generate-jwt-customizer-type-definition.js | ||
# Clean up | ||
rm -rf scripts-js/ |
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
60 changes: 60 additions & 0 deletions
60
packages/console/scripts/generate-jwt-customizer-type-definition.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,60 @@ | ||
import fs from 'node:fs'; | ||
|
||
import { | ||
jwtCustomizerUserContextGuard, | ||
accessTokenPayloadGuard, | ||
clientCredentialsPayloadGuard, | ||
} from '@logto/schemas'; | ||
import { type ZodTypeAny } from 'zod'; | ||
import { zodToTs, printNode } from 'zod-to-ts'; | ||
|
||
const filePath = 'src/consts/jwt-customizer-type-definition.ts'; | ||
|
||
const typeIdentifiers = `export enum JwtCustomizerTypeDefinitionKey { | ||
JwtCustomizerUserContext = 'JwtCustomizerUserContext', | ||
AccessTokenPayload = 'AccessTokenPayload', | ||
ClientCredentialsPayload = 'ClientCredentialsPayload', | ||
EnvironmentVariables = 'EnvironmentVariables', | ||
};`; | ||
|
||
const inferTsDefinitionFromZod = (zodSchema: ZodTypeAny, identifier: string): string => { | ||
const { node } = zodToTs(zodSchema, identifier); | ||
const typeDefinition = printNode(node); | ||
|
||
return `type ${identifier} = ${typeDefinition};`; | ||
}; | ||
|
||
// Create the jwt-customizer-type-definition.ts file | ||
const createJwtCustomizerTypeDefinitions = () => { | ||
const jwtCustomizerUserContextTypeDefinition = inferTsDefinitionFromZod( | ||
jwtCustomizerUserContextGuard, | ||
'JwtCustomizerUserContext' | ||
); | ||
|
||
const accessTokenPayloadTypeDefinition = inferTsDefinitionFromZod( | ||
accessTokenPayloadGuard, | ||
'AccessTokenPayload' | ||
); | ||
|
||
const clientCredentialsPayloadTypeDefinition = inferTsDefinitionFromZod( | ||
clientCredentialsPayloadGuard, | ||
'ClientCredentialsPayload' | ||
); | ||
|
||
const fileContent = `/* This file is auto-generated. Do not modify it manually. */ | ||
${typeIdentifiers} | ||
export const jwtCustomizerUserContextTypeDefinition = \` | ||
${jwtCustomizerUserContextTypeDefinition}\`; | ||
export const accessTokenPayloadTypeDefinition = \` | ||
${accessTokenPayloadTypeDefinition}\`; | ||
export const clientCredentialsPayloadTypeDefinition = | ||
\`${clientCredentialsPayloadTypeDefinition}\`; | ||
`; | ||
|
||
fs.writeFileSync(filePath, fileContent); | ||
}; | ||
|
||
createJwtCustomizerTypeDefinitions(); |
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
57 changes: 10 additions & 47 deletions
57
packages/console/src/pages/JwtClaims/utils/type-definitions.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
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,7 @@ | ||
{ | ||
"extends": "@silverhand/ts-config/tsconfig.base", | ||
"compilerOptions": { | ||
"outDir": "scripts-js" | ||
}, | ||
"include": ["scripts"] | ||
} |