0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor: patch type issues

This commit is contained in:
Gao Sun 2024-07-13 21:30:35 +08:00
parent e3109af026
commit f96277b410
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 17 additions and 6 deletions

View file

@ -55,12 +55,19 @@ export const buildOidcClientMetadata = (metadata?: OidcClientMetadata): OidcClie
...metadata,
});
export const validateCustomClientMetadata = (key: string, value: unknown) => {
const result = customClientMetadataGuard.pick({ [key]: true }).safeParse({ [key]: value });
// eslint-disable-next-line @typescript-eslint/ban-types
const isKeyOf = <T extends object>(object: T, key: string | number | symbol): key is keyof T =>
key in object;
if (!result.success) {
throw new errors.InvalidClientMetadata(key);
export const validateCustomClientMetadata = (key: string, value: unknown) => {
if (isKeyOf(customClientMetadataGuard.shape, key)) {
const result = customClientMetadataGuard.shape[key].safeParse(value);
if (result.success) {
return;
}
}
throw new errors.InvalidClientMetadata(key);
};
export const isOriginAllowed = (

View file

@ -8,6 +8,7 @@ import {
jwtCustomizerConfigsGuard,
jwtCustomizerTestRequestBodyGuard,
} from '@logto/schemas';
import { removeUndefinedKeys } from '@silverhand/essentials';
import { ResponseError } from '@withtyped/client';
import { ZodError, z } from 'zod';
@ -236,7 +237,7 @@ export default function logtoConfigJwtCustomizerRoutes<T extends ManagementApiRo
search: { isTest: 'true' },
});
} else {
ctx.body = await JwtCustomizerLibrary.runScriptInLocalVm(body);
ctx.body = removeUndefinedKeys(await JwtCustomizerLibrary.runScriptInLocalVm(body));
}
} catch (error: unknown) {
/**

View file

@ -286,7 +286,9 @@ export default class SchemaRouter<
this.delete(
`/:id/${pathname}/:${camelCaseSchemaId(relationSchema)}`,
koaGuard({
params: z.object({ id: z.string().min(1), [relationSchemaId]: z.string().min(1) }),
params: z
.object({ id: z.string().min(1) })
.extend({ [relationSchemaId]: z.string().min(1) }),
status: [204, 422],
}),
async (ctx, next) => {
@ -337,6 +339,7 @@ export default class SchemaRouter<
this.post(
'/',
koaGuard({
// @ts-expect-error -- `.omit()` doesn't play well with generics
body: schema.createGuard.omit({ id: true }),
response: entityGuard ?? schema.guard,
status: [201], // TODO: 409/422 for conflict?