@midwayjs/swagger 3.14.4 → 3.14.7

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.
@@ -22,18 +22,19 @@ let SwaggerConfiguration = class SwaggerConfiguration {
22
22
  'faas',
23
23
  ]);
24
24
  if (apps.length) {
25
+ const explorer = await container.getAsync(_1.SwaggerExplorer);
26
+ explorer.scanApp();
27
+ // 添加统一前缀
25
28
  let globalPrefix = this.configService.getConfiguration('globalPrefix') ||
26
29
  this.configService.getConfiguration('koa.globalPrefix') ||
27
30
  this.configService.getConfiguration('express.globalPrefix') ||
28
31
  this.configService.getConfiguration('egg.globalPrefix');
29
- const explorer = await container.getAsync(_1.SwaggerExplorer);
30
32
  if (globalPrefix) {
31
33
  if (!/^\//.test(globalPrefix)) {
32
34
  globalPrefix = '/' + globalPrefix;
33
35
  }
34
36
  explorer.addGlobalPrefix(globalPrefix);
35
37
  }
36
- explorer.scanApp();
37
38
  apps.forEach(app => {
38
39
  app.useMiddleware(_1.SwaggerMiddleware);
39
40
  });
@@ -1,4 +1,4 @@
1
- import { OpenAPIObject, ExternalDocumentationObject, SecurityRequirementObject, SecuritySchemeObject, ServerVariableObject, PathItemObject, SchemaObject } from './interfaces';
1
+ import { OpenAPIObject, ExternalDocumentationObject, SecurityRequirementObject, SecuritySchemeObject, ServerVariableObject, PathItemObject, SchemaObject, PathsObject } from './interfaces';
2
2
  export declare class DocumentBuilder {
3
3
  private readonly document;
4
4
  setTitle(title: string): this;
@@ -10,6 +10,7 @@ export declare class DocumentBuilder {
10
10
  addServer(url: string, description?: string, variables?: Record<string, ServerVariableObject>): this;
11
11
  setExternalDoc(description: string, url: string): this;
12
12
  addPaths(paths: Record<string, PathItemObject>): this;
13
+ getPaths(): PathsObject;
13
14
  addSchema(schema: Record<string, SchemaObject>): this;
14
15
  getSchema(name: string): SchemaObject;
15
16
  addTag(name: string, description?: string, externalDocs?: ExternalDocumentationObject): this;
@@ -53,6 +53,9 @@ class DocumentBuilder {
53
53
  Object.assign(this.document.paths, paths);
54
54
  return this;
55
55
  }
56
+ getPaths() {
57
+ return this.document.paths;
58
+ }
56
59
  addSchema(schema) {
57
60
  if (!this.document.components.schemas) {
58
61
  this.document.components.schemas = {};
@@ -5,7 +5,7 @@ export declare class SwaggerExplorer {
5
5
  private documentBuilder;
6
6
  private operationIdFactory;
7
7
  init(): Promise<void>;
8
- addGlobalPrefix(prefix: string): void;
8
+ addGlobalPrefix(globalPrefix: string): void;
9
9
  scanApp(): void;
10
10
  getData(): Omit<import("./interfaces").OpenAPIObject, "paths">;
11
11
  protected generatePath(target: Type): void;
@@ -64,11 +64,23 @@ let SwaggerExplorer = class SwaggerExplorer {
64
64
  this.setAuth((_7 = this.swaggerConfig) === null || _7 === void 0 ? void 0 : _7.auth);
65
65
  }
66
66
  }
67
- addGlobalPrefix(prefix) {
68
- if (!prefix) {
67
+ addGlobalPrefix(globalPrefix) {
68
+ if (!globalPrefix) {
69
69
  return;
70
70
  }
71
- this.documentBuilder.addServer(prefix);
71
+ const paths = this.documentBuilder.getPaths();
72
+ // 添加统一前缀后的接口地址
73
+ const newPaths = {};
74
+ for (const [routerUrl, value] of Object.entries(paths)) {
75
+ // 处理路由
76
+ if (!/^\//.test(routerUrl)) {
77
+ newPaths[`${globalPrefix}/${routerUrl}`] = value;
78
+ }
79
+ else {
80
+ newPaths[`${globalPrefix}${routerUrl}`] = value;
81
+ }
82
+ }
83
+ this.documentBuilder.addPaths(newPaths);
72
84
  }
73
85
  scanApp() {
74
86
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.14.4",
3
+ "version": "3.14.7",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -11,9 +11,9 @@
11
11
  ],
12
12
  "devDependencies": {
13
13
  "@midwayjs/core": "^3.14.4",
14
- "@midwayjs/koa": "^3.14.4",
15
- "@midwayjs/mock": "^3.14.4",
16
- "@midwayjs/validate": "^3.14.4",
14
+ "@midwayjs/koa": "^3.14.7",
15
+ "@midwayjs/mock": "^3.14.7",
16
+ "@midwayjs/validate": "^3.14.7",
17
17
  "swagger-ui-dist": "4.19.1"
18
18
  },
19
19
  "author": "Kurten Chan <chinkurten@gmail.com>",
@@ -28,5 +28,5 @@
28
28
  "type": "git",
29
29
  "url": "https://github.com/midwayjs/midway.git"
30
30
  },
31
- "gitHead": "72aacdfd87ef730f100690557de48dcbd4d806a7"
31
+ "gitHead": "148356ccd4b97404dde43d4a06edd51da039828a"
32
32
  }