@midwayjs/swagger 3.14.8 → 3.14.9-beta.1

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.
@@ -4,7 +4,9 @@ exports.ApiExcludeController = void 0;
4
4
  const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("../constants");
6
6
  function ApiExcludeController(disable = true) {
7
- return (0, core_1.createCustomMethodDecorator)(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, [disable], false);
7
+ return (target) => {
8
+ (0, core_1.saveClassMetadata)(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, { disable }, target);
9
+ };
8
10
  }
9
11
  exports.ApiExcludeController = ApiExcludeController;
10
12
  //# sourceMappingURL=api-exclude-controller.decorator.js.map
@@ -369,5 +369,9 @@ export interface SwaggerOptions {
369
369
  content: any;
370
370
  }>;
371
371
  swaggerUIRenderOptions?: Record<string, any>;
372
+ /**
373
+ * 自定义路由过滤器
374
+ */
375
+ routerFilter?: (url: string, options: RouterOption) => boolean;
372
376
  }
373
377
  //# sourceMappingURL=index.d.ts.map
@@ -16,6 +16,7 @@ const documentBuilder_1 = require("./documentBuilder");
16
16
  const _1 = require(".");
17
17
  let SwaggerExplorer = class SwaggerExplorer {
18
18
  constructor() {
19
+ this.swaggerConfig = {};
19
20
  this.documentBuilder = new documentBuilder_1.DocumentBuilder();
20
21
  this.operationIdFactory = (controllerKey, webRouter) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;
21
22
  }
@@ -96,17 +97,26 @@ let SwaggerExplorer = class SwaggerExplorer {
96
97
  return this.documentBuilder.build();
97
98
  }
98
99
  generatePath(target) {
99
- this.parseExtraModel(target);
100
- const metaForMethods = (0, core_1.getClassMetadata)(core_1.INJECT_CUSTOM_METHOD, target) || [];
101
- const exs = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_CONTROLLER);
102
- if (exs[0]) {
100
+ // 获取控制器元数据
101
+ const excludeClassMeta = (0, core_1.getClassMetadata)(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, target);
102
+ if (excludeClassMeta && excludeClassMeta.disable) {
103
+ // 如果存在需要排除的控制器,则直接返回
103
104
  return;
104
105
  }
106
+ // 解析额外的模型
107
+ this.parseExtraModel(target);
108
+ // 获取方法的元数据
109
+ const metaForClass = (0, core_1.getClassMetadata)(core_1.INJECT_CUSTOM_METHOD, target) || [];
110
+ // 获取参数的元数据
105
111
  const metaForParams = (0, core_1.getClassMetadata)(core_1.INJECT_CUSTOM_PARAM, target) || [];
112
+ // 获取控制器选项
106
113
  const controllerOption = (0, core_1.getClassMetadata)(core_1.CONTROLLER_KEY, target);
114
+ // 获取前缀
107
115
  const prefix = controllerOption.prefix;
108
- const tags = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_TAGS);
116
+ // 过滤出标签
117
+ const tags = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_TAGS);
109
118
  let strTags = [];
119
+ // 如果存在标签,则将其添加到文档构建器中
110
120
  if (tags.length > 0) {
111
121
  for (const t of tags) {
112
122
  // 这里 metadata => string[]
@@ -115,6 +125,7 @@ let SwaggerExplorer = class SwaggerExplorer {
115
125
  }
116
126
  }
117
127
  else {
128
+ // 如果不存在标签,则根据控制器选项生成标签
118
129
  const tag = { name: '', description: '' };
119
130
  if (prefix !== '/') {
120
131
  tag.name =
@@ -128,53 +139,71 @@ let SwaggerExplorer = class SwaggerExplorer {
128
139
  tag.description =
129
140
  (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
130
141
  }
142
+ // 如果标签名存在,则将其添加到文档构建器中
131
143
  if (tag.name) {
132
144
  strTags.push(tag.name);
133
145
  this.documentBuilder.addTag(tag.name, tag.description);
134
146
  }
135
147
  }
136
- // const globalMiddleware = controllerOption.routerOptions.middleware;
137
- // get router info
148
+ // 获取路由信息
138
149
  const webRouterInfo = (0, core_1.getClassMetadata)(core_1.WEB_ROUTER_KEY, target);
139
- let headers = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
150
+ // 过滤出头部信息
151
+ let headers = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
140
152
  if (headers.length > 0) {
141
153
  headers = headers.map(item => item.metadata);
142
154
  }
143
- const security = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
155
+ // 过滤出安全信息
156
+ const security = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
157
+ // 初始化路径对象
144
158
  const paths = {};
159
+ // 如果存在路由信息,则遍历生成路径
145
160
  if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
146
161
  for (const webRouter of webRouterInfo) {
162
+ // 生成URL
147
163
  let url = (prefix + webRouter.path).replace('//', '/');
148
164
  url = replaceUrl(url, parseParamsInPath(url));
149
165
  // 判断是否忽略当前路由
150
- const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
166
+ const endpoints = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
151
167
  item.propertyName === webRouter.method);
168
+ // 如果存在需要忽略的路由,则跳过当前循环
152
169
  if (endpoints[0]) {
153
170
  continue;
154
171
  }
172
+ // 判断是否需要过滤当前路由
173
+ if (this.swaggerConfig.routerFilter) {
174
+ const isFilter = this.swaggerConfig.routerFilter(url, webRouter);
175
+ if (isFilter) {
176
+ continue;
177
+ }
178
+ }
179
+ // 获取路由参数
155
180
  const routerArgs = metaForParams[webRouter.method] || [];
181
+ // 过滤出主体参数
156
182
  const bds = routerArgs.filter(item => {
157
183
  var _a;
158
184
  return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
159
185
  ((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) === core_1.RouteParamTypes.BODY;
160
186
  });
187
+ // 如果存在多个主体参数,则跳过当前循环,因为swagger不支持多个@Body
161
188
  if (bds.length > 1) {
162
- // swagger not support more than one @Body
163
189
  continue;
164
190
  }
165
- this.generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target);
166
- // 这里赋值 tags
191
+ // 生成路由方法
192
+ this.generateRouteMethod(url, webRouter, paths, metaForClass, routerArgs, headers, target);
193
+ // 如果当前路径的标签长度为0,则赋值标签
167
194
  if (paths[url][webRouter.requestMethod].tags.length === 0) {
168
195
  paths[url][webRouter.requestMethod].tags = strTags;
169
196
  }
170
- // extension => prefix 为 x-
171
- const exts = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXTENSION &&
197
+ // 过滤出扩展信息
198
+ const exts = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXTENSION &&
172
199
  item.propertyName === webRouter.method);
200
+ // 如果存在扩展信息,则将其添加到路径中
173
201
  for (const e of exts) {
174
202
  if (e.metadata) {
175
203
  Object.assign(paths[url][webRouter.requestMethod], e.metadata);
176
204
  }
177
205
  }
206
+ // 如果存在安全信息,则将其添加到路径中
178
207
  if (security.length > 0) {
179
208
  if (!paths[url][webRouter.requestMethod].security) {
180
209
  paths[url][webRouter.requestMethod].security = [];
@@ -188,6 +217,7 @@ let SwaggerExplorer = class SwaggerExplorer {
188
217
  }
189
218
  }
190
219
  }
220
+ // 将路径添加到文档构建器中
191
221
  this.documentBuilder.addPaths(paths);
192
222
  }
193
223
  /**
@@ -20,7 +20,7 @@ let SwaggerMiddleware = class SwaggerMiddleware {
20
20
  this.swaggerRender = this.swaggerConfig.swaggerUIRender(this.swaggerConfig, this.swaggerExplorer);
21
21
  }
22
22
  resolve(app) {
23
- if (app.getFrameworkType() === core_1.MidwayFrameworkType.WEB_EXPRESS) {
23
+ if (app.getNamespace() === 'express') {
24
24
  return async (req, res, next) => {
25
25
  const pathname = req.path;
26
26
  const renderResult = await this.swaggerRender(pathname);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.14.8",
3
+ "version": "3.14.9-beta.1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -27,6 +27,5 @@
27
27
  "repository": {
28
28
  "type": "git",
29
29
  "url": "https://github.com/midwayjs/midway.git"
30
- },
31
- "gitHead": "38fc9025b81b1697c18e7efa1e2f4b635f04dd6d"
30
+ }
32
31
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2013 - Now midwayjs
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.