@midwayjs/swagger 1.3.0 → 3.0.0-beta.9

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.
Files changed (72) hide show
  1. package/README.md +7 -23
  2. package/dist/common/enum.utils.d.ts +9 -0
  3. package/dist/common/enum.utils.js +63 -0
  4. package/dist/common/httpStatus.d.ts +51 -0
  5. package/dist/common/httpStatus.js +55 -0
  6. package/dist/config/config.default.d.ts +2 -8
  7. package/dist/config/config.default.js +6 -6
  8. package/dist/configuration.d.ts +4 -2
  9. package/dist/configuration.js +27 -11
  10. package/dist/constants.d.ts +15 -0
  11. package/dist/constants.js +18 -0
  12. package/dist/decorators/api-basic.decorator.d.ts +2 -0
  13. package/dist/decorators/api-basic.decorator.js +9 -0
  14. package/dist/decorators/api-bearer.decorator.d.ts +2 -0
  15. package/dist/decorators/api-bearer.decorator.js +9 -0
  16. package/dist/decorators/api-body.decorator.d.ts +16 -0
  17. package/dist/decorators/api-body.decorator.js +27 -0
  18. package/dist/decorators/api-cookie.decorator.d.ts +2 -0
  19. package/dist/decorators/api-cookie.decorator.js +9 -0
  20. package/dist/decorators/api-exclude-controller.decorator.d.ts +2 -0
  21. package/dist/decorators/api-exclude-controller.decorator.js +12 -0
  22. package/dist/decorators/api-exclude-endpoint.decorator.d.ts +2 -0
  23. package/dist/decorators/api-exclude-endpoint.decorator.js +12 -0
  24. package/dist/decorators/api-extension.decorator.d.ts +2 -0
  25. package/dist/decorators/api-extension.decorator.js +16 -0
  26. package/dist/decorators/api-header.decorator.d.ts +7 -0
  27. package/dist/decorators/api-header.decorator.js +43 -0
  28. package/dist/decorators/api-oauth2.decorator.d.ts +2 -0
  29. package/dist/decorators/api-oauth2.decorator.js +9 -0
  30. package/dist/decorators/api-operation.decorator.d.ts +4 -0
  31. package/dist/decorators/api-operation.decorator.js +16 -0
  32. package/dist/decorators/api-param.decorator.d.ts +15 -0
  33. package/dist/decorators/api-param.decorator.js +30 -0
  34. package/dist/decorators/api-property.decorator.d.ts +11 -0
  35. package/dist/decorators/api-property.decorator.js +56 -0
  36. package/dist/decorators/api-query.decorator.d.ts +17 -0
  37. package/dist/decorators/api-query.decorator.js +31 -0
  38. package/dist/decorators/api-response.decorator.d.ts +41 -0
  39. package/dist/decorators/api-response.decorator.js +149 -0
  40. package/dist/decorators/api-security.decorator.d.ts +3 -0
  41. package/dist/decorators/api-security.decorator.js +17 -0
  42. package/dist/decorators/api-tags.decorator.d.ts +2 -0
  43. package/dist/decorators/api-tags.decorator.js +10 -0
  44. package/dist/decorators/helpers.d.ts +7 -0
  45. package/dist/decorators/helpers.js +41 -0
  46. package/dist/decorators/index.d.ts +18 -0
  47. package/dist/decorators/index.js +35 -0
  48. package/dist/documentBuilder.d.ts +25 -0
  49. package/dist/documentBuilder.js +192 -0
  50. package/dist/index.d.ts +5 -5
  51. package/dist/index.js +8 -6
  52. package/dist/interfaces/index.d.ts +268 -0
  53. package/dist/interfaces/index.js +7 -0
  54. package/dist/swaggerExplorer.d.ts +30 -0
  55. package/dist/swaggerExplorer.js +567 -0
  56. package/dist/swaggerMiddleware.d.ts +9 -0
  57. package/dist/swaggerMiddleware.js +123 -0
  58. package/index.d.ts +8 -0
  59. package/package.json +13 -12
  60. package/CHANGELOG.md +0 -121
  61. package/dist/controller/swagger.d.ts +0 -31
  62. package/dist/controller/swagger.js +0 -96
  63. package/dist/interface.d.ts +0 -16
  64. package/dist/interface.js +0 -3
  65. package/dist/lib/createAPI.d.ts +0 -52
  66. package/dist/lib/createAPI.js +0 -276
  67. package/dist/lib/document.d.ts +0 -117
  68. package/dist/lib/document.js +0 -131
  69. package/dist/lib/generator.d.ts +0 -31
  70. package/dist/lib/generator.js +0 -310
  71. package/dist/service/generator.d.ts +0 -24
  72. package/dist/service/generator.js +0 -37
