@midwayjs/swagger 3.20.6 → 4.0.0-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.
@@ -49,6 +49,6 @@ const isEnumArray = (obj) => obj.isArray && obj.enum;
49
49
  exports.isEnumArray = isEnumArray;
50
50
  const isEnumDefined = (obj) => obj.enum;
51
51
  exports.isEnumDefined = isEnumDefined;
52
- const isEnumMetadata = (metadata) => { var _a; return metadata.enum || (metadata.isArray && ((_a = metadata.items) === null || _a === void 0 ? void 0 : _a['enum'])); };
52
+ const isEnumMetadata = (metadata) => metadata.enum || (metadata.isArray && metadata.items?.['enum']);
53
53
  exports.isEnumMetadata = isEnumMetadata;
54
54
  //# sourceMappingURL=enum.utils.js.map
@@ -51,5 +51,5 @@ var HttpStatus;
51
51
  HttpStatus[HttpStatus["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
52
52
  HttpStatus[HttpStatus["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
53
53
  HttpStatus[HttpStatus["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
54
- })(HttpStatus = exports.HttpStatus || (exports.HttpStatus = {}));
54
+ })(HttpStatus || (exports.HttpStatus = HttpStatus = {}));
55
55
  //# sourceMappingURL=httpStatus.js.map
@@ -41,6 +41,7 @@ let SwaggerConfiguration = class SwaggerConfiguration {
41
41
  }
42
42
  }
43
43
  };
