0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -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
parent 869a2425a2
commit b435414d91
No known key found for this signature in database
GPG key ID: 0F0EFA2E36639F31
3 changed files with 44 additions and 8 deletions

View file

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

View file

@ -1,7 +1,6 @@
import RequestError, { GuardErrorCode } from '@/errors/RequestError';
import { Middleware } from 'koa';
import koaBody from 'koa-body';
import compose from 'koa-compose';
import { IRouterParamContext } from 'koa-router';
import { ZodType } from 'zod';
@ -17,6 +16,24 @@ export type Guarded<QueryT, BodyT, 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<
StateT,
ContextT extends IRouterParamContext,
@ -30,12 +47,12 @@ export default function koaGuard<
params,
}: GuardConfig<GuardQueryT, GuardBodyT, GuardParametersT>): Middleware<
StateT,
ContextT & { guard: Guarded<GuardQueryT, GuardBodyT, GuardParametersT> },
WithGuarded<ContextT, GuardQueryT, GuardBodyT, GuardParametersT>,
ResponseBodyT
> {
const guard: Middleware<
StateT,
ContextT & { guard: Guarded<GuardQueryT, GuardBodyT, GuardParametersT> },
WithGuarded<ContextT, GuardQueryT, GuardBodyT, GuardParametersT>,
ResponseBodyT
> = async (ctx, next) => {
try {
@ -52,5 +69,24 @@ export default function koaGuard<
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:
"@types/node" "*"
"@types/koa-compose@*", "@types/koa-compose@^3.2.5":
"@types/koa-compose@*":
version "3.2.5"
resolved "https://registry.yarnpkg.com/@types/koa-compose/-/koa-compose-3.2.5.tgz#85eb2e80ac50be95f37ccf8c407c09bbe3468e9d"
integrity sha512-B8nG/OoE1ORZqCkBVsup/AKcvjdgoHnfi4pZMn5UwAPCbhk/96xyv284eBYW8JlQbQ7zDmnpFr68I/40mFoIBQ==