From 1010b1114228a445d5dd985524847744b6827a74 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Thu, 21 Mar 2024 10:36:01 +0800 Subject: [PATCH] refactor(console,core): jwt test api integration jwt test api integration --- .../JwtClaims/SettingsSection/TestTab.tsx | 2 +- .../src/pages/JwtClaims/utils/format.ts | 28 ++++++++++++------- packages/core/src/routes/logto-config.ts | 4 +-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/console/src/pages/JwtClaims/SettingsSection/TestTab.tsx b/packages/console/src/pages/JwtClaims/SettingsSection/TestTab.tsx index 3f4299ae2ef0..06e102e50d0b 100644 --- a/packages/console/src/pages/JwtClaims/SettingsSection/TestTab.tsx +++ b/packages/console/src/pages/JwtClaims/SettingsSection/TestTab.tsx @@ -26,7 +26,7 @@ type Props = { const userTokenModelSettings = [accessTokenPayloadTestModel, userContextTestModel]; const machineToMachineTokenModelSettings = [clientCredentialsPayloadTestModel]; -const testEndpointPath = 'api/config/jwt-customizer/test'; +const testEndpointPath = 'api/configs/jwt-customizer/test'; function TestTab({ isActive }: Props) { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.jwt_claims' }); diff --git a/packages/console/src/pages/JwtClaims/utils/format.ts b/packages/console/src/pages/JwtClaims/utils/format.ts index fb91325449c5..31c8308c5e14 100644 --- a/packages/console/src/pages/JwtClaims/utils/format.ts +++ b/packages/console/src/pages/JwtClaims/utils/format.ts @@ -86,21 +86,29 @@ export const formatFormDataToRequestData = (data: JwtClaimsFormType) => { }; }; -export const formatFormDataToTestRequestPayload = (data: JwtClaimsFormType) => { - const defaultTokenPayload = - data.tokenType === LogtoJwtTokenPath.AccessToken +export const formatFormDataToTestRequestPayload = ({ + tokenType, + script, + environmentVariables, + testSample, +}: JwtClaimsFormType) => { + const defaultTokenSample = + tokenType === LogtoJwtTokenPath.AccessToken ? defaultAccessTokenPayload : defaultClientCredentialsPayload; - const defaultContext = - data.tokenType === LogtoJwtTokenPath.AccessToken ? defaultUserTokenContextData : undefined; + const defaultContextSample = + tokenType === LogtoJwtTokenPath.AccessToken ? defaultUserTokenContextData : undefined; return { - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- parse empty string as undefined - script: data.script || undefined, - envVars: formatEnvVariablesFormData(data.environmentVariables), - token: formatSampleCodeStringToJson(data.testSample?.tokenSample) ?? defaultTokenPayload, - context: formatSampleCodeStringToJson(data.testSample?.contextSample) ?? defaultContext, + tokenType, + payload: { + script, + envVars: formatEnvVariablesFormData(environmentVariables), + tokenSample: formatSampleCodeStringToJson(testSample?.tokenSample) ?? defaultTokenSample, + contextSample: + formatSampleCodeStringToJson(testSample?.contextSample) ?? defaultContextSample, + }, }; }; diff --git a/packages/core/src/routes/logto-config.ts b/packages/core/src/routes/logto-config.ts index 91067ce8e502..37d858c7c606 100644 --- a/packages/core/src/routes/logto-config.ts +++ b/packages/core/src/routes/logto-config.ts @@ -304,14 +304,14 @@ export default function logtoConfigRoutes( */ body: z.discriminatedUnion('tokenType', [ z.object({ - tokenType: z.literal(LogtoJwtTokenKey.AccessToken), + tokenType: z.literal(LogtoJwtTokenPath.AccessToken), payload: accessTokenJwtCustomizerGuard.required({ script: true, tokenSample: true, }), }), z.object({ - tokenType: z.literal(LogtoJwtTokenKey.ClientCredentials), + tokenType: z.literal(LogtoJwtTokenPath.ClientCredentials), payload: clientCredentialsJwtCustomizerGuard.required({ script: true, tokenSample: true,