44
+ exports.SwaggerConfiguration = SwaggerConfiguration;
44
45
  __decorate([
45
46
  (0, core_1.Inject)(),
46
47
  __metadata("design:type", core_1.MidwayApplicationManager)
@@ -49,7 +50,7 @@ __decorate([
49
50
  (0, core_1.Inject)(),
50
51
  __metadata("design:type", core_1.MidwayConfigService)
51
52
  ], SwaggerConfiguration.prototype, "configService", void 0);
52
- SwaggerConfiguration = __decorate([
53
+ exports.SwaggerConfiguration = SwaggerConfiguration = __decorate([
53
54
  (0, core_1.Configuration)({
54
55
  importConfigs: [
55
56
  {
@@ -59,5 +60,4 @@ SwaggerConfiguration = __decorate([
59
60
  namespace: 'swagger',
60
61
  })
61
62
  ], SwaggerConfiguration);
62
- exports.SwaggerConfiguration = SwaggerConfiguration;
63
63
  //# sourceMappingURL=configuration.js.map
@@ -10,7 +10,7 @@ var BodyContentType;
10
10
  BodyContentType["Multipart"] = "multipart/form-data";
11
11
  BodyContentType["MultipartMixed"] = "multipart/mixed";
12
12
  BodyContentType["OctetStream"] = "application/octet-stream";
13
- })(BodyContentType = exports.BodyContentType || (exports.BodyContentType = {}));
13
+ })(BodyContentType || (exports.BodyContentType = BodyContentType = {}));
14
14
  const defaultBodyMetadata = {
15
15
  type: String,
16
16
  required: true,
@@ -5,7 +5,7 @@ const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("../constants");
6
6
  function ApiExcludeController(disable = true) {
7
7
  return (target) => {
8
- (0, core_1.saveClassMetadata)(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, { disable }, target);
8
+ core_1.MetadataManager.defineMetadata(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, { disable }, target);
9
9
  };
10
10
  }
11
11
  exports.ApiExcludeController = ApiExcludeController;
@@ -4,20 +4,20 @@ exports.getSchemaPath = exports.getTypeIsArrayTuple = exports.createParamDecorat
4
4
  const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("../constants");
6
6
  function createPropertyDecorator(metakey, metadata) {
7
- return (0, core_1.createCustomPropertyDecorator)(metakey, metadata, false);
7
+ return core_1.DecoratorManager.createCustomPropertyDecorator(metakey, metadata, false);
8
8
  }
9
9
  exports.createPropertyDecorator = createPropertyDecorator;
10
10
  function createMixedDecorator(metakey, metadata) {
11
11
  return (target, methodName) => {
12
12
  if (methodName) {
13
- (0, core_1.attachPropertyDataToClass)(constants_1.DECORATORS_METHOD_METADATA, {
13
+ core_1.MetadataManager.attachMetadata(constants_1.DECORATORS_METHOD_METADATA, {
14
14
  key: metakey,
15
15
  propertyName: methodName,
16
16
  metadata,
17
17
  }, target, methodName);
18
18
  }
19
19
  else {
20
- (0, core_1.attachClassMetadata)(constants_1.DECORATORS_CLASS_METADATA, {
20
+ core_1.MetadataManager.attachMetadata(constants_1.DECORATORS_CLASS_METADATA, {
21
21
  key: metakey,
22
22
  metadata,
23
23
  }, target);
@@ -68,9 +68,8 @@ class DocumentBuilder {
68
68
  return this;
69
69
  }
70
70
  getSchema(name) {
71
- var _a, _b;
72
- if ((_a = this.document.components) === null || _a === void 0 ? void 0 : _a.schemas) {
73
- return (_b = this.document.components) === null || _b === void 0 ? void 0 : _b.schemas[name];
71
+ if (this.document.components?.schemas) {
72
+ return this.document.components?.schemas[name];
74
73
  }
75
74
  return undefined;
76
75
  }
@@ -151,7 +150,7 @@ class DocumentBuilder {
151
150
  this.addSecurity(name, {
152
151
  type: 'oauth2',
153
152
  flows: {
154
- ...options === null || options === void 0 ? void 0 : options.flows,
153
+ ...options?.flows,
155
154
  },
156
155
  });
157
156
  return this;
@@ -22,48 +22,47 @@ let SwaggerExplorer = class SwaggerExplorer {
22
22
  this.operationIdFactory = (controllerKey, webRouter) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;
23
23
  }
24
24
  async init() {
25
- 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, _6, _7;
26
25
  this.documentBuilder.setTitle(this.swaggerConfig.title);
27
26
  this.documentBuilder.setVersion(this.swaggerConfig.version);
28
27
  this.documentBuilder.setDescription(this.swaggerConfig.description);
29
- if (((_a = this.swaggerConfig) === null || _a === void 0 ? void 0 : _a.contact) &&
30
- typeof ((_b = this.swaggerConfig) === null || _b === void 0 ? void 0 : _b.contact) === 'object') {
31
- 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
+ if (this.swaggerConfig?.contact &&
29
+ typeof this.swaggerConfig?.contact === 'object') {
30
+ this.documentBuilder.setContact(this.swaggerConfig?.contact?.name, this.swaggerConfig?.contact?.url, this.swaggerConfig?.contact?.email);
32
31
  }
33
- if (((_j = this.swaggerConfig) === null || _j === void 0 ? void 0 : _j.license) &&
34
- typeof ((_k = this.swaggerConfig) === null || _k === void 0 ? void 0 : _k.license) === 'object') {
35
- 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
+ if (this.swaggerConfig?.license &&
33
+ typeof this.swaggerConfig?.license === 'object') {
34
+ this.documentBuilder.setLicense(this.swaggerConfig?.license?.name, this.swaggerConfig?.license?.url);
36
35
  }
37
36
  if (this.swaggerConfig.termsOfService) {
38
37
  this.documentBuilder.setTermsOfService(this.swaggerConfig.termsOfService);
39
38
  }
40
- if (((_q = this.swaggerConfig) === null || _q === void 0 ? void 0 : _q.externalDocs) &&
41
- typeof ((_r = this.swaggerConfig) === null || _r === void 0 ? void 0 : _r.externalDocs) === 'object') {
42
- 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);
39
+ if (this.swaggerConfig?.externalDocs &&
40
+ typeof this.swaggerConfig?.externalDocs === 'object') {
41
+ this.documentBuilder.setExternalDoc(this.swaggerConfig?.externalDocs?.description, this.swaggerConfig?.externalDocs?.url);
43
42
  }
44
- if (((_w = this.swaggerConfig) === null || _w === void 0 ? void 0 : _w.servers) &&
45
- Array.isArray((_x = this.swaggerConfig) === null || _x === void 0 ? void 0 : _x.servers)) {
46
- for (const serv of (_y = this.swaggerConfig) === null || _y === void 0 ? void 0 : _y.servers) {
47
- this.documentBuilder.addServer(serv === null || serv === void 0 ? void 0 : serv.url, serv === null || serv === void 0 ? void 0 : serv.description, serv === null || serv === void 0 ? void 0 : serv.variables);
43
+ if (this.swaggerConfig?.servers &&
44
+ Array.isArray(this.swaggerConfig?.servers)) {
45
+ for (const serv of this.swaggerConfig?.servers) {
46
+ this.documentBuilder.addServer(serv?.url, serv?.description, serv?.variables);
48
47
  }
49
48
  }
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)) {
51
- for (const t of (_2 = this.swaggerConfig) === null || _2 === void 0 ? void 0 : _2.tags) {
49
+ if (this.swaggerConfig?.tags && Array.isArray(this.swaggerConfig?.tags)) {
50
+ for (const t of this.swaggerConfig?.tags) {
52
51
  this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
53
52
  }
54
53
  }
55
- if ((_4 = (_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.documentOptions) === null || _4 === void 0 ? void 0 : _4.operationIdFactory) {
54
+ if (this.swaggerConfig?.documentOptions?.operationIdFactory) {
56
55
  this.operationIdFactory =
57
56
  this.swaggerConfig.documentOptions.operationIdFactory;
58
57
  }
59
58
  // 设置 auth 类型
60
- if (Array.isArray((_5 = this.swaggerConfig) === null || _5 === void 0 ? void 0 : _5.auth)) {
61
- for (const a of (_6 = this.swaggerConfig) === null || _6 === void 0 ? void 0 : _6.auth) {
59
+ if (Array.isArray(this.swaggerConfig?.auth)) {
60
+ for (const a of this.swaggerConfig?.auth) {
62
61
  this.setAuth(a);
63
62
  }
64
63
  }
65
64
  else {
66
- this.setAuth((_7 = this.swaggerConfig) === null || _7 === void 0 ? void 0 : _7.auth);
65
+ this.setAuth(this.swaggerConfig?.auth);
67
66
  }
68
67
  }
69
68
  addGlobalPrefix(globalPrefix) {
@@ -85,12 +84,11 @@ let SwaggerExplorer = class SwaggerExplorer {
85
84
  this.documentBuilder.setPaths(newPaths);
86
85
  }
87
86
  scanApp() {
88
- var _a;
89
- const routes = (0, core_1.listModule)(core_1.CONTROLLER_KEY);
87
+ const routes = core_1.DecoratorManager.listModule(core_1.CONTROLLER_KEY);
90
88
  for (const route of routes) {
91
89
  this.generatePath(route);
92
90
  }
93
- if ((_a = this.swaggerConfig) === null || _a === void 0 ? void 0 : _a.tagSortable) {
91
+ if (this.swaggerConfig?.tagSortable) {
94
92
  this.documentBuilder.sortTags();
95
93
  }
96
94
  }
@@ -101,21 +99,20 @@ let SwaggerExplorer = class SwaggerExplorer {
101
99
  return this.documentBuilder;
102
100
  }
103
101
  generatePath(target) {
104
- var _a;
105
102
  // 获取控制器元数据
106
- const excludeClassMeta = (0, core_1.getClassMetadata)(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, target);
103
+ const excludeClassMeta = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, target);
107
104
  if (excludeClassMeta && excludeClassMeta.disable) {
108
105
  // 如果存在需要排除的控制器,则直接返回
109
106
  return;
110
107
  }
111
- const isGenerateTagForController = (_a = this.swaggerConfig.isGenerateTagForController) !== null && _a !== void 0 ? _a : true;
108
+ const isGenerateTagForController = this.swaggerConfig.isGenerateTagForController ?? true;
112
109
  // 解析额外的模型
113
110
  this.parseExtraModel(target);
114
- const metaForClass = (0, core_1.getClassMetadata)(constants_1.DECORATORS_CLASS_METADATA, target) || [];
111
+ const metaForClass = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS_CLASS_METADATA, target) || [];
115
112
  // 获取参数的元数据
116
- const metaForParams = (0, core_1.getClassMetadata)(core_1.INJECT_CUSTOM_PARAM, target) || [];
113
+ const metaForParams = core_1.MetadataManager.getPropertiesWithMetadata(core_1.CUSTOM_PARAM_INJECT_KEY, target) || {};
117
114
  // 获取控制器选项
118
- const controllerOption = (0, core_1.getClassMetadata)(core_1.CONTROLLER_KEY, target);
115
+ const controllerOption = core_1.MetadataManager.getOwnMetadata(core_1.CONTROLLER_KEY, target);
119
116
  // 获取前缀
120
117
  const prefix = controllerOption.prefix;
121
118
  // 过滤出标签
@@ -135,15 +132,15 @@ let SwaggerExplorer = class SwaggerExplorer {
135
132
  const tag = { name: '', description: '' };
136
133
  if (prefix !== '/') {
137
134
  tag.name =
138
- (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.tagName) ||
135
+ controllerOption?.routerOptions.tagName ||
139
136
  (/^\//.test(prefix) ? prefix.split('/')[1] : prefix);
140
137
  tag.description =
141
- (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
138
+ controllerOption?.routerOptions.description || tag.name;
142
139
  }
143
140
  else {
144
- tag.name = controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.tagName;
141
+ tag.name = controllerOption?.routerOptions.tagName;
145
142
  tag.description =
146
- (controllerOption === null || controllerOption === void 0 ? void 0 : controllerOption.routerOptions.description) || tag.name;
143
+ controllerOption?.routerOptions.description || tag.name;
147
144
  }
148
145
  // 如果标签名存在,则将其添加到文档构建器中
149
146
  if (tag.name) {
@@ -156,7 +153,7 @@ let SwaggerExplorer = class SwaggerExplorer {
156
153
  }
157
154
  }
158
155
  // 获取路由信息
159
- const webRouterInfo = (0, core_1.getClassMetadata)(core_1.WEB_ROUTER_KEY, target);
156
+ const webRouterInfo = core_1.MetadataManager.getMetadata(core_1.WEB_ROUTER_KEY, target);
160
157
  // 过滤出头部信息
161
158
  let headers = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
162
159
  if (headers.length > 0) {
@@ -173,7 +170,7 @@ let SwaggerExplorer = class SwaggerExplorer {
173
170
  let url = (prefix + webRouter.path).replace('//', '/');
174
171
  url = replaceUrl(url, parseParamsInPath(url));
175
172
  // 方法元数据
176
- const metaForMethods = (0, core_1.getPropertyDataFromClass)(constants_1.DECORATORS_METHOD_METADATA, target, webRouter.method) || [];
173
+ const metaForMethods = core_1.MetadataManager.getMetadata(constants_1.DECORATORS_METHOD_METADATA, target, webRouter.method) || [];
177
174
  const methodSecurity = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
178
175
  // 判断是否忽略当前路由
179
176
  const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
@@ -192,11 +189,8 @@ let SwaggerExplorer = class SwaggerExplorer {
192
189
  // 获取路由参数
193
190
  const routerArgs = metaForParams[webRouter.method] || [];
194
191
  // 过滤出主体参数
195
- const bds = routerArgs.filter(item => {
196
- var _a;
197
- return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
198
- ((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) === core_1.RouteParamTypes.BODY;
199
- });
192
+ const bds = routerArgs.filter(item => item.key === core_1.WEB_ROUTER_PARAM_KEY &&
193
+ item?.metadata?.type === core_1.RouteParamTypes.BODY);
200
194
  // 如果存在多个主体参数,则跳过当前循环,因为swagger不支持多个@Body
201
195
  if (bds.length > 1) {
202
196
  continue;
@@ -263,7 +257,6 @@ let SwaggerExplorer = class SwaggerExplorer {
263
257
  * 构造 router 提取方法
264
258
  */
265
259
  generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target) {
266
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
267
260
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
268
261
  item.propertyName === webRouter.method)[0];
269
262
  const routerTagsMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_TAGS &&
@@ -275,27 +268,24 @@ let SwaggerExplorer = class SwaggerExplorer {
275
268
  }
276
269
  const parameters = [];
277
270
  opts[webRouter.requestMethod] = {
278
- summary: getNotEmptyValue((_a = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _a === void 0 ? void 0 : _a.summary, webRouter.summary),
279
- description: getNotEmptyValue((_b = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _b === void 0 ? void 0 : _b.description, webRouter.description),
280
- operationId: ((_c = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _c === void 0 ? void 0 : _c.operationId) ||
271
+ summary: getNotEmptyValue(operMeta?.metadata?.summary, webRouter.summary),
272
+ description: getNotEmptyValue(operMeta?.metadata?.description, webRouter.description),
273
+ operationId: operMeta?.metadata?.operationId ||
281
274
  this.getOperationId(target.name, webRouter),
282
- tags: routerTags.length ? routerTags : (_e = (_d = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _d === void 0 ? void 0 : _d.tags) !== null && _e !== void 0 ? _e : [],
275
+ tags: routerTags.length ? routerTags : operMeta?.metadata?.tags ?? [],
283
276
  };
284
- if (((_f = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _f === void 0 ? void 0 : _f.deprecated) != null) {
277
+ if (operMeta?.metadata?.deprecated != null) {
285
278
  opts[webRouter.requestMethod].deprecated =
286
- !!((_g = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _g === void 0 ? void 0 : _g.deprecated);
279
+ !!operMeta?.metadata?.deprecated;
287
280
  }
288
281
  /**
289
282
  * [{"key":"web:router_param","parameterIndex":1,"propertyName":"create","metadata":{"type":2}},
290
283
  * {"key":"web:router_param","parameterIndex":0,"propertyName":"create","metadata":{"type":1,"propertyData":"createCatDto"}}]
291
284
  */
292
285
  // WEB_ROUTER_PARAM_KEY
293
- const args = routerArgs.filter(item => {
294
- var _a;
295
- return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
296
- ((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) !== core_1.RouteParamTypes.CUSTOM;
297
- });
298
- const types = (0, core_1.getMethodParamTypes)(target, webRouter.method);
286
+ const args = routerArgs.filter(item => item.key === core_1.WEB_ROUTER_PARAM_KEY &&
287
+ item?.metadata?.type !== core_1.RouteParamTypes.CUSTOM);
288
+ const types = core_1.MetadataManager.getMethodParamTypes(target, webRouter.method);
299
289
  const params = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_PARAMETERS &&
300
290
  item.propertyName === webRouter.method);
301
291
  // set params information from @ApiQuery() to parameters
@@ -331,9 +321,9 @@ let SwaggerExplorer = class SwaggerExplorer {
331
321
  parameters.push(p);
332
322
  }
333
323
  else if (p.in === 'body') {
334
- p.content = (_h = p.content) !== null && _h !== void 0 ? _h : {};
324
+ p.content = p.content ?? {};
335
325
  if (Object.keys(p.content).length === 0) {
336
- p.content[p.contentType || 'application/json'] = (_j = p.content[p.contentType || 'application/json']) !== null && _j !== void 0 ? _j : {
326
+ p.content[p.contentType || 'application/json'] = p.content[p.contentType || 'application/json'] ?? {
337
327
  schema: p.schema,
338
328
  };
339
329
  }
@@ -343,25 +333,25 @@ let SwaggerExplorer = class SwaggerExplorer {
343
333
  }
344
334
  // if requestBody is already set, skip
345
335
  opts[webRouter.requestMethod].requestBody =
346
- (_k = opts[webRouter.requestMethod].requestBody) !== null && _k !== void 0 ? _k : {};
336
+ opts[webRouter.requestMethod].requestBody ?? {};
347
337
  opts[webRouter.requestMethod].requestBody.description =
348
- (_l = opts[webRouter.requestMethod].requestBody.description) !== null && _l !== void 0 ? _l : p.description;
338
+ opts[webRouter.requestMethod].requestBody.description ??
339
+ p.description;
349
340
  opts[webRouter.requestMethod].requestBody.content =
350
- (_m = opts[webRouter.requestMethod].requestBody.content) !== null && _m !== void 0 ? _m : p.content;
341
+ opts[webRouter.requestMethod].requestBody.content ?? p.content;
351
342
  opts[webRouter.requestMethod].requestBody.required =
352
- (_o = opts[webRouter.requestMethod].requestBody.required) !== null && _o !== void 0 ? _o : p.required;
343
+ opts[webRouter.requestMethod].requestBody.required ?? p.required;
353
344
  }
354
345
  }
355
346
  for (const arg of args) {
356
347
  const currentType = types[arg.parameterIndex];
357
348
  const p = {
358
- name: (_p = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _p === void 0 ? void 0 : _p.propertyData,
359
- in: convertTypeToString((_q = arg.metadata) === null || _q === void 0 ? void 0 : _q.type),
349
+ name: arg?.metadata?.propertyData,
350
+ in: convertTypeToString(arg.metadata?.type),
360
351
  required: false,
361
352
  };
362
353
  const existsParam = parameters.find(item => {
363
- var _a;
364
- return item.name === ((_a = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _a === void 0 ? void 0 : _a.propertyData) && item.in === p.in;
354
+ return item.name === arg?.metadata?.propertyData && item.in === p.in;
365
355
  });
366
356
  // if exists same param from @ApiQuery and other decorator, just skip
367
357
  if (existsParam) {
@@ -381,13 +371,13 @@ let SwaggerExplorer = class SwaggerExplorer {
381
371
  // 如果@Query()装饰的 是一个对象,则把该对象的子属性作为多个@Query参数
382
372
  const schema = this.documentBuilder.getSchema(currentType.name);
383
373
  Object.keys(schema.properties).forEach(pName => {
384
- var _a, _b;
385
374
  const pp = {
386
375
  name: pName,
387
376
  in: p.in,
388
- description: (_a = schema.properties[pName]) === null || _a === void 0 ? void 0 : _a.description,
377
+ description: schema.properties[pName]
378
+ ?.description,
389
379
  schema: schema.properties[pName],
390
- required: ((_b = schema.required) === null || _b === void 0 ? void 0 : _b.includes(pName)) || false,
380
+ required: schema.required?.includes(pName) || false,
391
381
  };
392
382
  parameters.push(pp);
393
383
  });
@@ -398,7 +388,7 @@ let SwaggerExplorer = class SwaggerExplorer {
398
388
  continue;
399
389
  }
400
390
  p.schema = {
401
- type: convertSchemaType((_r = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _r !== void 0 ? _r : currentType),
391
+ type: convertSchemaType(currentType?.name ?? currentType),
402
392
  };
403
393
  }
404
394
  }
@@ -411,7 +401,7 @@ let SwaggerExplorer = class SwaggerExplorer {
411
401
  continue;
412
402
  }
413
403
  // 这里兼容一下 @File()、@Files()、@Fields() 装饰器
414
- if (((_s = arg.metadata) === null || _s === void 0 ? void 0 : _s.type) === core_1.RouteParamTypes.FILESSTREAM) {
404
+ if (arg.metadata?.type === core_1.RouteParamTypes.FILESSTREAM) {
415
405
  p.content = {};
416
406
  p.content[_1.BodyContentType.Multipart] = {
417
407
  schema: {
@@ -429,7 +419,7 @@ let SwaggerExplorer = class SwaggerExplorer {
429
419
  },
430
420
  };
431
421
  }
432
- else if (((_t = arg.metadata) === null || _t === void 0 ? void 0 : _t.type) === core_1.RouteParamTypes.FILESTREAM) {
422
+ else if (arg.metadata?.type === core_1.RouteParamTypes.FILESTREAM) {
433
423
  p.content = {};
434
424
  p.content[_1.BodyContentType.Multipart] = {
435
425
  schema: {
@@ -459,7 +449,7 @@ let SwaggerExplorer = class SwaggerExplorer {
459
449
  p.content = {
460
450
  'text/plain': {
461
451
  schema: {
462
- type: convertSchemaType((_u = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _u !== void 0 ? _u : currentType),
452
+ type: convertSchemaType(currentType?.name ?? currentType),
463
453
  },
464
454
  },
465
455
  };
@@ -565,7 +555,7 @@ let SwaggerExplorer = class SwaggerExplorer {
565
555
  * @param clzz
566
556
  */
567
557
  parseExtraModel(clzz) {
568
- const metaForClass = (0, core_1.getClassMetadata)(constants_1.DECORATORS_CLASS_METADATA, clzz) || [];
558
+ const metaForClass = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS_CLASS_METADATA, clzz) || [];
569
559
  const extraModels = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXTRA_MODEL);
570
560
  for (const m of extraModels) {
571
561
  if (Array.isArray(m.metadata)) {
@@ -579,46 +569,45 @@ let SwaggerExplorer = class SwaggerExplorer {
579
569
  }
580
570
  }
581
571
  parseSubPropertyType(metadata) {
582
- var _a, _b, _c;
583
572
  let typeMeta;
584
- if (metadata === null || metadata === void 0 ? void 0 : metadata.enum) {
573
+ if (metadata?.enum) {
585
574
  typeMeta = {
586
- type: metadata === null || metadata === void 0 ? void 0 : metadata.type,
587
- enum: metadata === null || metadata === void 0 ? void 0 : metadata.enum,
588
- default: metadata === null || metadata === void 0 ? void 0 : metadata.default,
575
+ type: metadata?.type,
576
+ enum: metadata?.enum,
577
+ default: metadata?.default,
589
578
  };
590
- if (metadata === null || metadata === void 0 ? void 0 : metadata.description) {
591
- typeMeta.description = metadata === null || metadata === void 0 ? void 0 : metadata.description;
579
+ if (metadata?.description) {
580
+ typeMeta.description = metadata?.description;
592
581
  }
593
582
  return typeMeta;
594
583
  }
595
- if ((_a = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _a === void 0 ? void 0 : _a.enum) {
584
+ if (metadata?.items?.enum) {
596
585
  typeMeta = {
597
- type: metadata === null || metadata === void 0 ? void 0 : metadata.type,
598
- items: metadata === null || metadata === void 0 ? void 0 : metadata.items,
599
- default: metadata === null || metadata === void 0 ? void 0 : metadata.default,
586
+ type: metadata?.type,
587
+ items: metadata?.items,
588
+ default: metadata?.default,
600
589
  };
601
- if (metadata === null || metadata === void 0 ? void 0 : metadata.description) {
602
- typeMeta.description = metadata === null || metadata === void 0 ? void 0 : metadata.description;
590
+ if (metadata?.description) {
591
+ typeMeta.description = metadata?.description;
603
592
  }
604
593
  return typeMeta;
605
594
  }
606
595
  let isArray = false;
607
- let currentType = parseTypeSchema(metadata === null || metadata === void 0 ? void 0 : metadata.type);
608
- metadata === null || metadata === void 0 ? true : delete metadata.type;
596
+ let currentType = parseTypeSchema(metadata?.type);
597
+ delete metadata?.type;
609
598
  if (currentType === 'array') {
610
599
  isArray = true;
611
- currentType = parseTypeSchema((_b = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _b === void 0 ? void 0 : _b.type);
612
- metadata === null || metadata === void 0 ? true : delete metadata.items.type;
600
+ currentType = parseTypeSchema(metadata?.items?.type);
601
+ delete metadata?.items.type;
613
602
  }
614
- if (metadata === null || metadata === void 0 ? void 0 : metadata.oneOf) {
603
+ if (metadata?.oneOf) {
615
604
  typeMeta = {
616
605
  oneOf: [],
617
606
  };
618
- metadata === null || metadata === void 0 ? void 0 : metadata.oneOf.forEach((item) => {
607
+ metadata?.oneOf.forEach((item) => {
619
608
  typeMeta.push(this.parseSubPropertyType(item));
620
609
  });
621
- metadata === null || metadata === void 0 ? true : delete metadata.oneOf;
610
+ delete metadata?.oneOf;
622
611
  }
623
612
  if (core_1.Types.isClass(currentType)) {
624
613
  this.parseClzz(currentType);
@@ -626,13 +615,13 @@ let SwaggerExplorer = class SwaggerExplorer {
626
615
  typeMeta = {
627
616
  type: 'array',
628
617
  items: {
629
- $ref: '#/components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
618
+ $ref: '#/components/schemas/' + currentType?.name,
630
619
  },
631
620
  };
632
621
  }
633
622
  else {
634
623
  typeMeta = {
635
- $ref: '#/components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
624
+ $ref: '#/components/schemas/' + currentType?.name,
636
625
  };
637
626
  }
638
627
  delete metadata.items;
@@ -641,19 +630,19 @@ let SwaggerExplorer = class SwaggerExplorer {
641
630
  if (isArray) {
642
631
  // 没有配置类型则认为自己配置了 items 内容
643
632
  if (!currentType) {
644
- if ((_c = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _c === void 0 ? void 0 : _c['$ref']) {
633
+ if (metadata?.items?.['$ref']) {
645
634
  metadata.items['$ref'] = parseTypeSchema(metadata.items['$ref']);
646
635
  }
647
636
  typeMeta = {
648
637
  type: 'array',
649
- items: metadata === null || metadata === void 0 ? void 0 : metadata.items,
638
+ items: metadata?.items,
650
639
  };
651
640
  }
652
641
  else {
653
642
  typeMeta = {
654
643
  type: 'array',
655
644
  items: {
656
- type: convertSchemaType((currentType === null || currentType === void 0 ? void 0 : currentType.name) || currentType),
645
+ type: convertSchemaType(currentType?.name || currentType),
657
646
  },
658
647
  };
659
648
  }
@@ -662,7 +651,7 @@ let SwaggerExplorer = class SwaggerExplorer {
662
651
  else {
663
652
  typeMeta = {
664
653
  type: currentType,
665
- format: metadata === null || metadata === void 0 ? void 0 : metadata.format,
654
+ format: metadata?.format,
666
655
  };
667
656
  // Date 类型支持
668
657
  if (typeMeta.type === 'Date') {
@@ -780,7 +769,11 @@ let SwaggerExplorer = class SwaggerExplorer {
780
769
  this.parseExtraModel(clzz);
781
770
  // 解析类上的 ApiProperty
782
771
  // TODO 这里后面不能用这个方法
783
- const props = (0, core_1.getClassExtendedMetadata)(core_1.INJECT_CUSTOM_PROPERTY, clzz);
772
+ const props = core_1.MetadataManager.getPropertiesWithMetadata(core_1.CUSTOM_PROPERTY_INJECT_KEY, clzz) || {};
773
+ // 这里属性值唯一,取数组最后一个
774
+ for (const key in props) {
775
+ props[key] = props[key][props[key].length - 1];
776
+ }
784
777
  const tt = {
785
778
  type: 'object',
786
779
  properties: {},
@@ -794,7 +787,7 @@ let SwaggerExplorer = class SwaggerExplorer {
794
787
  const metadata = props[key].metadata || {};
795
788
  if (!metadata.type) {
796
789
  // 推导类型
797
- metadata.type = (0, core_1.getPropertyType)(clzz.prototype, key).name;
790
+ metadata.type = core_1.MetadataManager.transformTypeFromTSDesign(core_1.MetadataManager.getPropertyType(clzz.prototype, key)).name;
798
791
  }
799
792
  tt.properties[key] = tt.properties[key] || {};
800
793
  // loop metadata
@@ -898,6 +891,7 @@ let SwaggerExplorer = class SwaggerExplorer {
898
891
  }
899
892
  }
900
893
  };
894
+ exports.SwaggerExplorer = SwaggerExplorer;
901
895
  __decorate([
902
896
  (0, core_1.Config)('swagger'),
903
897
  __metadata("design:type", Object)
@@ -908,11 +902,10 @@ __decorate([
908
902
  __metadata("design:paramtypes", []),
909
903
  __metadata("design:returntype", Promise)
910
904
  ], SwaggerExplorer.prototype, "init", null);
911
- SwaggerExplorer = __decorate([
905
+ exports.SwaggerExplorer = SwaggerExplorer = __decorate([
912
906
  (0, core_1.Provide)(),
913
907
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
914
908
  ], SwaggerExplorer);
915
- exports.SwaggerExplorer = SwaggerExplorer;
916
909
  /**
917
910
  * 解释路由上的参数
918
911
  * @param url
@@ -75,6 +75,7 @@ let SwaggerMiddleware = class SwaggerMiddleware {
75
75
  return 'swagger';
76
76
  }
77
77
  };
78
+ exports.SwaggerMiddleware = SwaggerMiddleware;
78
79
  __decorate([
79
80
  (0, core_1.Config)('swagger'),
80
81
  __metadata("design:type", Object)
@@ -93,9 +94,8 @@ __decorate([
93
94
  __metadata("design:paramtypes", []),
94
95
  __metadata("design:returntype", Promise)
95
96
  ], SwaggerMiddleware.prototype, "init", null);
96
- SwaggerMiddleware = __decorate([
97
+ exports.SwaggerMiddleware = SwaggerMiddleware = __decorate([
97
98
  (0, core_1.Provide)(),
98
99
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
99
100
  ], SwaggerMiddleware);
100
- exports.SwaggerMiddleware = SwaggerMiddleware;
101
101
  //# sourceMappingURL=swaggerMiddleware.js.map
package/dist/ui/render.js CHANGED
@@ -43,7 +43,7 @@ function renderSwaggerUIDist(swaggerConfig, swaggerExplorer) {
43
43
  }
44
44
  let content = (0, fs_1.readFileSync)((0, path_1.join)(swaggerUiAssetPath, lastName));
45
45
  if (lastName === 'swagger-initializer.js' &&
46
- (swaggerRenderOptions === null || swaggerRenderOptions === void 0 ? void 0 : swaggerRenderOptions.customInitializer)) {
46
+ swaggerRenderOptions?.customInitializer) {
47
47
  return { ext: 'js', content: swaggerRenderOptions.customInitializer };
48
48
  }
49
49
  if (lastName === 'index.html' || lastName === 'swagger-initializer.js') {
@@ -71,7 +71,7 @@ function renderJSON(swaggerConfig, swaggerExplorer) {
71
71
  exports.renderJSON = renderJSON;
72
72
  function renderSwaggerUIRemote(swaggerConfig, swaggerExplorer) {
73
73
  const swaggerRenderOptions = swaggerConfig.swaggerUIRenderOptions || {};
74
- const indexPagePath = (swaggerRenderOptions === null || swaggerRenderOptions === void 0 ? void 0 : swaggerRenderOptions.indexPagePath) || (0, path_1.join)(__dirname, '../../index.html');
74
+ const indexPagePath = swaggerRenderOptions?.indexPagePath || (0, path_1.join)(__dirname, '../../index.html');
75
75
  return async (pathname) => {
76
76
  if (pathname.indexOf(swaggerConfig.swaggerPath) === -1) {
77
77
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.20.6",
3
+ "version": "4.0.0-beta.1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -11,22 +11,23 @@
11
11
  ],
12
12
  "devDependencies": {
13
13
  "@apidevtools/swagger-parser": "11.0.1",
14
- "@midwayjs/core": "^3.20.4",
15
- "@midwayjs/koa": "^3.20.5",
16
- "@midwayjs/mock": "^3.20.4",
17
- "@midwayjs/validate": "^3.20.5",
14
+ "@midwayjs/core": "^4.0.0-beta.1",
15
+ "@midwayjs/koa": "^4.0.0-beta.1",
16
+ "@midwayjs/mock": "^4.0.0-beta.1",
17
+ "@midwayjs/validate": "^4.0.0-beta.1",
18
18
  "swagger-ui-dist": "5.18.3"
19
19
  },
20
20
  "author": "Kurten Chan <chinkurten@gmail.com>",
21
21
  "license": "MIT",
22
22
  "scripts": {
23
23
  "build": "tsc",
24
- "test": "../../node_modules/.bin/jest --runInBand",
25
- "cov": "../../node_modules/.bin/jest --runInBand --coverage --forceExit"
24
+ "test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
25
+ "cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
26
+ "ci": "npm run test"
26
27
  },
27
28
  "repository": {
28
29
  "type": "git",
29
30
  "url": "https://github.com/midwayjs/midway.git"
30
31
  },
31
- "gitHead": "3bfa0c5d058250a16f4ef304c2c0d10dc060b95c"
32
+ "gitHead": "832961ec3aff123c033197d8c00cb2bc9bad7ff8"
32
33
  }
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.