@midwayjs/swagger 3.0.0-beta.13 → 3.0.0-beta.17

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.
@@ -12,6 +12,7 @@ export declare class DocumentBuilder {
12
12
  setBasePath(path: string | string[]): this;
13
13
  addPaths(paths: Record<string, PathItemObject>): this;
14
14
  addSchema(schema: Record<string, SchemaObject>): this;
15
+ getSchema(name: string): SchemaObject;
15
16
  addTag(name: string, description?: string, externalDocs?: ExternalDocumentationObject): this;
16
17
  addSecurity(name: string, options: SecuritySchemeObject): this;
17
18
  addSecurityRequirements(name: string | SecurityRequirementObject, requirements?: string[]): this;
@@ -20,6 +21,7 @@ export declare class DocumentBuilder {
20
21
  addApiKey(options?: SecuritySchemeObject, name?: string): this;
21
22
  addBasicAuth(options?: SecuritySchemeObject, name?: string): this;
22
23
  addCookieAuth(cookieName?: string, options?: SecuritySchemeObject, securityName?: string): this;
24
+ sortTags(): void;
23
25
  build(): Omit<OpenAPIObject, 'paths'>;
24
26
  }
25
27
  //# sourceMappingURL=documentBuilder.d.ts.map
@@ -77,6 +77,13 @@ class DocumentBuilder {
77
77
  Object.assign(this.document.components.schemas, schema);
78
78
  return this;
79
79
  }
80
+ getSchema(name) {
81
+ var _a, _b;
82
+ if ((_a = this.document.components) === null || _a === void 0 ? void 0 : _a.schemas) {
83
+ return (_b = this.document.components) === null || _b === void 0 ? void 0 : _b.schemas[name];
84
+ }
85
+ return undefined;
86
+ }
80
87
  addTag(name, description = '', externalDocs) {
81
88
  if (Array.isArray(name)) {
82
89
  const arr = name;
@@ -184,6 +191,23 @@ class DocumentBuilder {
184
191
  });
185
192
  return this;
186
193
  }
194
+ sortTags() {
195
+ const tags = this.document.tags;
196
+ this.document.tags = tags.sort((a, b) => {
197
+ const s1 = a.name;
198
+ const s2 = b.name;
199
+ const len = s1.length > s2.length ? s2.length : s1.length;
200
+ for (let i = 0; i < len; i++) {
201
+ if (s1.charCodeAt(i) > s2.charCodeAt(i)) {
202
+ return 1;
203
+ }
204
+ else if (s1.charCodeAt(i) < s2.charCodeAt(i)) {
205
+ return -1;
206
+ }
207
+ }
208
+ return 0;
209
+ });
210
+ }
187
211
  build() {
188
212
  return this.document;
189
213
  }
@@ -335,5 +335,10 @@ export interface SwaggerOptions {
335
335
  * 访问 swagger ui 的路径
336
336
  */
337
337
  swaggerPath?: string;
338
+ /**
339
+ * 对路由 tag 进行 ascii 排序
340
+ * 可以使用 1-xxx、2-xxx、3-xxx 来定义 tag
341
+ */
342
+ tagSortable?: boolean;
338
343
  }
339
344
  //# sourceMappingURL=index.d.ts.map
@@ -13,12 +13,13 @@ exports.SwaggerExplorer = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const constants_1 = require("./constants");
15
15
  const documentBuilder_1 = require("./documentBuilder");
16
+ const _1 = require(".");
16
17
  let SwaggerExplorer = class SwaggerExplorer {
17
18
  constructor() {
18
19
  this.documentBuilder = new documentBuilder_1.DocumentBuilder();
19
20
  }
20
21
  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
+ 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, _2, _3, _4, _5;
22
23
  this.documentBuilder.setTitle(this.swaggerConfig.title);
23
24
  this.documentBuilder.setVersion(this.swaggerConfig.version);
24
25
  this.documentBuilder.setDescription(this.swaggerConfig.description);
@@ -47,25 +48,29 @@ let SwaggerExplorer = class SwaggerExplorer {
47
48
  }
48
49
  }
49
50
  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
+ for (const t of (_2 = this.swaggerConfig) === null || _2 === void 0 ? void 0 : _2.tags) {
51
52
  this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
52
53
  }
53
54
  }
