@midwayjs/swagger 3.14.8 → 3.14.9-beta.2
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 (
|
|
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
|
package/dist/swaggerExplorer.js
CHANGED
|
@@ -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,25 +97,37 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
96
97
|
return this.documentBuilder.build();
|
|
97
98
|
}
|
|
98
99
|
generatePath(target) {
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
116
|
+
// 过滤出标签
|
|
117
|
+
const tags = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_TAGS);
|
|
109
118
|
let strTags = [];
|
|
119
|
+
const controllerTags = [];
|
|
120
|
+
// 如果存在标签,则将其添加到文档构建器中
|
|
110
121
|
if (tags.length > 0) {
|
|
111
122
|
for (const t of tags) {
|
|
112
123
|
// 这里 metadata => string[]
|
|
113
124
|
strTags = strTags.concat(t.metadata);
|
|
114
|
-
|
|
125
|
+
controllerTags.push(t.metadata);
|
|
126
|
+
// this.documentBuilder.addTag(t.metadata);
|
|
115
127
|
}
|
|
116
128
|
}
|
|
117
129
|
else {
|
|
130
|
+
// 如果不存在标签,则根据控制器选项生成标签
|
|
118
131
|
const tag = { name: '', description: '' };
|
|
119
132
|
if (prefix !== '/') {
|
|
120
133
|
tag.name =
|
|
@@ -128,53 +141,72 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
128
141
|
tag.description =
|
|
129
142
|
(controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
|
|
130
143
|
}
|
|
144
|
+
// 如果标签名存在,则将其添加到文档构建器中
|
|
131
145
|
if (tag.name) {
|
|
132
146
|
strTags.push(tag.name);
|
|
133
|
-
|
|
147
|
+
controllerTags.push([tag.name, tag.description]);
|
|
148
|
+
// this.documentBuilder.addTag(tag.name, tag.description);
|
|
134
149
|
}
|
|
135
150
|
}
|
|
136
|
-
//
|
|
137
|
-
// get router info
|
|
151
|
+
// 获取路由信息
|
|
138
152
|
const webRouterInfo = (0, core_1.getClassMetadata)(core_1.WEB_ROUTER_KEY, target);
|
|
139
|
-
|
|
153
|
+
// 过滤出头部信息
|
|
154
|
+
let headers = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
|
|
140
155
|
if (headers.length > 0) {
|
|
141
156
|
headers = headers.map(item => item.metadata);
|
|
142
157
|
}
|
|
143
|
-
|
|
158
|
+
// 过滤出安全信息
|
|
159
|
+
const security = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
|
|
160
|
+
// 初始化路径对象
|
|
144
161
|
const paths = {};
|
|
162
|
+
// 如果存在路由信息,则遍历生成路径
|
|
145
163
|
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
|
|
146
164
|
for (const webRouter of webRouterInfo) {
|
|
165
|
+
// 生成URL
|
|
147
166
|
let url = (prefix + webRouter.path).replace('//', '/');
|
|
148
167
|
url = replaceUrl(url, parseParamsInPath(url));
|
|
149
168
|
// 判断是否忽略当前路由
|
|
150
|
-
const endpoints =
|
|
169
|
+
const endpoints = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
|
|
151
170
|
item.propertyName === webRouter.method);
|
|
171
|
+
// 如果存在需要忽略的路由,则跳过当前循环
|
|
152
172
|
if (endpoints[0]) {
|
|
153
173
|
continue;
|
|
154
174
|
}
|
|
175
|
+
// 判断是否需要过滤当前路由
|
|
176
|
+
if (this.swaggerConfig.routerFilter) {
|
|
177
|
+
const isFilter = this.swaggerConfig.routerFilter(url, webRouter);
|
|
178
|
+
if (isFilter) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// 获取路由参数
|
|
155
183
|
const routerArgs = metaForParams[webRouter.method] || [];
|
|
184
|
+
// 过滤出主体参数
|
|
156
185
|
const bds = routerArgs.filter(item => {
|
|
157
186
|
var _a;
|
|
158
187
|
return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
|
|
159
188
|
((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) === core_1.RouteParamTypes.BODY;
|
|
160
189
|
});
|
|
190
|
+
// 如果存在多个主体参数,则跳过当前循环,因为swagger不支持多个@Body
|
|
161
191
|
if (bds.length > 1) {
|
|
162
|
-
// swagger not support more than one @Body
|
|
163
192
|
continue;
|
|
164
193
|
}
|
|
165
|
-
|
|
166
|
-
|
|
194
|
+
// 生成路由方法
|
|
195
|
+
this.generateRouteMethod(url, webRouter, paths, metaForClass, routerArgs, headers, target);
|
|
196
|
+
// 如果当前路径的标签长度为0,则赋值标签
|
|
167
197
|
if (paths[url][webRouter.requestMethod].tags.length === 0) {
|
|
168
198
|
paths[url][webRouter.requestMethod].tags = strTags;
|
|
169
199
|
}
|
|
170
|
-
//
|
|
171
|
-
const exts =
|
|
200
|
+
// 过滤出扩展信息
|
|
201
|
+
const exts = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXTENSION &&
|
|
172
202
|
item.propertyName === webRouter.method);
|
|
203
|
+
// 如果存在扩展信息,则将其添加到路径中
|
|
173
204
|
for (const e of exts) {
|
|
174
205
|
if (e.metadata) {
|
|
175
206
|
Object.assign(paths[url][webRouter.requestMethod], e.metadata);
|
|
176
207
|
}
|
|
177
208
|
}
|
|
209
|
+
// 如果存在安全信息,则将其添加到路径中
|
|
178
210
|
if (security.length > 0) {
|
|
179
211
|
if (!paths[url][webRouter.requestMethod].security) {
|
|
180
212
|
paths[url][webRouter.requestMethod].security = [];
|
|
@@ -188,7 +220,19 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
188
220
|
}
|
|
189
221
|
}
|
|
190
222
|
}
|
|
223
|
+
// 将路径添加到文档构建器中
|
|
191
224
|
this.documentBuilder.addPaths(paths);
|
|
225
|
+
// 将控制器标签添加到文档构建器中
|
|
226
|
+
if (Object.keys(paths).length > 0) {
|
|
227
|
+
controllerTags.forEach(tag => {
|
|
228
|
+
if (Array.isArray(tag)) {
|
|
229
|
+
this.documentBuilder.addTag(tag[0], tag[1]);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
this.documentBuilder.addTag(tag);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
192
236
|
}
|
|
193
237
|
/**
|
|
194
238
|
* 构造 router 提取方法
|
|
@@ -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.
|
|
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.
|
|
3
|
+
"version": "3.14.9-beta.2",
|
|
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.
|