@@ -0,0 +1,567 @@
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.externalDoc) &&
40
+ typeof ((_r = this.swaggerConfig) === null || _r === void 0 ? void 0 : _r.externalDoc) === 'object') {
41
+ this.documentBuilder.setExternalDoc((_t = (_s = this.swaggerConfig) === null || _s === void 0 ? void 0 : _s.externalDoc) === null || _t === void 0 ? void 0 : _t.description, (_v = (_u = this.swaggerConfig) === null || _u === void 0 ? void 0 : _u.externalDoc) === 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
+ const requestBody = {
213
+ required: true,
214
+ description: p.description || p.name,
215
+ content: p.content || {
216
+ 'application/json': {
217
+ schema: p.schema,
218
+ },
219
+ },
220
+ };
221
+ opts[webRouter.requestMethod].requestBody = requestBody;
222
+ delete p.content;
223
+ // in body 不需要处理
224
+ continue;
225
+ }
226
+ parameters.push(p);
227
+ }
228
+ // class header 需要使用 ApiHeader 装饰器
229
+ if (header) {
230
+ parameters.unshift(header);
231
+ }
232
+ opts[webRouter.requestMethod].parameters = parameters;
233
+ opts[webRouter.requestMethod].responses = {};
234
+ const responses = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_RESPONSE &&
235
+ item.propertyName === webRouter.method);
236
+ for (const r of responses) {
237
+ const resp = r.metadata;
238
+ const keys = Object.keys(resp);
239
+ for (const k of keys) {
240
+ // 这里是引用,赋值可以直接更改
241
+ const tt = resp[k];
242
+ if (tt.type) {
243
+ if ((0, decorator_1.isClass)(tt.type)) {
244
+ this.parseClzz(tt.type);
245
+ if (tt.isArray) {
246
+ tt.content = {
247
+ 'application/json': {
248
+ schema: {
249
+ type: 'array',
250
+ items: {
251
+ $ref: '#/components/schemas/' + tt.type.name,
252
+ },
253
+ },
254
+ },
255
+ };
256
+ }
257
+ else {
258
+ tt.content = {
259
+ 'application/json': {
260
+ schema: {
261
+ $ref: '#/components/schemas/' + tt.type.name,
262
+ },
263
+ },
264
+ };
265
+ }
266
+ }
267
+ else {
268
+ tt.content = {
269
+ 'text/plan': {
270
+ schema: {
271
+ type: convertSchemaType(tt.type),
272
+ },
273
+ },
274
+ };
275
+ }
276
+ }
277
+ delete tt.status;
278
+ delete tt.type;
279
+ delete tt.isArray;
280
+ delete tt.format;
281
+ }
282
+ Object.assign(opts[webRouter.requestMethod].responses, resp);
283
+ }
284
+ paths[url] = opts;
285
+ }
286
+ /**
287
+ * 提取参数
288
+ * @param params
289
+ * @param p
290
+ */
291
+ parseFromParamsToP(paramMeta, p) {
292
+ var _a, _b, _c, _d;
293
+ if (paramMeta) {
294
+ const param = paramMeta.metadata;
295
+ if (param) {
296
+ p.description = param.description;
297
+ if (param.in === 'query') {
298
+ p.allowEmptyValue = param.allowEmptyValue || false;
299
+ }
300
+ if (param.example) {
301
+ p.example = param.example;
302
+ }
303
+ if (param.examples) {
304
+ p.examples = param.examples;
305
+ }
306
+ if (param.deprecated) {
307
+ p.deprecated = param.deprecated;
308
+ }
309
+ p.in = (_a = param === null || param === void 0 ? void 0 : param.in) !== null && _a !== void 0 ? _a : p.in;
310
+ p.required = (_b = param === null || param === void 0 ? void 0 : param.required) !== null && _b !== void 0 ? _b : p.required;
311
+ if (p.in === 'query') {
312
+ p.style = 'form';
313
+ }
314
+ else if (p.in === 'path' || p.in === 'header') {
315
+ p.style = 'simple';
316
+ }
317
+ else if (p.in === 'cookie') {
318
+ p.style = 'form';
319
+ }
320
+ p.explode = p.style === 'form';
321
+ // response content
322
+ if (param === null || param === void 0 ? void 0 : param.content) {
323
+ p.content = param === null || param === void 0 ? void 0 : param.content;
324
+ }
325
+ if (param.schema) {
326
+ p.schema = param.schema;
327
+ }
328
+ else {
329
+ if (param.type) {
330
+ if ((0, decorator_1.isClass)(param.type)) {
331
+ this.parseClzz(param.type);
332
+ p.schema = {
333
+ $ref: '#/components/schemas/' + param.type.name,
334
+ };
335
+ }
336
+ if (param.isArray) {
337
+ p.schema = {
338
+ type: 'array',
339
+ items: {
340
+ 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),
341
+ format: param.format,
342
+ },
343
+ };
344
+ }
345
+ else {
346
+ if (!p.schema) {
347
+ p.schema = {
348
+ type: param.type ? convertSchemaType(param.type) : p.type,
349
+ format: param.format || p.format,
350
+ };
351
+ }
352
+ }
353
+ }
354
+ else if (param.format) {
355
+ p.schema.format = param.format;
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
361
+ /**
362
+ * 解析类型的 ApiProperty
363
+ * @param clzz
364
+ */
365
+ parseClzz(clzz) {
366
+ const props = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_PROPERTY, clzz);
367
+ if (props) {
368
+ const tt = {
369
+ type: 'object',
370
+ properties: {},
371
+ required: [],
372
+ example: {},
373
+ };
374
+ Object.keys(props).forEach(key => {
375
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
376
+ if ((_a = props[key].metadata) === null || _a === void 0 ? void 0 : _a.example) {
377
+ tt.example[key] = (_b = props[key].metadata) === null || _b === void 0 ? void 0 : _b.example;
378
+ }
379
+ if (((_c = props[key].metadata) === null || _c === void 0 ? void 0 : _c.required) !== false) {
380
+ tt.required.push(key);
381
+ }
382
+ if ((_d = props[key].metadata) === null || _d === void 0 ? void 0 : _d.enum) {
383
+ tt.properties[key] = {
384
+ type: (_e = props[key].metadata) === null || _e === void 0 ? void 0 : _e.type,
385
+ enum: (_f = props[key].metadata) === null || _f === void 0 ? void 0 : _f.enum,
386
+ default: (_g = props[key].metadata) === null || _g === void 0 ? void 0 : _g.default,
387
+ };
388
+ return;
389
+ }
390
+ if ((_j = (_h = props[key].metadata) === null || _h === void 0 ? void 0 : _h.items) === null || _j === void 0 ? void 0 : _j.enum) {
391
+ tt.properties[key] = {
392
+ type: (_k = props[key].metadata) === null || _k === void 0 ? void 0 : _k.type,
393
+ items: (_l = props[key].metadata) === null || _l === void 0 ? void 0 : _l.items,
394
+ default: (_m = props[key].metadata) === null || _m === void 0 ? void 0 : _m.default,
395
+ };
396
+ return;
397
+ }
398
+ let currentType = (_o = props[key].metadata) === null || _o === void 0 ? void 0 : _o.type;
399
+ if (currentType === 'array') {
400
+ currentType = (_q = (_p = props[key].metadata) === null || _p === void 0 ? void 0 : _p.items) === null || _q === void 0 ? void 0 : _q.type;
401
+ }
402
+ if ((0, decorator_1.isClass)(currentType)) {
403
+ this.parseClzz(currentType);
404
+ if (((_r = props[key].metadata) === null || _r === void 0 ? void 0 : _r.type) === 'array') {
405
+ tt.properties[key] = {
406
+ type: 'array',
407
+ items: {
408
+ $ref: '#components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
409
+ },
410
+ };
411
+ }
412
+ else {
413
+ tt.properties[key] = {
414
+ $ref: '#components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
415
+ };
416
+ }
417
+ }
418
+ else {
419
+ if (((_s = props[key].metadata) === null || _s === void 0 ? void 0 : _s.type) === 'array') {
420
+ tt.properties[key] = {
421
+ type: 'array',
422
+ items: {
423
+ type: (0, decorator_1.getPropertyType)(clzz.prototype, key).name,
424
+ format: (_t = props[key].metadata) === null || _t === void 0 ? void 0 : _t.format,
425
+ },
426
+ };
427
+ }
428
+ else {
429
+ tt.properties[key] = {
430
+ type: (0, decorator_1.getPropertyType)(clzz.prototype, key).name,
431
+ format: (_u = props[key].metadata) === null || _u === void 0 ? void 0 : _u.format,
432
+ };
433
+ }
434
+ }
435
+ });
436
+ this.documentBuilder.addSchema({
437
+ [clzz.name]: tt,
438
+ });
439
+ }
440
+ }
441
+ /**
442
+ * 授权验证
443
+ * @param opts
444
+ * @returns
445
+ */
446
+ setAuth(opts) {
447
+ if (!opts) {
448
+ return;
449
+ }
450
+ const authType = opts.authType;
451
+ delete opts.authType;
452
+ // TODO 加 security
453
+ switch (authType) {
454
+ case 'basic':
455
+ {
456
+ const name = opts.name;
457
+ delete opts.name;
458
+ this.documentBuilder.addBasicAuth(opts, name);
459
+ }
460
+ break;
461
+ case 'bearer':
462
+ {
463
+ const name = opts.name;
464
+ delete opts.name;
465
+ this.documentBuilder.addBearerAuth(opts, name);
466
+ }
467
+ break;
468
+ case 'cookie':
469
+ {
470
+ const cname = opts.cookieName;
471
+ const secName = opts.securityName;
472
+ delete opts.cookieName;
473
+ delete opts.securityName;
474
+ this.documentBuilder.addCookieAuth(cname, opts, secName);
475
+ }
476
+ break;
477
+ case 'oauth2':
478
+ {
479
+ const name = opts.name;
480
+ delete opts.name;
481
+ this.documentBuilder.addOAuth2(opts, name);
482
+ }
483
+ break;
484
+ case 'apikey':
485
+ {
486
+ const name = opts.name;
487
+ delete opts.name;
488
+ this.documentBuilder.addApiKey(opts, name);
489
+ }
490
+ break;
491
+ case 'custom':
492
+ {
493
+ this.documentBuilder.addSecurity(opts === null || opts === void 0 ? void 0 : opts.name, opts);
494
+ }
495
+ break;
496
+ }
497
+ }
498
+ };
499
+ __decorate([
500
+ (0, decorator_1.Config)('swagger'),
501
+ __metadata("design:type", Object)
502
+ ], SwaggerExplorer.prototype, "swaggerConfig", void 0);
503
+ __decorate([
504
+ (0, decorator_1.Init)(),
505
+ __metadata("design:type", Function),
506
+ __metadata("design:paramtypes", []),
507
+ __metadata("design:returntype", Promise)
508
+ ], SwaggerExplorer.prototype, "init", null);
509
+ SwaggerExplorer = __decorate([
510
+ (0, decorator_1.Provide)(),
511
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
512
+ ], SwaggerExplorer);
513
+ exports.SwaggerExplorer = SwaggerExplorer;
514
+ /**
515
+ * 解释路由上的参数
516
+ * @param url
517
+ */
518
+ function parseParamsInPath(url) {
519
+ const names = [];
520
+ url.split('/').forEach(item => {
521
+ if (item.startsWith(':')) {
522
+ const paramName = item.substr(1);
523
+ names.push(paramName);
524
+ }
525
+ });
526
+ return names;
527
+ }
528
+ /**
529
+ * 替换成 openapi 的url
530
+ * @param url
531
+ * @param names
532
+ */
533
+ function replaceUrl(url, names) {
534
+ names.forEach(n => {
535
+ url = url.replace(`:${n}`, `{${n}}`);
536
+ });
537
+ return url;
538
+ }
539
+ function convertTypeToString(type) {
540
+ switch (type) {
541
+ case decorator_1.RouteParamTypes.HEADERS:
542
+ return 'header';
543
+ case decorator_1.RouteParamTypes.QUERY:
544
+ return 'query';
545
+ case decorator_1.RouteParamTypes.PARAM:
546
+ return 'path';
547
+ case decorator_1.RouteParamTypes.BODY:
548
+ return 'body';
549
+ default:
550
+ return 'header';
551
+ }
552
+ }
553
+ function convertSchemaType(value) {
554
+ switch (value) {
555
+ case 'Object':
556
+ return 'object';
557
+ case 'Boolean':
558
+ return 'boolean';
559
+ case 'Number':
560
+ return 'number';
561
+ case 'String':
562
+ return 'string';
563
+ default:
564
+ return 'object';
565
+ }
566
+ }
567
+ //# sourceMappingURL=swaggerExplorer.js.map
@@ -0,0 +1,9 @@
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
+ }
9
+ //# sourceMappingURL=swaggerMiddleware.d.ts.map