master
2022-03-08 ea91d26a2c90e8f70b6433b32a78aa840e501c58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web';
import { Context } from 'egg';
import * as _ from 'lodash';
 
/**
 * swagger
 */
@Provide()
export class BaseSwaggerMiddleware implements IWebMiddleware {
  resolve() {
    return async (ctx: Context, next: IMidwayWebNext) => {
      const { url } = ctx;
      await next();
      if (_.startsWith(url, '/swagger-ui/json')) {
        ctx.body.components.securitySchemes = {
          Authorization: {
            type: 'apiKey',
            name: 'Authorization',
            in: 'header',
          },
        };
        ctx.body.security = [{ Authorization: [] }];
      }
    };
  }
}