-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
44 lines (38 loc) · 1.3 KB
/
app.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { IOptionalOpenapiRouterConfig, OpenapiRouter } from '@rickyli79/koa-openapi-router';
import { Application, IBoot } from 'egg';
import { OPENAPI_ROUTER_LOGGER } from './config/types';
export default class implements IBoot {
protected app: Application;
constructor(app: Application) {
this.app = app;
}
public async willReady() {
await this.initOpenapiRouter();
}
private async initOpenapiRouter() {
const logger = this.app.getLogger(OPENAPI_ROUTER_LOGGER);
const configs: IOptionalOpenapiRouterConfig[] = [];
const config = this.app.config.openapiRouter;
if (config.config) {
configs.push(config.config);
}
if (config.configs) {
const cfgs = config.configs;
for (const iConfigKey in cfgs) {
const iConfig = cfgs[iConfigKey];
iConfig.routerPrefix = iConfigKey;
configs.push(iConfig);
}
}
let proxyAction:any|undefined;
if (config.options?.proxyAction) {
const paths = config.options.proxyAction.split('/');
proxyAction = <any> this.app.middleware;
for (const iPath of paths) {
proxyAction = proxyAction[iPath];
}
proxyAction = this.app.middleware[config.options.proxyAction];
}
await OpenapiRouter.Start(this.app, configs, { ...config.options, proxyAction, logger });
}
}