0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-03 21:48:55 -05:00

refactor(routes): remove koa-compose dep and save guard config in middleware

This commit is contained in:
Gao Sun 2021-07-20 01:07:49 +08:00 committed by Gao Sun
parent 1d8ac4bc8e
commit 2c0aa72bd8
3 changed files with 44 additions and 8 deletions

View file

@ -22,7 +22,6 @@
"got": "^11.8.2", "got": "^11.8.2",
"koa": "^2.13.1", "koa": "^2.13.1",
"koa-body": "^4.2.0", "koa-body": "^4.2.0",
"koa-compose": "^4.1.0",
"koa-logger": "^3.2.1", "koa-logger": "^3.2.1",
"koa-mount": "^4.0.0", "koa-mount": "^4.0.0",
"koa-proxies": "^0.12.1", "koa-proxies": "^0.12.1",
@ -41,7 +40,6 @@
"@logto/eslint-config": "^0.1.0-rc.6", "@logto/eslint-config": "^0.1.0-rc.6",
"@logto/ts-config": "^0.1.0-rc.6", "@logto/ts-config": "^0.1.0-rc.6",
"@types/koa": "^2.13.3", "@types/koa": "^2.13.3",
"@types/koa-compose": "^3.2.5",
"@types/koa-logger": "^3.1.1", "@types/koa-logger": "^3.1.1",
"@types/koa-mount": "^4.0.0", "@types/koa-mount": "^4.0.0",
"@types/koa-router": "^7.4.2", "@types/koa-router": "^7.4.2",
@ -63,6 +61,8 @@
"_moduleAliases": { "_moduleAliases": {
"@": "./build" "@": "./build"
}, },
"eslintConfig": { "extends": "@logto" }, "eslintConfig": {
"extends": "@logto"
},
"prettier": "@logto/eslint-config/.prettierrc" "prettier": "@logto/eslint-config/.prettierrc"
} }

View file

@ -1,7 +1,6 @@
import RequestError, { GuardErrorCode } from '@/errors/RequestError'; import RequestError, { GuardErrorCode } from '@/errors/RequestError';
import { Middleware } from 'koa'; import { Middleware } from 'koa';
import koaBody from 'koa-body'; import koaBody from 'koa-body';
import compose from 'koa-compose';
import { IRouterParamContext } from 'koa-router'; import { IRouterParamContext } from 'koa-router';
import { ZodType } from 'zod'; import { ZodType } from 'zod';
@ -17,6 +16,24 @@ export type Guarded<QueryT, BodyT, ParametersT> = {
params: ParametersT; params: ParametersT;
}; };
export type WithGuarded<
ContextT extends IRouterParamContext,
GuardQueryT,
GuardBodyT,
GuardParametersT
> = ContextT & {
guard: Guarded<GuardQueryT, GuardBodyT, GuardParametersT>;
};
export type WithGuardConfig<
Type,
GuardQueryT = unknown,
GuardBodyT = unknown,
GuardParametersT = unknown
> = Type & {
config: GuardConfig<GuardQueryT, GuardBodyT, GuardParametersT>;
};
export default function koaGuard< export default function koaGuard<
StateT, StateT,
ContextT extends IRouterParamContext, ContextT extends IRouterParamContext,
@ -30,12 +47,12 @@ export default function koaGuard<
params, params,
}: GuardConfig<GuardQueryT, GuardBodyT, GuardParametersT>): Middleware< }: GuardConfig<GuardQueryT, GuardBodyT, GuardParametersT>): Middleware<
StateT, StateT,
ContextT & { guard: Guarded<GuardQueryT, GuardBodyT, GuardParametersT> }, WithGuarded<ContextT, GuardQueryT, GuardBodyT, GuardParametersT>,
ResponseBodyT ResponseBodyT
> { > {
const guard: Middleware< const guard: Middleware<
StateT, StateT,
ContextT & { guard: Guarded<GuardQueryT, GuardBodyT, GuardParametersT> }, WithGuarded<ContextT, GuardQueryT, GuardBodyT, GuardParametersT>,
ResponseBodyT ResponseBodyT
> = async (ctx, next) => { > = async (ctx, next) => {
try { try {
@ -52,5 +69,24 @@ export default function koaGuard<
await next(); await next();
}; };
return body ? compose([koaBody(), guard]) : guard; const guardMiddleware: WithGuardConfig<
Middleware<
StateT,
WithGuarded<ContextT, GuardQueryT, GuardBodyT, GuardParametersT>,
ResponseBodyT
>
> = async function (ctx, next) {
if (body) {
await koaBody<StateT, ContextT>()(ctx, async () => {
await guard(ctx, next);
});
return;
}
await guard(ctx, next);
};
guardMiddleware.config = { query, body, params };
return guardMiddleware;
} }

View file

@ -605,7 +605,7 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@types/koa-compose@*", "@types/koa-compose@^3.2.5": "@types/koa-compose@*":
version "3.2.5" version "3.2.5"
resolved "https://registry.yarnpkg.com/@types/koa-compose/-/koa-compose-3.2.5.tgz#85eb2e80ac50be95f37ccf8c407c09bbe3468e9d" resolved "https://registry.yarnpkg.com/@types/koa-compose/-/koa-compose-3.2.5.tgz#85eb2e80ac50be95f37ccf8c407c09bbe3468e9d"
integrity sha512-B8nG/OoE1ORZqCkBVsup/AKcvjdgoHnfi4pZMn5UwAPCbhk/96xyv284eBYW8JlQbQ7zDmnpFr68I/40mFoIBQ== integrity sha512-B8nG/OoE1ORZqCkBVsup/AKcvjdgoHnfi4pZMn5UwAPCbhk/96xyv284eBYW8JlQbQ7zDmnpFr68I/40mFoIBQ==