From 2c0aa72bd839b458087b84753bd259cb6f69f101 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Tue, 20 Jul 2021 01:07:49 +0800 Subject: [PATCH] refactor(routes): remove `koa-compose` dep and save guard config in middleware --- packages/core/package.json | 6 ++-- packages/core/src/middleware/koa-guard.ts | 44 ++++++++++++++++++++--- packages/core/yarn.lock | 2 +- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 4b83b99a2..211d7aeb6 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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" } diff --git a/packages/core/src/middleware/koa-guard.ts b/packages/core/src/middleware/koa-guard.ts index 25bc90be4..aeba6097d 100644 --- a/packages/core/src/middleware/koa-guard.ts +++ b/packages/core/src/middleware/koa-guard.ts @@ -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 = { params: ParametersT; }; +export type WithGuarded< + ContextT extends IRouterParamContext, + GuardQueryT, + GuardBodyT, + GuardParametersT +> = ContextT & { + guard: Guarded; +}; + +export type WithGuardConfig< + Type, + GuardQueryT = unknown, + GuardBodyT = unknown, + GuardParametersT = unknown +> = Type & { + config: GuardConfig; +}; + export default function koaGuard< StateT, ContextT extends IRouterParamContext, @@ -30,12 +47,12 @@ export default function koaGuard< params, }: GuardConfig): Middleware< StateT, - ContextT & { guard: Guarded }, + WithGuarded, ResponseBodyT > { const guard: Middleware< StateT, - ContextT & { guard: Guarded }, + WithGuarded, 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, + ResponseBodyT + > + > = async function (ctx, next) { + if (body) { + await koaBody()(ctx, async () => { + await guard(ctx, next); + }); + return; + } + + await guard(ctx, next); + }; + + guardMiddleware.config = { query, body, params }; + + return guardMiddleware; } diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock index 9d6d55d5c..277554851 100644 --- a/packages/core/yarn.lock +++ b/packages/core/yarn.lock @@ -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==