54
55
  // 设置 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) {
56
+ if (Array.isArray((_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.auth)) {
57
+ for (const a of (_4 = this.swaggerConfig) === null || _4 === void 0 ? void 0 : _4.auth) {
57
58
  this.setAuth(a);
58
59
  }
59
60
  }
60
61
  else {
61
- this.setAuth((_4 = this.swaggerConfig) === null || _4 === void 0 ? void 0 : _4.auth);
62
+ this.setAuth((_5 = this.swaggerConfig) === null || _5 === void 0 ? void 0 : _5.auth);
62
63
  }
63
64
  }
64
65
  scanApp() {
66
+ var _a;
65
67
  const routes = (0, decorator_1.listModule)(decorator_1.CONTROLLER_KEY);
66
68
  for (const route of routes) {
67
69
  this.generatePath(route);
68
70
  }
71
+ if ((_a = this.swaggerConfig) === null || _a === void 0 ? void 0 : _a.tagSortable) {
72
+ this.documentBuilder.sortTags();
73
+ }
69
74
  }
70
75
  getData() {
71
76
  return this.documentBuilder.build();
@@ -98,11 +103,12 @@ let SwaggerExplorer = class SwaggerExplorer {
98
103
  (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
99
104
  }
100
105
  else {
101
- tag.name = (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.tagName) || 'default';
106
+ tag.name = controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.tagName;
102
107
  tag.description =
103
108
  (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
104
109
  }
105
110
  if (tag.name) {
111
+ strTags.push(tag.name);
106
112
  this.documentBuilder.addTag(tag.name, tag.description);
107
113
  }
108
114
  }
@@ -158,7 +164,7 @@ let SwaggerExplorer = class SwaggerExplorer {
158
164
  * 构造 router 提取方法
159
165
  */
160
166
  generateRouteMethod(url, webRouter, paths, metaForMethods, metaForParams, header, target) {
161
- var _a, _b, _c, _d, _e, _f, _g, _h;
167
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
162
168
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
163
169
  item.propertyName === webRouter.method)[0];
164
170
  let opts = paths[url];
@@ -196,7 +202,7 @@ let SwaggerExplorer = class SwaggerExplorer {
196
202
  continue;
197
203
  }
198
204
  }
199
- if ((0, decorator_1.isClass)(currentType)) {
205
+ if (decorator_1.Types.isClass(currentType)) {
200
206
  this.parseClzz(currentType);
201
207
  p.schema = {
202
208
  $ref: '#/components/schemas/' + currentType.name,
@@ -209,18 +215,65 @@ let SwaggerExplorer = class SwaggerExplorer {
209
215
  }
210
216
  this.parseFromParamsToP(params[params.length - 1 - arg.parameterIndex], p);
211
217
  if (p.in === 'body') {
218
+ // 这里兼容一下 @File()、@Files()、@Fields() 装饰器
219
+ if (((_j = arg.metadata) === null || _j === void 0 ? void 0 : _j.type) === decorator_1.RouteParamTypes.FILESSTREAM) {
220
+ p.schema = {
221
+ type: 'object',
222
+ properties: {
223
+ files: {
224
+ type: 'array',
225
+ items: {
226
+ type: 'string',
227
+ format: 'binary',
228
+ },
229
+ description: p.description,
230
+ },
231
+ },
232
+ };
233
+ p.contentType = _1.BodyContentType.Multipart;
234
+ }
235
+ if (((_k = arg.metadata) === null || _k === void 0 ? void 0 : _k.type) === decorator_1.RouteParamTypes.FILESTREAM) {
236
+ p.schema = {
237
+ type: 'object',
238
+ properties: {
239
+ file: {
240
+ type: 'string',
241
+ format: 'binary',
242
+ description: p.description,
243
+ },
244
+ },
245
+ };
246
+ p.contentType = _1.BodyContentType.Multipart;
247
+ }
248
+ if (((_l = arg.metadata) === null || _l === void 0 ? void 0 : _l.type) === decorator_1.RouteParamTypes.FIELDS) {
249
+ if (p.schema['$ref']) {
250
+ // 展开各个字段属性
251
+ const name = p.schema['$ref'].replace('#/components/schemas/', '');
252
+ const schema = this.documentBuilder.getSchema(name);
253
+ delete p.schema['$ref'];
254
+ p.schema = JSON.parse(JSON.stringify(schema));
255
+ }
256
+ p.contentType = _1.BodyContentType.Multipart;
257
+ }
212
258
  if (!p.content) {
213
259
  p.content = {};
214
260
  p.content[p.contentType || 'application/json'] = {
215
261
  schema: p.schema,
216
262
  };
217
263
  }
218
- const requestBody = {
219
- required: true,
220
- description: p.description || p.name,
221
- content: p.content,
222
- };
223
- opts[webRouter.requestMethod].requestBody = requestBody;
264
+ if (!opts[webRouter.requestMethod].requestBody) {
265
+ const requestBody = {
266
+ required: true,
267
+ description: p.description || p.name,
268
+ content: p.content,
269
+ };
270
+ opts[webRouter.requestMethod].requestBody = requestBody;
271
+ }
272
+ else {
273
+ // 这里拼 schema properties 时肯定存在
274
+ Object.assign(opts[webRouter.requestMethod].requestBody.content[p.contentType]
275
+ .schema.properties, p.schema.properties);
276
+ }
224
277
  delete p.contentType;
225
278
  delete p.content;
226
279
  // in body 不需要处理
@@ -243,7 +296,7 @@ let SwaggerExplorer = class SwaggerExplorer {
243
296
  // 这里是引用,赋值可以直接更改
244
297
  const tt = resp[k];
245
298
  if (tt.type) {
246
- if ((0, decorator_1.isClass)(tt.type)) {
299
+ if (decorator_1.Types.isClass(tt.type)) {
247
300
  this.parseClzz(tt.type);
248
301
  if (tt.isArray) {
249
302
  tt.content = {
@@ -343,7 +396,7 @@ let SwaggerExplorer = class SwaggerExplorer {
343
396
  }
344
397
  else {
345
398
  if (param.type) {
346
- if ((0, decorator_1.isClass)(param.type)) {
399
+ if (decorator_1.Types.isClass(param.type)) {
347
400
  this.parseClzz(param.type);
348
401
  p.schema = {
349
402
  $ref: '#/components/schemas/' + param.type.name,
@@ -379,6 +432,9 @@ let SwaggerExplorer = class SwaggerExplorer {
379
432
  * @param clzz
380
433
  */
381
434
  parseClzz(clzz) {
435
+ if (this.documentBuilder.getSchema(clzz.name)) {
436
+ return;
437
+ }
382
438
  const props = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_PROPERTY, clzz);
383
439
  const tt = {
384
440
  type: 'object',
@@ -432,7 +488,7 @@ let SwaggerExplorer = class SwaggerExplorer {
432
488
  currentType = (_b = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _b === void 0 ? void 0 : _b.type;
433
489
  delete metadata.items;
434
490
  }
435
- if ((0, decorator_1.isClass)(currentType)) {
491
+ if (decorator_1.Types.isClass(currentType)) {
436
492
  this.parseClzz(currentType);
437
493
  if (isArray) {
438
494
  tt.properties[key] = {
@@ -579,6 +635,9 @@ function convertTypeToString(type) {
579
635
  case decorator_1.RouteParamTypes.PARAM:
580
636
  return 'path';
581
637
  case decorator_1.RouteParamTypes.BODY:
638
+ case decorator_1.RouteParamTypes.FIELDS:
639
+ case decorator_1.RouteParamTypes.FILESSTREAM:
640
+ case decorator_1.RouteParamTypes.FILESTREAM:
582
641
  return 'body';
583
642
  default:
584
643
  return 'header';
@@ -595,7 +654,7 @@ function convertSchemaType(value) {
595
654
  case 'String':
596
655
  return 'string';
597
656
  default:
598
- return 'object';
657
+ return value;
599
658
  }
600
659
  }
601
660
  //# sourceMappingURL=swaggerExplorer.js.map
package/index.d.ts CHANGED
@@ -3,6 +3,6 @@ export * from './dist/index';
3
3
 
4
4
  declare module '@midwayjs/core/dist/interface' {
5
5
  interface MidwayConfig {
6
- swagger: Partial<SwaggerOptions>;
6
+ swagger?: Partial<SwaggerOptions>;
7
7
  }
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.0.0-beta.13",
3
+ "version": "3.0.0-beta.17",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -9,23 +9,23 @@
9
9
  "index.d.ts"
10
10
  ],
11
11
  "devDependencies": {
12
- "@midwayjs/core": "^3.0.0-beta.13",
13
- "@midwayjs/decorator": "^3.0.0-beta.13",
14
- "@midwayjs/koa": "^3.0.0-beta.13",
15
- "@midwayjs/mock": "^3.0.0-beta.13",
16
- "swagger-ui-dist": "^3.34.0"
12
+ "@midwayjs/core": "^3.0.0-beta.17",
13
+ "@midwayjs/decorator": "^3.0.0-beta.17",
14
+ "@midwayjs/koa": "^3.0.0-beta.17",
15
+ "@midwayjs/mock": "^3.0.0-beta.17",
16
+ "swagger-ui-dist": "4.1.3"
17
17
  },
18
18
  "author": "Kurten Chan <chinkurten@gmail.com>",
19
19
  "license": "MIT",
20
20
  "scripts": {
21
21
  "build": "tsc",
22
22
  "jest": "node --require=ts-node/register ../../node_modules/jest/bin/jest.js",
23
- "test": "node --require=ts-node/register ../../node_modules/.bin/jest",
24
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit"
23
+ "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
24
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit"
25
25
  },
26
26
  "repository": {
27
27
  "type": "git",
28
28
  "url": "https://github.com/midwayjs/midway.git"
29
29
  },
30
- "gitHead": "d3c47770fee9dce33a8d148882173fd7782864ad"
30
+ "gitHead": "17a8b5bd3d0b0b21f24dd2f165b5df9097fc3ec4"
31
31
  }
package/CHANGELOG.md DELETED
@@ -1,49 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [3.0.0-beta.13](https://github.com/midwayjs/midway/compare/v3.0.0-beta.12...v3.0.0-beta.13) (2021-12-30)
7
-
8
-
9
- ### Features
10
-
11
- * add custom decorator filter ([#1477](https://github.com/midwayjs/midway/issues/1477)) ([97501a9](https://github.com/midwayjs/midway/commit/97501a989abc211b0c7400b1df45e050bb237c6a))
12
-
13
-
14
-
15
-
16
-
17
- # [3.0.0-beta.12](https://github.com/midwayjs/midway/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-12-28)
18
-
19
-
20
- ### Bug Fixes
21
-
22
- * 3.x copy all properties ([#1444](https://github.com/midwayjs/midway/issues/1444)) ([21ec8b6](https://github.com/midwayjs/midway/commit/21ec8b6a85b6ba3f4fbff0c8a571484aaa078788))
23
-
24
-
25
- ### Features
26
-
27
- * add fileupload support ([#1439](https://github.com/midwayjs/midway/issues/1439)) ([0a81e72](https://github.com/midwayjs/midway/commit/0a81e720f525ddab0718301f44f80fce376f9bfe))
28
- * support throw err status ([#1440](https://github.com/midwayjs/midway/issues/1440)) ([7b98110](https://github.com/midwayjs/midway/commit/7b98110d65c5287a8fcb3eb5356dea2d7a32cee9))
29
-
30
-
31
-
32
-
33
-
34
- # [3.0.0-beta.11](https://github.com/midwayjs/midway/compare/v3.0.0-beta.10...v3.0.0-beta.11) (2021-12-21)
35
-
36
- **Note:** Version bump only for package @midwayjs/swagger
37
-
38
-
39
-
40
-
41
-
42
- # [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
43
-
44
-
45
- ### Features
46
-
47
- * 3.x swagger ([#1409](https://github.com/midwayjs/midway/issues/1409)) ([e00fbbf](https://github.com/midwayjs/midway/commit/e00fbbf7a494f300a53f3bd8635966364cfd96a9))
48
- * add swagger doc & fix bug ([#1427](https://github.com/midwayjs/midway/issues/1427)) ([82dc6f1](https://github.com/midwayjs/midway/commit/82dc6f10f7244a75f58922edda1b0625fc6cb90e))
49
- * support express ([61e73db](https://github.com/midwayjs/midway/commit/61e73db509bfea21c8251855fccb02a4ce09988f))