@midwayjs/swagger 3.19.2 → 4.0.0-alpha.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
  // 判断是否忽略当前路由
178
175
  const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
179
176
  item.propertyName === webRouter.method);
@@ -191,11 +188,8 @@ let SwaggerExplorer = class SwaggerExplorer {
191
188
  // 获取路由参数
192
189
  const routerArgs = metaForParams[webRouter.method] || [];
193
190
  // 过滤出主体参数
194
- const bds = routerArgs.filter(item => {
195
- var _a;
196
- return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
197
- ((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) === core_1.RouteParamTypes.BODY;
198
- });
191
+ const bds = routerArgs.filter(item => item.key === core_1.WEB_ROUTER_PARAM_KEY &&
192
+ item?.metadata?.type === core_1.RouteParamTypes.BODY);
199
193
  // 如果存在多个主体参数,则跳过当前循环,因为swagger不支持多个@Body
200
194
  if (bds.length > 1) {
201
195
  continue;
@@ -256,7 +250,6 @@ let SwaggerExplorer = class SwaggerExplorer {
256
250
  * 构造 router 提取方法
257
251
  */
258
252
  generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target) {
259
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
260
253
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
261
254
  item.propertyName === webRouter.method)[0];
262
255
  const routerTagsMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_TAGS &&
@@ -268,27 +261,24 @@ let SwaggerExplorer = class SwaggerExplorer {
268
261
  }
269
262
  const parameters = [];
270
263
  opts[webRouter.requestMethod] = {
271
- summary: getNotEmptyValue((_a = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _a === void 0 ? void 0 : _a.summary, webRouter.summary),
272
- description: getNotEmptyValue((_b = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _b === void 0 ? void 0 : _b.description, webRouter.description),
273
- operationId: ((_c = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _c === void 0 ? void 0 : _c.operationId) ||
264
+ summary: getNotEmptyValue(operMeta?.metadata?.summary, webRouter.summary),
265
+ description: getNotEmptyValue(operMeta?.metadata?.description, webRouter.description),
266
+ operationId: operMeta?.metadata?.operationId ||
274
267
  this.getOperationId(target.name, webRouter),
275
- 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 : [],
268
+ tags: routerTags.length ? routerTags : operMeta?.metadata?.tags ?? [],
276
269
  };
277
- if (((_f = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _f === void 0 ? void 0 : _f.deprecated) != null) {
270
+ if (operMeta?.metadata?.deprecated != null) {
278
271
  opts[webRouter.requestMethod].deprecated =
279
- !!((_g = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _g === void 0 ? void 0 : _g.deprecated);
272
+ !!operMeta?.metadata?.deprecated;
280
273
  }
281
274
  /**
282
275
  * [{"key":"web:router_param","parameterIndex":1,"propertyName":"create","metadata":{"type":2}},
283
276
  * {"key":"web:router_param","parameterIndex":0,"propertyName":"create","metadata":{"type":1,"propertyData":"createCatDto"}}]
284
277
  */
285
278
  // WEB_ROUTER_PARAM_KEY
286
- const args = routerArgs.filter(item => {
287
- var _a;
288
- return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
289
- ((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) !== core_1.RouteParamTypes.CUSTOM;
290
- });
291
- const types = (0, core_1.getMethodParamTypes)(target, webRouter.method);
279
+ const args = routerArgs.filter(item => item.key === core_1.WEB_ROUTER_PARAM_KEY &&
280
+ item?.metadata?.type !== core_1.RouteParamTypes.CUSTOM);
281
+ const types = core_1.MetadataManager.getMethodParamTypes(target, webRouter.method);
292
282
  const params = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_PARAMETERS &&
293
283
  item.propertyName === webRouter.method);
294
284
  // set params information from @ApiQuery() to parameters
@@ -324,9 +314,9 @@ let SwaggerExplorer = class SwaggerExplorer {
324
314
  parameters.push(p);
325
315
  }
326
316
  else if (p.in === 'body') {
327
- p.content = (_h = p.content) !== null && _h !== void 0 ? _h : {};
317
+ p.content = p.content ?? {};
328
318
  if (Object.keys(p.content).length === 0) {
329
- p.content[p.contentType || 'application/json'] = (_j = p.content[p.contentType || 'application/json']) !== null && _j !== void 0 ? _j : {
319
+ p.content[p.contentType || 'application/json'] = p.content[p.contentType || 'application/json'] ?? {
330
320
  schema: p.schema,
331
321
  };
332
322
  }
@@ -336,25 +326,25 @@ let SwaggerExplorer = class SwaggerExplorer {
336
326
  }
337
327
  // if requestBody is already set, skip
338
328
  opts[webRouter.requestMethod].requestBody =
339
- (_k = opts[webRouter.requestMethod].requestBody) !== null && _k !== void 0 ? _k : {};
329
+ opts[webRouter.requestMethod].requestBody ?? {};
340
330
  opts[webRouter.requestMethod].requestBody.description =
341
- (_l = opts[webRouter.requestMethod].requestBody.description) !== null && _l !== void 0 ? _l : p.description;
331
+ opts[webRouter.requestMethod].requestBody.description ??
332
+ p.description;
342
333
  opts[webRouter.requestMethod].requestBody.content =
343
- (_m = opts[webRouter.requestMethod].requestBody.content) !== null && _m !== void 0 ? _m : p.content;
334
+ opts[webRouter.requestMethod].requestBody.content ?? p.content;
344
335
  opts[webRouter.requestMethod].requestBody.required =
345
- (_o = opts[webRouter.requestMethod].requestBody.required) !== null && _o !== void 0 ? _o : p.required;
336
+ opts[webRouter.requestMethod].requestBody.required ?? p.required;
346
337
  }
347
338
  }
348
339
  for (const arg of args) {
349
340
  const currentType = types[arg.parameterIndex];
350
341
  const p = {
351
- name: (_p = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _p === void 0 ? void 0 : _p.propertyData,
352
- in: convertTypeToString((_q = arg.metadata) === null || _q === void 0 ? void 0 : _q.type),
342
+ name: arg?.metadata?.propertyData,
343
+ in: convertTypeToString(arg.metadata?.type),
353
344
  required: false,
354
345
  };
355
346
  const existsParam = parameters.find(item => {
356
- var _a;
357
- 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;
347
+ return item.name === arg?.metadata?.propertyData && item.in === p.in;
358
348
  });
359
349
  // if exists same param from @ApiQuery and other decorator, just skip
360
350
  if (existsParam) {
@@ -374,13 +364,13 @@ let SwaggerExplorer = class SwaggerExplorer {
374
364
  // 如果@Query()装饰的 是一个对象,则把该对象的子属性作为多个@Query参数
375
365
  const schema = this.documentBuilder.getSchema(currentType.name);
376
366
  Object.keys(schema.properties).forEach(pName => {
377
- var _a, _b;
378
367
  const pp = {
379
368
  name: pName,
380
369
  in: p.in,
381
- description: (_a = schema.properties[pName]) === null || _a === void 0 ? void 0 : _a.description,
370
+ description: schema.properties[pName]
371
+ ?.description,
382
372
  schema: schema.properties[pName],
383
- required: ((_b = schema.required) === null || _b === void 0 ? void 0 : _b.includes(pName)) || false,
373
+ required: schema.required?.includes(pName) || false,
384
374
  };
385
375
  parameters.push(pp);
386
376
  });
@@ -391,7 +381,7 @@ let SwaggerExplorer = class SwaggerExplorer {
391
381
  continue;
392
382
  }
393
383
  p.schema = {
394
- type: convertSchemaType((_r = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _r !== void 0 ? _r : currentType),
384
+ type: convertSchemaType(currentType?.name ?? currentType),
395
385
  };
396
386
  }
397
387
  }
@@ -404,7 +394,7 @@ let SwaggerExplorer = class SwaggerExplorer {
404
394
  continue;
405
395
  }
406
396
  // 这里兼容一下 @File()、@Files()、@Fields() 装饰器
407
- if (((_s = arg.metadata) === null || _s === void 0 ? void 0 : _s.type) === core_1.RouteParamTypes.FILESSTREAM) {
397
+ if (arg.metadata?.type === core_1.RouteParamTypes.FILESSTREAM) {
408
398
  p.content = {};
409
399
  p.content[_1.BodyContentType.Multipart] = {
410
400
  schema: {
@@ -422,7 +412,7 @@ let SwaggerExplorer = class SwaggerExplorer {
422
412
  },
423
413
  };
424
414
  }
425
- else if (((_t = arg.metadata) === null || _t === void 0 ? void 0 : _t.type) === core_1.RouteParamTypes.FILESTREAM) {
415
+ else if (arg.metadata?.type === core_1.RouteParamTypes.FILESTREAM) {
426
416
  p.content = {};
427
417
  p.content[_1.BodyContentType.Multipart] = {
428
418
  schema: {
@@ -452,7 +442,7 @@ let SwaggerExplorer = class SwaggerExplorer {
452
442
  p.content = {
453
443
  'text/plain': {
454
444
  schema: {
455
- type: convertSchemaType((_u = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _u !== void 0 ? _u : currentType),
445
+ type: convertSchemaType(currentType?.name ?? currentType),
456
446
  },
457
447
  },
458
448
  };
@@ -558,7 +548,7 @@ let SwaggerExplorer = class SwaggerExplorer {
558
548
  * @param clzz
559
549
  */
560
550
  parseExtraModel(clzz) {
561
- const metaForClass = (0, core_1.getClassMetadata)(constants_1.DECORATORS_CLASS_METADATA, clzz) || [];
551
+ const metaForClass = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS_CLASS_METADATA, clzz) || [];
562
552
  const extraModels = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXTRA_MODEL);
563
553
  for (const m of extraModels) {
564
554
  if (Array.isArray(m.metadata)) {
@@ -572,46 +562,45 @@ let SwaggerExplorer = class SwaggerExplorer {
572
562
  }
573
563
  }
574
564
  parseSubPropertyType(metadata) {
575
- var _a, _b, _c;
576
565
  let typeMeta;
577
- if (metadata === null || metadata === void 0 ? void 0 : metadata.enum) {
566
+ if (metadata?.enum) {
578
567
  typeMeta = {
579
- type: metadata === null || metadata === void 0 ? void 0 : metadata.type,
580
- enum: metadata === null || metadata === void 0 ? void 0 : metadata.enum,
581
- default: metadata === null || metadata === void 0 ? void 0 : metadata.default,
568
+ type: metadata?.type,
569
+ enum: metadata?.enum,
570
+ default: metadata?.default,
582
571
  };
583
- if (metadata === null || metadata === void 0 ? void 0 : metadata.description) {
584
- typeMeta.description = metadata === null || metadata === void 0 ? void 0 : metadata.description;
572
+ if (metadata?.description) {
573
+ typeMeta.description = metadata?.description;
585
574
  }
586
575
  return typeMeta;
587
576
  }
588
- if ((_a = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _a === void 0 ? void 0 : _a.enum) {
577
+ if (metadata?.items?.enum) {
589
578
  typeMeta = {
590
- type: metadata === null || metadata === void 0 ? void 0 : metadata.type,
591
- items: metadata === null || metadata === void 0 ? void 0 : metadata.items,
592
- default: metadata === null || metadata === void 0 ? void 0 : metadata.default,
579
+ type: metadata?.type,
580
+ items: metadata?.items,
581
+ default: metadata?.default,
593
582
  };
594
- if (metadata === null || metadata === void 0 ? void 0 : metadata.description) {
595
- typeMeta.description = metadata === null || metadata === void 0 ? void 0 : metadata.description;
583
+ if (metadata?.description) {
584
+ typeMeta.description = metadata?.description;
596
585
  }
597
586
  return typeMeta;
598
587
  }
599
588
  let isArray = false;
600
- let currentType = parseTypeSchema(metadata === null || metadata === void 0 ? void 0 : metadata.type);
601
- metadata === null || metadata === void 0 ? true : delete metadata.type;
589
+ let currentType = parseTypeSchema(metadata?.type);
590
+ delete metadata?.type;
602
591
  if (currentType === 'array') {
603
592
  isArray = true;
604
- currentType = parseTypeSchema((_b = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _b === void 0 ? void 0 : _b.type);
605
- metadata === null || metadata === void 0 ? true : delete metadata.items.type;
593
+ currentType = parseTypeSchema(metadata?.items?.type);
594
+ delete metadata?.items.type;
606
595
  }
607
- if (metadata === null || metadata === void 0 ? void 0 : metadata.oneOf) {
596
+ if (metadata?.oneOf) {
608
597
  typeMeta = {
609
598
  oneOf: [],
610
599
  };
611
- metadata === null || metadata === void 0 ? void 0 : metadata.oneOf.forEach((item) => {
600
+ metadata?.oneOf.forEach((item) => {
612
601
  typeMeta.push(this.parseSubPropertyType(item));
613
602
  });
614
- metadata === null || metadata === void 0 ? true : delete metadata.oneOf;
603
+ delete metadata?.oneOf;
615
604
  }
616
605
  if (core_1.Types.isClass(currentType)) {
617
606
  this.parseClzz(currentType);
@@ -619,13 +608,13 @@ let SwaggerExplorer = class SwaggerExplorer {
619
608
  typeMeta = {
620
609
  type: 'array',
621
610
  items: {
622
- $ref: '#/components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
611
+ $ref: '#/components/schemas/' + currentType?.name,
623
612
  },
624
613
  };
625
614
  }
626
615
  else {
627
616
  typeMeta = {
628
- $ref: '#/components/schemas/' + (currentType === null || currentType === void 0 ? void 0 : currentType.name),
617
+ $ref: '#/components/schemas/' + currentType?.name,
629
618
  };
630
619
  }
631
620
  delete metadata.items;
@@ -634,19 +623,19 @@ let SwaggerExplorer = class SwaggerExplorer {
634
623
  if (isArray) {
635
624
  // 没有配置类型则认为自己配置了 items 内容
636
625
  if (!currentType) {
637
- if ((_c = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _c === void 0 ? void 0 : _c['$ref']) {
626
+ if (metadata?.items?.['$ref']) {
638
627
  metadata.items['$ref'] = parseTypeSchema(metadata.items['$ref']);
639
628
  }
640
629
  typeMeta = {
641
630
  type: 'array',
642
- items: metadata === null || metadata === void 0 ? void 0 : metadata.items,
631
+ items: metadata?.items,
643
632
  };
644
633
  }
645
634
  else {
646
635
  typeMeta = {
647
636
  type: 'array',
648
637
  items: {
649
- type: convertSchemaType((currentType === null || currentType === void 0 ? void 0 : currentType.name) || currentType),
638
+ type: convertSchemaType(currentType?.name || currentType),
650
639
  },
651
640
  };
652
641
  }
@@ -655,7 +644,7 @@ let SwaggerExplorer = class SwaggerExplorer {
655
644
  else {
656
645
  typeMeta = {
657
646
  type: currentType,
658
- format: metadata === null || metadata === void 0 ? void 0 : metadata.format,
647
+ format: metadata?.format,
659
648
  };
660
649
  // Date 类型支持
661
650
  if (typeMeta.type === 'Date') {
@@ -773,7 +762,11 @@ let SwaggerExplorer = class SwaggerExplorer {
773
762
  this.parseExtraModel(clzz);
774
763
  // 解析类上的 ApiProperty
775
764
  // TODO 这里后面不能用这个方法
776
- const props = (0, core_1.getClassExtendedMetadata)(core_1.INJECT_CUSTOM_PROPERTY, clzz);
765
+ const props = core_1.MetadataManager.getPropertiesWithMetadata(core_1.CUSTOM_PROPERTY_INJECT_KEY, clzz) || {};
766
+ // 这里属性值唯一,取数组最后一个
767
+ for (const key in props) {
768
+ props[key] = props[key][props[key].length - 1];
769
+ }
777
770
  const tt = {
778
771
  type: 'object',
779
772
  properties: {},
@@ -787,7 +780,7 @@ let SwaggerExplorer = class SwaggerExplorer {
787
780
  const metadata = props[key].metadata || {};
788
781
  if (!metadata.type) {
789
782
  // 推导类型
790
- metadata.type = (0, core_1.getPropertyType)(clzz.prototype, key).name;
783
+ metadata.type = core_1.MetadataManager.transformTypeFromTSDesign(core_1.MetadataManager.getPropertyType(clzz.prototype, key)).name;
791
784
  }
792
785
  tt.properties[key] = tt.properties[key] || {};
793
786
  // loop metadata
@@ -897,12 +890,13 @@ let SwaggerExplorer = class SwaggerExplorer {
897
890
  break;
898
891
  case 'custom':
899
892
  {
900
- this.documentBuilder.addSecurity(opts === null || opts === void 0 ? void 0 : opts.name, opts);
893
+ this.documentBuilder.addSecurity(opts?.name, opts);
901
894
  }
902
895
  break;
903
896
  }
904
897
  }
905
898
  };
899
+ exports.SwaggerExplorer = SwaggerExplorer;
906
900
  __decorate([
907
901
  (0, core_1.Config)('swagger'),
908
902
  __metadata("design:type", Object)
@@ -913,11 +907,10 @@ __decorate([
913
907
  __metadata("design:paramtypes", []),
914
908
  __metadata("design:returntype", Promise)
915
909
  ], SwaggerExplorer.prototype, "init", null);
916
- SwaggerExplorer = __decorate([
910
+ exports.SwaggerExplorer = SwaggerExplorer = __decorate([
917
911
  (0, core_1.Provide)(),
918
912
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
919
913
  ], SwaggerExplorer);
920
- exports.SwaggerExplorer = SwaggerExplorer;
921
914
  /**
922
915
  * 解释路由上的参数
923
916
  * @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.19.2",
3
+ "version": "4.0.0-alpha.1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -10,22 +10,23 @@
10
10
  "index.html"
11
11
  ],
12
12
  "devDependencies": {
13
- "@midwayjs/core": "^3.19.0",
14
- "@midwayjs/koa": "^3.19.2",
15
- "@midwayjs/mock": "^3.19.2",
16
- "@midwayjs/validate": "^3.19.2",
13
+ "@midwayjs/core": "^4.0.0-alpha.1",
14
+ "@midwayjs/koa": "^4.0.0-alpha.1",
15
+ "@midwayjs/mock": "^4.0.0-alpha.1",
16
+ "@midwayjs/validate": "^4.0.0-alpha.1",
17
17
  "swagger-ui-dist": "5.18.2"
18
18
  },
19
19
  "author": "Kurten Chan <chinkurten@gmail.com>",
20
20
  "license": "MIT",
21
21
  "scripts": {
22
22
  "build": "tsc",
23
- "test": "../../node_modules/.bin/jest --runInBand",
24
- "cov": "../../node_modules/.bin/jest --runInBand --coverage --forceExit"
23
+ "test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
24
+ "cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
25
+ "ci": "npm run test"
25
26
  },
26
27
  "repository": {
27
28
  "type": "git",
28
29
  "url": "https://github.com/midwayjs/midway.git"
29
30
  },
30
- "gitHead": "57fd034be94897fb819b0d9ef776de0b9923ab0f"
31
+ "gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
31
32
  }