mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
refactor(core): overload function interface
This commit is contained in:
parent
5d8e6853d1
commit
0f0247ce2f
2 changed files with 17 additions and 23 deletions
|
@ -92,33 +92,27 @@ export const isGuardMiddleware = <Type extends IMiddleware>(
|
||||||
): function_ is WithGuardConfig<Type> =>
|
): function_ is WithGuardConfig<Type> =>
|
||||||
function_.name === 'guardMiddleware' && has(function_, 'config');
|
function_.name === 'guardMiddleware' && has(function_, 'config');
|
||||||
|
|
||||||
/**
|
export function tryParse<Output, Definition extends ZodTypeDef, Input>(
|
||||||
* Previous `tryParse` function's output type was `Output | undefined`.
|
|
||||||
* It can not properly infer the output type to be `Output` even if the guard is provided,
|
|
||||||
* which brings additional but unnecessary type checks.
|
|
||||||
*/
|
|
||||||
export const parse = <Output, Definition extends ZodTypeDef, Input>(
|
|
||||||
type: 'query' | 'body' | 'params' | 'files',
|
type: 'query' | 'body' | 'params' | 'files',
|
||||||
guard: ZodType<Output, Definition, Input>,
|
guard: ZodType<Output, Definition, Input>,
|
||||||
data: unknown
|
data: unknown
|
||||||
) => {
|
): Output;
|
||||||
try {
|
export function tryParse<Output, Definition extends ZodTypeDef, Input>(
|
||||||
return guard.parse(data);
|
type: 'query' | 'body' | 'params' | 'files',
|
||||||
} catch (error: unknown) {
|
guard: undefined,
|
||||||
throw new RequestError({ code: 'guard.invalid_input', type }, error);
|
data: unknown
|
||||||
}
|
): undefined;
|
||||||
};
|
export function tryParse<Output, Definition extends ZodTypeDef, Input>(
|
||||||
|
|
||||||
const tryParse = <Output, Definition extends ZodTypeDef, Input>(
|
|
||||||
type: 'query' | 'body' | 'params' | 'files',
|
type: 'query' | 'body' | 'params' | 'files',
|
||||||
guard: Optional<ZodType<Output, Definition, Input>>,
|
guard: Optional<ZodType<Output, Definition, Input>>,
|
||||||
data: unknown
|
data: unknown
|
||||||
) => {
|
) {
|
||||||
if (!guard) {
|
try {
|
||||||
return;
|
return guard?.parse(data);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
throw new RequestError({ code: 'guard.invalid_input', type }, error);
|
||||||
}
|
}
|
||||||
return parse(type, guard, data);
|
}
|
||||||
};
|
|
||||||
|
|
||||||
export default function koaGuard<
|
export default function koaGuard<
|
||||||
StateT,
|
StateT,
|
||||||
|
|
|
@ -19,7 +19,7 @@ import {
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import RequestError from '#src/errors/RequestError/index.js';
|
import RequestError from '#src/errors/RequestError/index.js';
|
||||||
import koaGuard, { parse } from '#src/middleware/koa-guard.js';
|
import koaGuard, { tryParse } from '#src/middleware/koa-guard.js';
|
||||||
import { exportJWK } from '#src/utils/jwks.js';
|
import { exportJWK } from '#src/utils/jwks.js';
|
||||||
|
|
||||||
import type { AuthedRouter, RouterInitArgs } from './types.js';
|
import type { AuthedRouter, RouterInitArgs } from './types.js';
|
||||||
|
@ -41,12 +41,12 @@ const getJwtTokenKeyAndBody = (tokenPath: LogtoJwtTokenPath, body: unknown) => {
|
||||||
if (tokenPath === LogtoJwtTokenPath.AccessToken) {
|
if (tokenPath === LogtoJwtTokenPath.AccessToken) {
|
||||||
return {
|
return {
|
||||||
key: LogtoJwtTokenKey.AccessToken,
|
key: LogtoJwtTokenKey.AccessToken,
|
||||||
body: parse('body', jwtCustomizerAccessTokenGuard, body),
|
body: tryParse('body', jwtCustomizerAccessTokenGuard, body),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
key: LogtoJwtTokenKey.ClientCredentials,
|
key: LogtoJwtTokenKey.ClientCredentials,
|
||||||
body: parse('body', jwtCustomizerClientCredentialsGuard, body),
|
body: tryParse('body', jwtCustomizerClientCredentialsGuard, body),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue