@midwayjs/swagger 1.2.0 → 3.0.0-beta.12
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.
- package/CHANGELOG.md +9 -75
- package/LICENSE +21 -0
- package/README.md +8 -22
- package/dist/common/enum.utils.d.ts +9 -0
- package/dist/common/enum.utils.js +63 -0
- package/dist/common/httpStatus.d.ts +51 -0
- package/dist/common/httpStatus.js +55 -0
- package/dist/config/config.default.d.ts +2 -8
- package/dist/config/config.default.js +6 -6
- package/dist/configuration.d.ts +4 -2
- package/dist/configuration.js +27 -11
- package/dist/constants.d.ts +15 -0
- package/dist/constants.js +18 -0
- package/dist/decorators/api-basic.decorator.d.ts +2 -0
- package/dist/decorators/api-basic.decorator.js +9 -0
- package/dist/decorators/api-bearer.decorator.d.ts +2 -0
- package/dist/decorators/api-bearer.decorator.js +9 -0
- package/dist/decorators/api-body.decorator.d.ts +24 -0
- package/dist/decorators/api-body.decorator.js +35 -0
- package/dist/decorators/api-cookie.decorator.d.ts +2 -0
- package/dist/decorators/api-cookie.decorator.js +9 -0
- package/dist/decorators/api-exclude-controller.decorator.d.ts +2 -0
- package/dist/decorators/api-exclude-controller.decorator.js +12 -0
- package/dist/decorators/api-exclude-endpoint.decorator.d.ts +2 -0
- package/dist/decorators/api-exclude-endpoint.decorator.js +12 -0
- package/dist/decorators/api-extension.decorator.d.ts +2 -0
- package/dist/decorators/api-extension.decorator.js +16 -0
- package/dist/decorators/api-header.decorator.d.ts +7 -0
- package/dist/decorators/api-header.decorator.js +43 -0
- package/dist/decorators/api-oauth2.decorator.d.ts +2 -0
- package/dist/decorators/api-oauth2.decorator.js +9 -0
- package/dist/decorators/api-operation.decorator.d.ts +4 -0
- package/dist/decorators/api-operation.decorator.js +16 -0
- package/dist/decorators/api-param.decorator.d.ts +15 -0
- package/dist/decorators/api-param.decorator.js +30 -0
- package/dist/decorators/api-property.decorator.d.ts +11 -0
- package/dist/decorators/api-property.decorator.js +55 -0
- package/dist/decorators/api-query.decorator.d.ts +17 -0
- package/dist/decorators/api-query.decorator.js +31 -0
- package/dist/decorators/api-response.decorator.d.ts +41 -0
- package/dist/decorators/api-response.decorator.js +149 -0
- package/dist/decorators/api-security.decorator.d.ts +3 -0
- package/dist/decorators/api-security.decorator.js +17 -0
- package/dist/decorators/api-tags.decorator.d.ts +2 -0
- package/dist/decorators/api-tags.decorator.js +10 -0
- package/dist/decorators/helpers.d.ts +7 -0
- package/dist/decorators/helpers.js +41 -0
- package/dist/decorators/index.d.ts +18 -0
- package/dist/decorators/index.js +35 -0
- package/dist/documentBuilder.d.ts +25 -0
- package/dist/documentBuilder.js +192 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +8 -6
- package/dist/interfaces/index.d.ts +339 -0
- package/dist/interfaces/index.js +7 -0
- package/dist/swaggerExplorer.d.ts +30 -0
- package/dist/swaggerExplorer.js +601 -0
- package/dist/swaggerMiddleware.d.ts +10 -0
- package/dist/swaggerMiddleware.js +126 -0
- package/index.d.ts +8 -0
- package/package.json +12 -10
- package/dist/controller/swagger.d.ts +0 -31
- package/dist/controller/swagger.js +0 -96
- package/dist/interface.d.ts +0 -16
- package/dist/interface.js +0 -3
- package/dist/lib/createAPI.d.ts +0 -52
- package/dist/lib/createAPI.js +0 -276
- package/dist/lib/document.d.ts +0 -117
- package/dist/lib/document.js +0 -131
- package/dist/lib/generator.d.ts +0 -31
- package/dist/lib/generator.js +0 -310
- package/dist/service/generator.d.ts +0 -24
- package/dist/service/generator.js +0 -37
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SwaggerExplorer = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const constants_1 = require("./constants");
|
|
15
|
+
const documentBuilder_1 = require("./documentBuilder");
|
|
16
|
+
let SwaggerExplorer = class SwaggerExplorer {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.documentBuilder = new documentBuilder_1.DocumentBuilder();
|
|
19
|
+
}
|
|
20
|
+
async init() {
|
|
21
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4;
|
|
22
|
+
this.documentBuilder.setTitle(this.swaggerConfig.title);
|
|
23
|
+
this.documentBuilder.setVersion(this.swaggerConfig.version);
|
|
24
|
+
this.documentBuilder.setDescription(this.swaggerConfig.description);
|
|
25
|
+
if (((_a = this.swaggerConfig) === null || _a === void 0 ? void 0 : _a.contact) &&
|
|
26
|
+
typeof ((_b = this.swaggerConfig) === null || _b === void 0 ? void 0 : _b.contact) === 'object') {
|
|
27
|
+
this.documentBuilder.setContact((_d = (_c = this.swaggerConfig) === null || _c === void 0 ? void 0 : _c.contact) === null || _d === void 0 ? void 0 : _d.name, (_f = (_e = this.swaggerConfig) === null || _e === void 0 ? void 0 : _e.contact) === null || _f === void 0 ? void 0 : _f.url, (_h = (_g = this.swaggerConfig) === null || _g === void 0 ? void 0 : _g.contact) === null || _h === void 0 ? void 0 : _h.email);
|
|
28
|
+
}
|
|
29
|
+
if (((_j = this.swaggerConfig) === null || _j === void 0 ? void 0 : _j.license) &&
|
|
30
|
+
typeof ((_k = this.swaggerConfig) === null || _k === void 0 ? void 0 : _k.license) === 'object') {
|
|
31
|
+
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);
|
|
32
|
+
}
|
|
33
|
+
if (this.swaggerConfig.basePath) {
|
|
34
|
+
this.documentBuilder.setBasePath(this.swaggerConfig.basePath);
|
|
35
|
+
}
|
|
36
|
+
if (this.swaggerConfig.termsOfService) {
|
|
37
|
+
this.documentBuilder.setTermsOfService(this.swaggerConfig.termsOfService);
|
|
38
|
+
}
|
|
39
|
+
if (((_q = this.swaggerConfig) === null || _q === void 0 ? void 0 : _q.externalDocs) &&
|
|
40
|
+
typeof ((_r = this.swaggerConfig) === null || _r === void 0 ? void 0 : _r.externalDocs) === 'object') {
|
|
41
|
+
this.documentBuilder.setExternalDoc((_t = (_s = this.swaggerConfig) === null || _s === void 0 ? void 0 : _s.externalDocs) === null || _t === void 0 ? void 0 : _t.description, (_v = (_u = this.swaggerConfig) === null || _u === void 0 ? void 0 : _u.externalDocs) === null || _v === void 0 ? void 0 : _v.url);
|
|
42
|
+
}
|
|
43
|
+
if (((_w = this.swaggerConfig) === null || _w === void 0 ? void 0 : _w.servers) &&
|
|
44
|
+
Array.isArray((_x = this.swaggerConfig) === null || _x === void 0 ? void 0 : _x.servers)) {
|
|
45
|
+
for (const serv of (_y = this.swaggerConfig) === null || _y === void 0 ? void 0 : _y.servers) {
|
|
46
|
+
this.documentBuilder.addServer(serv === null || serv === void 0 ? void 0 : serv.url, serv === null || serv === void 0 ? void 0 : serv.description);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
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)) {
|
|
50
|
+
for (const t of (_1 = this.swaggerConfig) === null || _1 === void 0 ? void 0 : _1.tags) {
|
|
51
|
+
this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// 设置 auth 类型
|
|
55
|
+
if (Array.isArray((_2 = this.swaggerConfig) === null || _2 === void 0 ? void 0 : _2.auth)) {
|
|
56
|
+
for (const a of (_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.auth) {
|
|
57
|
+
this.setAuth(a);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
this.setAuth((_4 = this.swaggerConfig) === null || _4 === void 0 ? void 0 : _4.auth);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
scanApp() {
|
|
65
|
+
const routes = (0, decorator_1.listModule)(decorator_1.CONTROLLER_KEY);
|
|
66
|
+
for (const route of routes) {
|
|
67
|
+
this.generatePath(route);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
getData() {
|
|
71
|
+
return this.documentBuilder.build();
|
|
72
|
+
}
|
|
73
|
+
generatePath(target) {
|
|
74
|
+
const metaForMethods = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_METHOD, target) || [];
|
|
75
|
+
const exs = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_CONTROLLER);
|
|
76
|
+
if (exs[0]) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const metaForParams = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_PARAM, target) || [];
|
|
80
|
+
const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, target);
|
|
81
|
+
const prefix = controllerOption.prefix;
|
|
82
|
+
const tags = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_TAGS);
|
|
83
|
+
let strTags = [];
|
|
84
|
+
if (tags.length > 0) {
|
|
85
|
+
for (const t of tags) {
|
|
86
|
+
// 这里 metadata => string[]
|
|
87
|
+
strTags = strTags.concat(t.metadata);
|
|
88
|
+
this.documentBuilder.addTag(t.metadata);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
const tag = { name: '', description: '' };
|
|
93
|
+
if (prefix !== '/') {
|
|
94
|
+
tag.name =
|
|
95
|
+
(controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.tagName) ||
|
|
96
|
+
(/^\//.test(prefix) ? prefix.split('/')[1] : prefix);
|
|
97
|
+
tag.description =
|
|
98
|
+
(controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
tag.name = (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.tagName) || 'default';
|
|
102
|
+
tag.description =
|
|
103
|
+
(controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
|
|
104
|
+
}
|
|
105
|
+
if (tag.name) {
|
|
106
|
+
this.documentBuilder.addTag(tag.name, tag.description);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// const globalMiddleware = controllerOption.routerOptions.middleware;
|
|
110
|
+
// get router info
|
|
111
|
+
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.WEB_ROUTER_KEY, target);
|
|
112
|
+
let header = null;
|
|
113
|
+
const headers = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
|
|
114
|
+
if (headers.length > 0) {
|
|
115
|
+
header = headers[0].metadata;
|
|
116
|
+
}
|
|
117
|
+
const security = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
|
|
118
|
+
const paths = {};
|
|
119
|
+
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
|
|
120
|
+
for (const webRouter of webRouterInfo) {
|
|
121
|
+
let url = (prefix + webRouter.path).replace('//', '/');
|
|
122
|
+
url = replaceUrl(url, parseParamsInPath(url));
|
|
123
|
+
// 判断是否忽略当前路由
|
|
124
|
+
const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
|
|
125
|
+
item.propertyName === webRouter.method);
|
|
126
|
+
if (endpoints[0]) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
this.generateRouteMethod(url, webRouter, paths, metaForMethods, metaForParams, header, target);
|
|
130
|
+
// 这里赋值 tags
|
|
131
|
+
if (paths[url][webRouter.requestMethod].tags.length === 0) {
|
|
132
|
+
paths[url][webRouter.requestMethod].tags = strTags;
|
|
133
|
+
}
|
|
134
|
+
// extension => prefix 为 x-
|
|
135
|
+
const exts = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXTENSION &&
|
|
136
|
+
item.propertyName === webRouter.method);
|
|
137
|
+
for (const e of exts) {
|
|
138
|
+
if (e.metadata) {
|
|
139
|
+
Object.assign(paths[url][webRouter.requestMethod], e.metadata);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (security.length > 0) {
|
|
143
|
+
if (!paths[url][webRouter.requestMethod].security) {
|
|
144
|
+
paths[url][webRouter.requestMethod].security = [];
|
|
145
|
+
}
|
|
146
|
+
for (const s of security) {
|
|
147
|
+
if (!s.metadata) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
paths[url][webRouter.requestMethod].security.push(s.metadata);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
this.documentBuilder.addPaths(paths);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 构造 router 提取方法
|
|
159
|
+
*/
|
|
160
|
+
generateRouteMethod(url, webRouter, paths, metaForMethods, metaForParams, header, target) {
|
|
161
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
162
|
+
const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
|
|
163
|
+
item.propertyName === webRouter.method)[0];
|
|
164
|
+
let opts = paths[url];
|
|
165
|
+
if (!opts) {
|
|
166
|
+
opts = {};
|
|
167
|
+
}
|
|
168
|
+
const parameters = [];
|
|
169
|
+
opts[webRouter.requestMethod] = {
|
|
170
|
+
summary: (_a = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _a === void 0 ? void 0 : _a.summary,
|
|
171
|
+
description: (_b = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _b === void 0 ? void 0 : _b.description,
|
|
172
|
+
operationId: ((_c = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _c === void 0 ? void 0 : _c.operationId) || webRouter.method,
|
|
173
|
+
tags: ((_d = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _d === void 0 ? void 0 : _d.tags) || [],
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* [{"key":"web:router_param","parameterIndex":1,"propertyName":"create","metadata":{"type":2}},
|
|
177
|
+
* {"key":"web:router_param","parameterIndex":0,"propertyName":"create","metadata":{"type":1,"propertyData":"createCatDto"}}]
|
|
178
|
+
*/
|
|
179
|
+
const routerArgs = metaForParams[webRouter.method] || [];
|
|
180
|
+
// WEB_ROUTER_PARAM_KEY
|
|
181
|
+
let args = routerArgs.filter(item => item.key === decorator_1.WEB_ROUTER_PARAM_KEY);
|
|
182
|
+
args = args.filter(item => item.key === decorator_1.WEB_ROUTER_PARAM_KEY);
|
|
183
|
+
const types = (0, decorator_1.getMethodParamTypes)(target, webRouter.method);
|
|
184
|
+
const params = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_PARAMETERS &&
|
|
185
|
+
item.propertyName === webRouter.method);
|
|
186
|
+
for (const arg of args) {
|
|
187
|
+
const currentType = types[arg.parameterIndex];
|
|
188
|
+
const p = {
|
|
189
|
+
name: (_f = (_e = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _e === void 0 ? void 0 : _e.propertyData) !== null && _f !== void 0 ? _f : '',
|
|
190
|
+
in: convertTypeToString((_g = arg.metadata) === null || _g === void 0 ? void 0 : _g.type),
|
|
191
|
+
required: false,
|
|
192
|
+
};
|
|
193
|
+
if (p.in === 'path') {
|
|
194
|
+
p.required = true;
|
|
195
|
+
if (url.indexOf('{' + p.name + '}') === -1) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if ((0, decorator_1.isClass)(currentType)) {
|
|
200
|
+
this.parseClzz(currentType);
|
|
201
|
+
p.schema = {
|
|
202
|
+
$ref: '#/components/schemas/' + currentType.name,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
p.schema = {
|
|
207
|
+
type: convertSchemaType((_h = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _h !== void 0 ? _h : currentType),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
this.parseFromParamsToP(params[params.length - 1 - arg.parameterIndex], p);
|
|
211
|
+
if (p.in === 'body') {
|
|
212
|
+
if (!p.content) {
|
|
213
|
+
p.content = {};
|
|
214
|
+
p.content[p.contentType || 'application/json'] = {
|
|
215
|
+
schema: p.schema,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const requestBody = {
|
|
219
|
+
required: true,
|
|
220
|
+
description: p.description || p.name,
|
|
221
|
+
content: p.content,
|
|
222
|
+
};
|
|
223
|
+
opts[webRouter.requestMethod].requestBody = requestBody;
|
|
224
|
+
delete p.contentType;
|
|
225
|
+
delete p.content;
|
|
226
|
+
// in body 不需要处理
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
parameters.push(p);
|
|
230
|
+
}
|
|
231
|
+
// class header 需要使用 ApiHeader 装饰器
|
|
232
|
+
if (header) {
|
|
233
|
+
parameters.unshift(header);
|
|
234
|
+
}
|
|
235
|
+
opts[webRouter.requestMethod].parameters = parameters;
|
|
236
|
+
const responses = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_RESPONSE &&
|
|
237
|
+
item.propertyName === webRouter.method);
|
|
238
|
+
const returnResponses = {};
|
|
239
|
+
for (const r of responses) {
|
|
240
|
+
const resp = r.metadata;
|
|
241
|
+
const keys = Object.keys(resp);
|
|
242
|
+
for (const k of keys) {
|
|
243
|
+
// 这里是引用,赋值可以直接更改
|
|
244
|
+
const tt = resp[k];
|
|
245
|
+
if (tt.type) {
|
|
246
|
+
if ((0, decorator_1.isClass)(tt.type)) {
|
|
247
|
+
this.parseClzz(tt.type);
|
|
248
|
+
if (tt.isArray) {
|
|
249
|
+
tt.content = {
|
|
250
|
+
'application/json': {
|
|
251
|
+
schema: {
|
|
252
|
+
type: 'array',
|
|
253
|
+
items: {
|
|
254
|
+
$ref: '#/components/schemas/' + tt.type.name,
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
tt.content = {
|
|
262
|
+
'application/json': {
|
|
263
|
+
schema: {
|
|
264
|
+
$ref: '#/components/schemas/' + tt.type.name,
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
tt.content = {
|
|
272
|
+
'text/plan': {
|
|
273
|
+
schema: {
|
|
274
|
+
type: convertSchemaType(tt.type),
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
delete tt.status;
|
|
281
|
+
delete tt.type;
|
|
282
|
+
delete tt.isArray;
|
|
283
|
+
delete tt.format;
|
|
284
|
+
}
|
|
285
|
+
Object.assign(returnResponses, resp);
|
|
286
|
+
}
|
|
287
|
+
if (Object.keys(returnResponses).length > 0) {
|
|
288
|
+
opts[webRouter.requestMethod].responses = returnResponses;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
opts[webRouter.requestMethod].responses = {
|
|
292
|
+
200: {
|
|
293
|
+
description: 'OK',
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
paths[url] = opts;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* 提取参数
|
|
301
|
+
* @param params
|
|
302
|
+
* @param p
|
|
303
|
+
*/
|
|
304
|
+
parseFromParamsToP(paramMeta, p) {
|
|
305
|
+
var _a, _b, _c, _d;
|
|
306
|
+
if (paramMeta) {
|
|
307
|
+
const param = paramMeta.metadata;
|
|
308
|
+
if (param) {
|
|
309
|
+
p.description = param.description;
|
|
310
|
+
if (param.in === 'query') {
|
|
311
|
+
p.allowEmptyValue = param.allowEmptyValue || false;
|
|
312
|
+
}
|
|
313
|
+
if (param.example) {
|
|
314
|
+
p.example = param.example;
|
|
315
|
+
}
|
|
316
|
+
if (param.examples) {
|
|
317
|
+
p.examples = param.examples;
|
|
318
|
+
}
|
|
319
|
+
if (param.deprecated) {
|
|
320
|
+
p.deprecated = param.deprecated;
|
|
321
|
+
}
|
|
322
|
+
if (param.contentType) {
|
|
323
|
+
p.contentType = param.contentType;
|
|
324
|
+
}
|
|
325
|
+
p.in = (_a = param === null || param === void 0 ? void 0 : param.in) !== null && _a !== void 0 ? _a : p.in;
|
|
326
|
+
p.required = (_b = param === null || param === void 0 ? void 0 : param.required) !== null && _b !== void 0 ? _b : p.required;
|
|
327
|
+
if (p.in === 'query') {
|
|
328
|
+
p.style = 'form';
|
|
329
|
+
}
|
|
330
|
+
else if (p.in === 'path' || p.in === 'header') {
|
|
331
|
+
p.style = 'simple';
|
|
332
|
+
}
|
|
333
|
+
else if (p.in === 'cookie') {
|
|
334
|
+
p.style = 'form';
|
|
335
|
+
}
|
|
336
|
+
p.explode = p.style === 'form';
|
|
337
|
+
// response content
|
|
338
|
+
if (param === null || param === void 0 ? void 0 : param.content) {
|
|
339
|
+
p.content = param === null || param === void 0 ? void 0 : param.content;
|
|
340
|
+
}
|
|
341
|
+
if (param.schema) {
|
|
342
|
+
p.schema = param.schema;
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
if (param.type) {
|
|
346
|
+
if ((0, decorator_1.isClass)(param.type)) {
|
|
347
|
+
this.parseClzz(param.type);
|
|
348
|
+
p.schema = {
|
|
349
|
+
$ref: '#/components/schemas/' + param.type.name,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if (param.isArray) {
|
|
353
|
+
p.schema = {
|
|
354
|
+
type: 'array',
|
|
355
|
+
items: {
|
|
356
|
+
type: (_d = (_c = p === null || p === void 0 ? void 0 : p.schema) === null || _c === void 0 ? void 0 : _c.$ref) !== null && _d !== void 0 ? _d : convertSchemaType(param.type),
|
|
357
|
+
format: param.format,
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
if (!p.schema) {
|
|
363
|
+
p.schema = {
|
|
364
|
+
type: param.type ? convertSchemaType(param.type) : p.type,
|
|
365
|
+
format: param.format || p.format,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
else if (param.format) {
|
|
371
|
+
p.schema.format = param.format;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* 解析类型的 ApiProperty
|
|
379
|
+
* @param clzz
|
|
380
|
+
*/
|
|
381
|
+
parseClzz(clzz) {
|
|
382
|
+
const props = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_PROPERTY, clzz);
|
|
383
|
+
const tt = {
|
|
384
|
+
type: 'object',
|
|
385
|
+
properties: {},
|
|
386
|
+
};
|
|
387
|
+
if (props) {
|
|
388
|
+
Object.keys(props).forEach(key => {
|
|
389
|
+
var _a, _b;
|
|
390
|
+
const metadata = props[key].metadata;
|
|
391
|
+
if (metadata === null || metadata === void 0 ? void 0 : metadata.example) {
|
|
392
|
+
if (!tt.example) {
|
|
393
|
+
tt.example = {};
|
|
394
|
+
}
|
|
395
|
+
tt.example[key] = metadata === null || metadata === void 0 ? void 0 : metadata.example;
|
|
396
|
+
delete metadata.example;
|
|
397
|
+
}
|
|
398
|
+
if ((metadata === null || metadata === void 0 ? void 0 : metadata.required) !== false) {
|
|
399
|
+
if (!tt.required) {
|
|
400
|
+
tt.required = [];
|
|
401
|
+
}
|
|
402
|
+
tt.required.push(key);
|
|
403
|
+
delete metadata.required;
|
|
404
|
+
}
|
|
405
|
+
if (metadata === null || metadata === void 0 ? void 0 : metadata.enum) {
|
|
406
|
+
tt.properties[key] = {
|
|
407
|
+
type: metadata === null || metadata === void 0 ? void 0 : metadata.type,
|
|
408
|
+
enum: metadata === null || metadata === void 0 ? void 0 : metadata.enum,
|
|
409
|
+
default: metadata === null || metadata === void 0 ? void 0 : metadata.default,
|
|
410
|
+
};
|
|
411
|
+
if (metadata === null || metadata === void 0 ? void 0 : metadata.description) {
|
|
412
|
+
tt.properties[key].description = metadata === null || metadata === void 0 ? void 0 : metadata.description;
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if ((_a = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _a === void 0 ? void 0 : _a.enum) {
|
|
417
|
+
tt.properties[key] = {
|
|
418
|
+
type: metadata === null || metadata === void 0 ? void 0 : metadata.type,
|
|
419
|
+
items: metadata === null || metadata === void 0 ? void 0 : metadata.items,
|
|
420
|
+
default: metadata === null || metadata === void 0 ? void 0 : metadata.default,
|
|
421
|
+
};
|
|
422
|
+
if (metadata === null || metadata === void 0 ? void 0 : metadata.description) {
|
|
423
|
+
tt.properties[key].description = metadata === null || metadata === void 0 ? void 0 : metadata.description;
|
|
424
|
+
}
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
let isArray = false;
|
|
428
|
+
let currentType = metadata === null || metadata === void 0 ? void 0 : metadata.type;
|
|
429
|
+
metadata === null || metadata === void 0 ? true : delete metadata.type;
|
|
430
|
+
if (currentType === 'array') {
|
|
431
|
+
isArray = true;
|
|
432
|
+
currentType = (_b = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _b === void 0 ? void 0 : _b.type;
|
|
433
|
+
delete metadata.items;
|
|
434
|
+
}
|
|
435
|
+
if ((0, decorator_1.isClass)(currentType)) {
|
|
436
|
+
this.parseClzz(currentType);
|
|
437
|
+
if (isArray) {
|
|
438
|
+
tt.properties[key] = {
|
|
439
|
+
type: 'array',
|
|
440
|
+
items: {
|
|
441
|
+
$ref: '#components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
|
|
442
|
+
},
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
tt.properties[key] = {
|
|
447
|
+
$ref: '#components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
if (isArray) {
|
|
453
|
+
tt.properties[key] = {
|
|
454
|
+
type: 'array',
|
|
455
|
+
items: {
|
|
456
|
+
type: convertSchemaType((currentType === null || currentType === void 0 ? void 0 : currentType.name) || currentType),
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
tt.properties[key] = {
|
|
462
|
+
type: (0, decorator_1.getPropertyType)(clzz.prototype, key).name,
|
|
463
|
+
format: metadata === null || metadata === void 0 ? void 0 : metadata.format,
|
|
464
|
+
};
|
|
465
|
+
delete metadata.format;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
Object.assign(tt.properties[key], metadata);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
this.documentBuilder.addSchema({
|
|
472
|
+
[clzz.name]: tt,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* 授权验证
|
|
477
|
+
* @param opts
|
|
478
|
+
* @returns
|
|
479
|
+
*/
|
|
480
|
+
setAuth(opts) {
|
|
481
|
+
if (!opts) {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const authType = opts.authType;
|
|
485
|
+
delete opts.authType;
|
|
486
|
+
// TODO 加 security
|
|
487
|
+
switch (authType) {
|
|
488
|
+
case 'basic':
|
|
489
|
+
{
|
|
490
|
+
const name = opts.name;
|
|
491
|
+
delete opts.name;
|
|
492
|
+
this.documentBuilder.addBasicAuth(opts, name);
|
|
493
|
+
}
|
|
494
|
+
break;
|
|
495
|
+
case 'bearer':
|
|
496
|
+
{
|
|
497
|
+
const name = opts.name;
|
|
498
|
+
delete opts.name;
|
|
499
|
+
this.documentBuilder.addBearerAuth(opts, name);
|
|
500
|
+
}
|
|
501
|
+
break;
|
|
502
|
+
case 'cookie':
|
|
503
|
+
{
|
|
504
|
+
const cname = opts.cookieName;
|
|
505
|
+
const secName = opts.securityName;
|
|
506
|
+
delete opts.cookieName;
|
|
507
|
+
delete opts.securityName;
|
|
508
|
+
this.documentBuilder.addCookieAuth(cname, opts, secName);
|
|
509
|
+
}
|
|
510
|
+
break;
|
|
511
|
+
case 'oauth2':
|
|
512
|
+
{
|
|
513
|
+
const name = opts.name;
|
|
514
|
+
delete opts.name;
|
|
515
|
+
this.documentBuilder.addOAuth2(opts, name);
|
|
516
|
+
}
|
|
517
|
+
break;
|
|
518
|
+
case 'apikey':
|
|
519
|
+
{
|
|
520
|
+
const name = opts.name;
|
|
521
|
+
delete opts.name;
|
|
522
|
+
this.documentBuilder.addApiKey(opts, name);
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
525
|
+
case 'custom':
|
|
526
|
+
{
|
|
527
|
+
this.documentBuilder.addSecurity(opts === null || opts === void 0 ? void 0 : opts.name, opts);
|
|
528
|
+
}
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
__decorate([
|
|
534
|
+
(0, decorator_1.Config)('swagger'),
|
|
535
|
+
__metadata("design:type", Object)
|
|
536
|
+
], SwaggerExplorer.prototype, "swaggerConfig", void 0);
|
|
537
|
+
__decorate([
|
|
538
|
+
(0, decorator_1.Init)(),
|
|
539
|
+
__metadata("design:type", Function),
|
|
540
|
+
__metadata("design:paramtypes", []),
|
|
541
|
+
__metadata("design:returntype", Promise)
|
|
542
|
+
], SwaggerExplorer.prototype, "init", null);
|
|
543
|
+
SwaggerExplorer = __decorate([
|
|
544
|
+
(0, decorator_1.Provide)(),
|
|
545
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
546
|
+
], SwaggerExplorer);
|
|
547
|
+
exports.SwaggerExplorer = SwaggerExplorer;
|
|
548
|
+
/**
|
|
549
|
+
* 解释路由上的参数
|
|
550
|
+
* @param url
|
|
551
|
+
*/
|
|
552
|
+
function parseParamsInPath(url) {
|
|
553
|
+
const names = [];
|
|
554
|
+
url.split('/').forEach(item => {
|
|
555
|
+
if (item.startsWith(':')) {
|
|
556
|
+
const paramName = item.substr(1);
|
|
557
|
+
names.push(paramName);
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
return names;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* 替换成 openapi 的url
|
|
564
|
+
* @param url
|
|
565
|
+
* @param names
|
|
566
|
+
*/
|
|
567
|
+
function replaceUrl(url, names) {
|
|
568
|
+
names.forEach(n => {
|
|
569
|
+
url = url.replace(`:${n}`, `{${n}}`);
|
|
570
|
+
});
|
|
571
|
+
return url;
|
|
572
|
+
}
|
|
573
|
+
function convertTypeToString(type) {
|
|
574
|
+
switch (type) {
|
|
575
|
+
case decorator_1.RouteParamTypes.HEADERS:
|
|
576
|
+
return 'header';
|
|
577
|
+
case decorator_1.RouteParamTypes.QUERY:
|
|
578
|
+
return 'query';
|
|
579
|
+
case decorator_1.RouteParamTypes.PARAM:
|
|
580
|
+
return 'path';
|
|
581
|
+
case decorator_1.RouteParamTypes.BODY:
|
|
582
|
+
return 'body';
|
|
583
|
+
default:
|
|
584
|
+
return 'header';
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
function convertSchemaType(value) {
|
|
588
|
+
switch (value) {
|
|
589
|
+
case 'Object':
|
|
590
|
+
return 'object';
|
|
591
|
+
case 'Boolean':
|
|
592
|
+
return 'boolean';
|
|
593
|
+
case 'Number':
|
|
594
|
+
return 'number';
|
|
595
|
+
case 'String':
|
|
596
|
+
return 'string';
|
|
597
|
+
default:
|
|
598
|
+
return 'object';
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
//# sourceMappingURL=swaggerExplorer.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IMiddleware, IMidwayApplication, IMidwayContext, NextFunction } from '@midwayjs/core';
|
|
2
|
+
export declare class SwaggerMiddleware implements IMiddleware<IMidwayContext, NextFunction, unknown> {
|
|
3
|
+
private swaggerConfig;
|
|
4
|
+
private swaggerUiAssetPath;
|
|
5
|
+
private swaggerExplorer;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
resolve(app: IMidwayApplication): (req: any, res: any, next: NextFunction) => Promise<any>;
|
|
8
|
+
static getName(): string;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=swaggerMiddleware.d.ts.map
|