mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
refactor(core): use discriminate union for custom jwt test API
This commit is contained in:
parent
5fbee28565
commit
ef7e8e1fda
1 changed files with 30 additions and 21 deletions
|
@ -17,7 +17,7 @@ import {
|
||||||
LogtoJwtTokenKey,
|
LogtoJwtTokenKey,
|
||||||
LogtoJwtTokenPath,
|
LogtoJwtTokenPath,
|
||||||
jsonObjectGuard,
|
jsonObjectGuard,
|
||||||
customJwtFetcherGuard,
|
type CustomJwtFetcher,
|
||||||
} from '@logto/schemas';
|
} from '@logto/schemas';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
@ -296,38 +296,47 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
|
||||||
}
|
}
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/configs/jwt-customizer/:tokenTypePath/test',
|
'/configs/jwt-customizer/test',
|
||||||
koaGuard({
|
koaGuard({
|
||||||
params: z.object({
|
body: z.discriminatedUnion('tokenType', [
|
||||||
tokenTypePath: z.nativeEnum(LogtoJwtTokenPath),
|
z.object({
|
||||||
}),
|
tokenType: z.literal(LogtoJwtTokenKey.AccessToken),
|
||||||
body: z.unknown(),
|
payload: accessTokenJwtCustomizerGuard,
|
||||||
|
}),
|
||||||
|
z.object({
|
||||||
|
tokenType: z.literal(LogtoJwtTokenKey.ClientCredentials),
|
||||||
|
payload: clientCredentialsJwtCustomizerGuard,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
response: jsonObjectGuard,
|
response: jsonObjectGuard,
|
||||||
/**
|
/**
|
||||||
* 400 for cloud service zod error (data type does not match expectation, can be either request body or response body)
|
* Code 400 indicates Zod errors in cloud service (data type does not match expectation, can be either request body or response body).
|
||||||
* 422 for cloud service syntax error
|
* Code 422 indicates syntax errors in cloud service.
|
||||||
*/
|
*/
|
||||||
status: [200, 400, 422],
|
status: [200, 400, 422],
|
||||||
}),
|
}),
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const {
|
const {
|
||||||
params: { tokenTypePath },
|
body: {
|
||||||
body: rawBody,
|
payload: { tokenSample, contextSample, ...rest },
|
||||||
|
},
|
||||||
} = ctx.guard;
|
} = ctx.guard;
|
||||||
const {
|
|
||||||
body: { tokenSample, contextSample, ...rest },
|
|
||||||
} = getJwtTokenKeyAndBody(tokenTypePath, rawBody);
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We have ensured the API request body via koa guard, manually cast the cloud service API call's
|
||||||
|
* `requestBody` type and let the cloud service API to throw if needed.
|
||||||
|
*/
|
||||||
|
// eslint-disable-next-line no-restricted-syntax, @typescript-eslint/consistent-type-assertions
|
||||||
|
const requestBody = {
|
||||||
|
...rest,
|
||||||
|
token: tokenSample,
|
||||||
|
context: contextSample,
|
||||||
|
} as CustomJwtFetcher;
|
||||||
const client = await cloudConnection.getClient();
|
const client = await cloudConnection.getClient();
|
||||||
const testResult = await client.post(`/api/services/custom-jwt`, {
|
|
||||||
body: customJwtFetcherGuard.parse({
|
|
||||||
...rest,
|
|
||||||
tokenSample,
|
|
||||||
contextSample,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.body = testResult;
|
ctx.body = await client.post(`/api/services/custom-jwt`, {
|
||||||
|
body: requestBody,
|
||||||
|
});
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue