@midwayjs/swagger 3.0.4-beta.1 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
- import { ILifeCycle, IMidwayContainer, MidwayApplicationManager } from '@midwayjs/core';
1
+ import { ILifeCycle, IMidwayContainer, MidwayApplicationManager, MidwayConfigService } from '@midwayjs/core';
2
2
  export declare class SwaggerConfiguration implements ILifeCycle {
3
3
  applicationManager: MidwayApplicationManager;
4
+ configService: MidwayConfigService;
4
5
  onReady(container: IMidwayContainer): Promise<void>;
5
6
  }
6
7
  //# sourceMappingURL=configuration.d.ts.map
@@ -16,19 +16,34 @@ const _1 = require(".");
16
16
  const DefaultConfig = require("./config/config.default");
17
17
  let SwaggerConfiguration = class SwaggerConfiguration {
18
18
  async onReady(container) {
19
- this.applicationManager
20
- .getApplications(['express', 'koa', 'egg', 'faas'])
21
- .forEach(app => {
22
- app.useMiddleware(_1.SwaggerMiddleware);
23
- });
24
- const explorer = await container.getAsync(_1.SwaggerExplorer);
25
- explorer.scanApp();
19
+ const apps = this.applicationManager.getApplications([
20
+ 'express',
21
+ 'koa',
22
+ 'egg',
23
+ 'faas',
24
+ ]);
25
+ if (apps.length) {
26
+ const globalPrefix = this.configService.getConfiguration('globalPrefix') ||
27
+ this.configService.getConfiguration('koa.globalPrefix') ||
28
+ this.configService.getConfiguration('express.globalPrefix') ||
29
+ this.configService.getConfiguration('egg.globalPrefix');
30
+ const explorer = await container.getAsync(_1.SwaggerExplorer);
31
+ explorer.addGlobalPrefix(globalPrefix);
32
+ explorer.scanApp();
33
+ apps.forEach(app => {
34
+ app.useMiddleware(_1.SwaggerMiddleware);
35
+ });
36
+ }
26
37
  }
27
38
  };
28
39
  __decorate([
29
40
  (0, decorator_1.Inject)(),
30
41
  __metadata("design:type", core_1.MidwayApplicationManager)
31
42
  ], SwaggerConfiguration.prototype, "applicationManager", void 0);
43
+ __decorate([
44
+ (0, decorator_1.Inject)(),
45
+ __metadata("design:type", core_1.MidwayConfigService)
46
+ ], SwaggerConfiguration.prototype, "configService", void 0);
32
47
  SwaggerConfiguration = __decorate([
33
48
  (0, decorator_1.Configuration)({
34
49
  importConfigs: [
@@ -1,2 +1,2 @@
1
- export declare function ApiTags(...tags: string[]): any;
1
+ export declare function ApiTags(tags: string | string[]): any;
2
2
  //# sourceMappingURL=api-tags.decorator.d.ts.map
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiTags = void 0;
4
4
  const constants_1 = require("../constants");
5
5
  const helpers_1 = require("./helpers");
6
- function ApiTags(...tags) {
6
+ function ApiTags(tags) {
7
7
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_TAGS, tags);
8
8
  }
9
9
  exports.ApiTags = ApiTags;
@@ -9,7 +9,6 @@ export declare class DocumentBuilder {
9
9
  setLicense(name: string, url: string): this;
10
10
  addServer(url: string, description?: string, variables?: Record<string, ServerVariableObject>): this;
11
11
  setExternalDoc(description: string, url: string): this;
12
- setBasePath(path: string | string[]): this;
13
12
  addPaths(paths: Record<string, PathItemObject>): this;
14
13
  addSchema(schema: Record<string, SchemaObject>): this;
15
14
  getSchema(name: string): SchemaObject;
@@ -49,23 +49,6 @@ class DocumentBuilder {
49
49
  this.document.externalDocs = { description, url };
50
50
  return this;
51
51
  }
52
- setBasePath(path) {
53
- if (this.document.basePath) {
54
- if (Array.isArray(this.document.basePath)) {
55
- this.document.basePath.push(path);
56
- }
57
- else {
58
- this.document.basePath = [
59
- this.document.basePath,
60
- path,
61
- ];
62
- }
63
- }
64
- else {
65
- this.document.basePath = path;
66
- }
67
- return this;
68
- }
69
52
  addPaths(paths) {
70
53
  Object.assign(this.document.paths, paths);
71
54
  return this;
@@ -326,10 +326,6 @@ export interface SwaggerOptions {
326
326
  * 可以参考 https://swagger.io/specification/#security-scheme-object
327
327
  */
328
328
  auth?: AuthOptions | AuthOptions[];
329
- /**
330
- * api 的 根路径
331
- */
332
- basePath?: string | string[];
333
329
  /**
334
330
  * 默认值: /swagger-ui
335
331
  * 访问 swagger ui 的路径
@@ -2,6 +2,7 @@ export declare class SwaggerExplorer {
2
2
  private swaggerConfig;
3
3
  private documentBuilder;
4
4
  init(): Promise<void>;
5
+ addGlobalPrefix(prefix: string): void;
5
6
  scanApp(): void;
6
7
  getData(): Omit<import("./interfaces").OpenAPIObject, "paths">;
7
8
  private generatePath;
@@ -31,9 +31,6 @@ let SwaggerExplorer = class SwaggerExplorer {
31
31
  typeof ((_k = this.swaggerConfig) === null || _k === void 0 ? void 0 : _k.license) === 'object') {
32
32
  this.documentBuilder.setLicense((_m = (_l = this.swaggerConfig) === null || _l === void 0 ? void 0 : _l.license) === null || _m === void 0 ? void 0 : _m.name, (_p = (_o = this.swaggerConfig) === null || _o === void 0 ? void 0 : _o.license) === null || _p === void 0 ? void 0 : _p.url);
33
33
  }
34
- if (this.swaggerConfig.basePath) {
35
- this.documentBuilder.setBasePath(this.swaggerConfig.basePath);
36
- }
37
34
  if (this.swaggerConfig.termsOfService) {
38
35
  this.documentBuilder.setTermsOfService(this.swaggerConfig.termsOfService);
39
36
  }
@@ -44,7 +41,7 @@ let SwaggerExplorer = class SwaggerExplorer {
44
41
  if (((_w = this.swaggerConfig) === null || _w === void 0 ? void 0 : _w.servers) &&
45
42
  Array.isArray((_x = this.swaggerConfig) === null || _x === void 0 ? void 0 : _x.servers)) {
46
43
  for (const serv of (_y = this.swaggerConfig) === null || _y === void 0 ? void 0 : _y.servers) {
47
- this.documentBuilder.addServer(serv === null || serv === void 0 ? void 0 : serv.url, serv === null || serv === void 0 ? void 0 : serv.description);
44
+ this.documentBuilder.addServer(serv === null || serv === void 0 ? void 0 : serv.url, serv === null || serv === void 0 ? void 0 : serv.description, serv === null || serv === void 0 ? void 0 : serv.variables);
48
45
  }
49
46
  }
50
47
  if (((_z = this.swaggerConfig) === null || _z === void 0 ? void 0 : _z.tags) && Array.isArray((_0 = this.swaggerConfig) === null || _0 === void 0 ? void 0 : _0.tags)) {
@@ -62,6 +59,12 @@ let SwaggerExplorer = class SwaggerExplorer {
62
59
  this.setAuth((_5 = this.swaggerConfig) === null || _5 === void 0 ? void 0 : _5.auth);
63
60
  }
64
61
  }
62
+ addGlobalPrefix(prefix) {
63
+ if (!prefix) {
64
+ return;
65
+ }
66
+ this.documentBuilder.addServer(prefix);
67
+ }
65
68
  scanApp() {
66
69
  var _a;
67
70
  const routes = (0, decorator_1.listModule)(decorator_1.CONTROLLER_KEY);
@@ -132,7 +135,17 @@ let SwaggerExplorer = class SwaggerExplorer {
132
135
  if (endpoints[0]) {
133
136
  continue;
134
137
  }
135
- this.generateRouteMethod(url, webRouter, paths, metaForMethods, metaForParams, header, target);
138
+ const routerArgs = metaForParams[webRouter.method] || [];
139
+ const bds = routerArgs.filter(item => {
140
+ var _a;
141
+ return item.key === decorator_1.WEB_ROUTER_PARAM_KEY &&
142
+ ((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) === decorator_1.RouteParamTypes.BODY;
143
+ });
144
+ if (bds.length > 1) {
145
+ // swagger not support more than one @Body
146
+ continue;
147
+ }
148
+ this.generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, header, target);
136
149
  // 这里赋值 tags
137
150
  if (paths[url][webRouter.requestMethod].tags.length === 0) {
138
151
  paths[url][webRouter.requestMethod].tags = strTags;
@@ -163,7 +176,7 @@ let SwaggerExplorer = class SwaggerExplorer {
163
176
  /**
164
177
  * 构造 router 提取方法
165
178
  */
166
- generateRouteMethod(url, webRouter, paths, metaForMethods, metaForParams, header, target) {
179
+ generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, header, target) {
167
180
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
168
181
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
169
182
  item.propertyName === webRouter.method)[0];
@@ -182,10 +195,8 @@ let SwaggerExplorer = class SwaggerExplorer {
182
195
  * [{"key":"web:router_param","parameterIndex":1,"propertyName":"create","metadata":{"type":2}},
183
196
  * {"key":"web:router_param","parameterIndex":0,"propertyName":"create","metadata":{"type":1,"propertyData":"createCatDto"}}]
184
197
  */
185
- const routerArgs = metaForParams[webRouter.method] || [];
186
198
  // WEB_ROUTER_PARAM_KEY
187
- let args = routerArgs.filter(item => item.key === decorator_1.WEB_ROUTER_PARAM_KEY);
188
- args = args.filter(item => item.key === decorator_1.WEB_ROUTER_PARAM_KEY);
199
+ const args = routerArgs.filter(item => item.key === decorator_1.WEB_ROUTER_PARAM_KEY);
189
200
  const types = (0, decorator_1.getMethodParamTypes)(target, webRouter.method);
190
201
  const params = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_PARAMETERS &&
191
202
  item.propertyName === webRouter.method);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.0.4-beta.1",
3
+ "version": "3.0.4",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -9,11 +9,11 @@
9
9
  "index.d.ts"
10
10
  ],
11
11
  "devDependencies": {
12
- "@midwayjs/core": "^3.0.4-beta.1",
13
- "@midwayjs/decorator": "^3.0.4-beta.1",
14
- "@midwayjs/koa": "^3.0.4-beta.1",
15
- "@midwayjs/mock": "^3.0.4-beta.1",
16
- "swagger-ui-dist": "4.4.0"
12
+ "@midwayjs/core": "^3.0.4",
13
+ "@midwayjs/decorator": "^3.0.4",
14
+ "@midwayjs/koa": "^3.0.4",
15
+ "@midwayjs/mock": "^3.0.4",
16
+ "swagger-ui-dist": "4.5.0"
17
17
  },
18
18
  "author": "Kurten Chan <chinkurten@gmail.com>",
19
19
  "license": "MIT",
@@ -27,5 +27,5 @@
27
27
  "type": "git",
28
28
  "url": "https://github.com/midwayjs/midway.git"
29
29
  },
30
- "gitHead": "52bc37e7a570806c121b33a0e8ac1cd084445f5d"
30
+ "gitHead": "c2a37dd026c7bf3e855f4498691f572ef61396e3"
31
31
  }