فهرست منبع

bug:统一返回中间件拦截api文档请求

lrf 2 سال پیش
والد
کامیت
16c3d911c1
1فایلهای تغییر یافته به همراه13 افزوده شده و 6 حذف شده
  1. 13 6
      src/middleware/response.middleware.ts

+ 13 - 6
src/middleware/response.middleware.ts

@@ -1,16 +1,23 @@
 import { IMiddleware } from '@midwayjs/core';
-import { Middleware } from '@midwayjs/decorator';
-import { NextFunction, Context } from '@midwayjs/koa';
+import { Middleware, App } from '@midwayjs/decorator';
+import { NextFunction, Context, Application } from '@midwayjs/koa';
 import { VOBase } from '../interface/VOBase';
 @Middleware()
 export class ResponseMiddleware implements IMiddleware<Context, NextFunction> {
+  @App()
+  app: Application;
   resolve() {
     return async (ctx: Context, next: NextFunction) => {
       await next();
-      const response = ctx.response;
-      const body = response.body;
-      const nb = new VOBase(body as object);
-      return nb;
+      // 过滤掉 api文档的请求
+      const swaggerPath = this.app.getConfig()?.swagger?.swaggerPath;
+      const url = _.get(ctx, 'request.originalUrl');
+      if (!url.includes(swaggerPath)) {
+        const response = ctx.response;
+        const body = response.body;
+        const nb = new VOBase(body as object);
+        return nb;
+      }
     };
   }