@midwayjs/swagger 3.13.10-beta.2 → 3.14.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
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.
@@ -2,12 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.swagger = void 0;
4
4
  const render_1 = require("../ui/render");
5
+ const core_1 = require("@midwayjs/core");
6
+ const swaggerDist = (0, core_1.safeRequire)('swagger-ui-dist');
5
7
  exports.swagger = {
6
8
  title: 'My Project',
7
9
  description: 'This is a swagger-ui for midwayjs project',
8
10
  version: '1.0.0',
9
11
  swaggerPath: '/swagger-ui',
10
- swaggerUIRender: render_1.renderSwaggerUIRemote,
12
+ swaggerUIRender: swaggerDist && swaggerDist.getAbsoluteFSPath
13
+ ? render_1.renderSwaggerUIDist
14
+ : render_1.renderSwaggerUIRemote,
11
15
  swaggerUIRenderOptions: {},
12
16
  };
13
17
  //# sourceMappingURL=config.default.js.map
@@ -68,21 +68,27 @@ class DocumentBuilder {
68
68
  return undefined;
69
69
  }
70
70
  addTag(name, description = '', externalDocs) {
71
+ const tags = this.document.tags || [];
71
72
  if (Array.isArray(name)) {
72
73
  const arr = name;
73
74
  for (const s of arr) {
74
- this.document.tags.push({
75
- name: s,
76
- description: '',
77
- });
75
+ if (!tags.find(tag => tag.name === name)) {
76
+ tags.push({
77
+ name: s,
78
+ description: '',
79
+ });
80
+ }
78
81
  }
79
82
  return this;
80
83
  }
81
- this.document.tags.push({
82
- name,
83
- description,
84
- externalDocs,
85
- });
84
+ if (!tags.find(tag => tag.name === name)) {
85
+ tags.push({
86
+ name,
87
+ description,
88
+ externalDocs,
89
+ });
90
+ }
91
+ this.document.tags = tags;
86
92
  return this;
87
93
  }
88
94
  addSecurity(name, options) {
package/dist/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export { SwaggerConfiguration as Configuration } from './configuration';
4
4
  export { SwaggerExplorer } from './swaggerExplorer';
5
5
  export { SwaggerMiddleware } from './swaggerMiddleware';
6
6
  export { renderSwaggerUIRemote, renderSwaggerUIDist, renderJSON, } from './ui/render';
7
+ export * from './constants';
7
8
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -27,4 +27,5 @@ var render_1 = require("./ui/render");
27
27
  Object.defineProperty(exports, "renderSwaggerUIRemote", { enumerable: true, get: function () { return render_1.renderSwaggerUIRemote; } });
28
28
  Object.defineProperty(exports, "renderSwaggerUIDist", { enumerable: true, get: function () { return render_1.renderSwaggerUIDist; } });
29
29
  Object.defineProperty(exports, "renderJSON", { enumerable: true, get: function () { return render_1.renderJSON; } });
30
+ __exportStar(require("./constants"), exports);
30
31
  //# sourceMappingURL=index.js.map
@@ -182,7 +182,7 @@ let SwaggerExplorer = class SwaggerExplorer {
182
182
  * 构造 router 提取方法
183
183
  */
184
184
  generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target) {
185
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
185
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
186
186
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
187
187
  item.propertyName === webRouter.method)[0];
188
188
  let opts = paths[url];
@@ -196,6 +196,10 @@ let SwaggerExplorer = class SwaggerExplorer {
196
196
  operationId: this.getOperationId(target.name, webRouter),
197
197
  tags: ((_c = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _c === void 0 ? void 0 : _c.tags) || [],
198
198
  };
199
+ if (((_d = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _d === void 0 ? void 0 : _d.deprecated) != null) {
200
+ opts[webRouter.requestMethod].deprecated =
201
+ !!((_e = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _e === void 0 ? void 0 : _e.deprecated);
202
+ }
199
203
  /**
200
204
  * [{"key":"web:router_param","parameterIndex":1,"propertyName":"create","metadata":{"type":2}},
201
205
  * {"key":"web:router_param","parameterIndex":0,"propertyName":"create","metadata":{"type":1,"propertyData":"createCatDto"}}]
@@ -208,8 +212,8 @@ let SwaggerExplorer = class SwaggerExplorer {
208
212
  for (const arg of args) {
209
213
  const currentType = types[arg.parameterIndex];
210
214
  const p = {
211
- name: (_e = (_d = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _d === void 0 ? void 0 : _d.propertyData) !== null && _e !== void 0 ? _e : '',
212
- in: convertTypeToString((_f = arg.metadata) === null || _f === void 0 ? void 0 : _f.type),
215
+ name: (_g = (_f = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _f === void 0 ? void 0 : _f.propertyData) !== null && _g !== void 0 ? _g : '',
216
+ in: convertTypeToString((_h = arg.metadata) === null || _h === void 0 ? void 0 : _h.type),
213
217
  required: false,
214
218
  };
215
219
  if (p.in === 'path') {
@@ -242,7 +246,7 @@ let SwaggerExplorer = class SwaggerExplorer {
242
246
  }
243
247
  else {
244
248
  p.schema = {
245
- type: convertSchemaType((_g = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _g !== void 0 ? _g : currentType),
249
+ type: convertSchemaType((_j = currentType === null || currentType === void 0 ? void 0 : currentType.name) !== null && _j !== void 0 ? _j : currentType),
246
250
  };
247
251
  }
248
252
  this.parseFromParamsToP(params[params.length - 1 - arg.parameterIndex], p);
@@ -251,7 +255,7 @@ let SwaggerExplorer = class SwaggerExplorer {
251
255
  continue;
252
256
  }
253
257
  // 这里兼容一下 @File()、@Files()、@Fields() 装饰器
254
- if (((_h = arg.metadata) === null || _h === void 0 ? void 0 : _h.type) === core_1.RouteParamTypes.FILESSTREAM) {
258
+ if (((_k = arg.metadata) === null || _k === void 0 ? void 0 : _k.type) === core_1.RouteParamTypes.FILESSTREAM) {
255
259
  p.schema = {
256
260
  type: 'object',
257
261
  properties: {
@@ -267,7 +271,7 @@ let SwaggerExplorer = class SwaggerExplorer {
267
271
  };
268
272
  p.contentType = _1.BodyContentType.Multipart;
269
273
  }
270
- if (((_j = arg.metadata) === null || _j === void 0 ? void 0 : _j.type) === core_1.RouteParamTypes.FILESTREAM) {
274
+ if (((_l = arg.metadata) === null || _l === void 0 ? void 0 : _l.type) === core_1.RouteParamTypes.FILESTREAM) {
271
275
  p.schema = {
272
276
  type: 'object',
273
277
  properties: {
@@ -280,7 +284,7 @@ let SwaggerExplorer = class SwaggerExplorer {
280
284
  };
281
285
  p.contentType = _1.BodyContentType.Multipart;
282
286
  }
283
- if (((_k = arg.metadata) === null || _k === void 0 ? void 0 : _k.type) === core_1.RouteParamTypes.FIELDS) {
287
+ if (((_m = arg.metadata) === null || _m === void 0 ? void 0 : _m.type) === core_1.RouteParamTypes.FIELDS) {
284
288
  this.expandSchemaRef(p);
285
289
  p.contentType = _1.BodyContentType.Multipart;
286
290
  }
@@ -300,8 +304,8 @@ let SwaggerExplorer = class SwaggerExplorer {
300
304
  }
301
305
  else {
302
306
  // 这里拼 schema properties 时肯定存在
303
- Object.assign({}, (_l = opts[webRouter.requestMethod].requestBody.content[p.contentType]
304
- .schema) === null || _l === void 0 ? void 0 : _l.properties, p.schema.properties);
307
+ Object.assign({}, (_o = opts[webRouter.requestMethod].requestBody.content[p.contentType]
308
+ .schema) === null || _o === void 0 ? void 0 : _o.properties, p.schema.properties);
305
309
  }
306
310
  delete p.contentType;
307
311
  delete p.content;
@@ -1,6 +1,9 @@
1
+ /// <reference types="node" />
1
2
  import type { SwaggerOptions } from '../interfaces';
2
3
  import { SwaggerExplorer } from '../swaggerExplorer';
3
- export declare function renderSwaggerUIDist(swaggerConfig: SwaggerOptions, swaggerExplorer: SwaggerExplorer): (pathname: string) => Promise<{
4
+ export declare function renderSwaggerUIDist(swaggerConfig: SwaggerOptions, swaggerExplorer: SwaggerExplorer, swaggerRenderOptions?: {
5
+ customInitializer?: Buffer | string;
6
+ }): (pathname: string) => Promise<{
4
7
  ext: string;
5
8
  content: any;
6
9
  }>;
@@ -8,7 +11,9 @@ export declare function renderJSON(swaggerConfig: SwaggerOptions, swaggerExplore
8
11
  ext: string;
9
12
  content: Omit<import("../interfaces").OpenAPIObject, "paths">;
10
13
  }>;
11
- export declare function renderSwaggerUIRemote(swaggerConfig: SwaggerOptions, swaggerExplorer: SwaggerExplorer, swaggerRenderOptions: any): (pathname: string) => Promise<{
14
+ export declare function renderSwaggerUIRemote(swaggerConfig: SwaggerOptions, swaggerExplorer: SwaggerExplorer, swaggerRenderOptions?: {
15
+ indexPagePath?: string;
16
+ }): (pathname: string) => Promise<{
12
17
  ext: string;
13
18
  content: Omit<import("../interfaces").OpenAPIObject, "paths">;
14
19
  } | {
package/dist/ui/render.js CHANGED
@@ -4,11 +4,20 @@ exports.renderSwaggerUIRemote = exports.renderJSON = exports.renderSwaggerUIDist
4
4
  const core_1 = require("@midwayjs/core");
5
5
  const fs_1 = require("fs");
6
6
  const path_1 = require("path");
7
- function renderSwaggerUIDist(swaggerConfig, swaggerExplorer) {
7
+ function renderSwaggerUIDist(swaggerConfig, swaggerExplorer, swaggerRenderOptions) {
8
8
  const { getAbsoluteFSPath } = (0, core_1.safeRequire)('swagger-ui-dist');
9
9
  if (!getAbsoluteFSPath) {
10
10
  throw new core_1.MidwayCommonError('swagger-ui-dist is not installed');
11
11
  }
12
+ swaggerRenderOptions = swaggerRenderOptions || {};
13
+ if (swaggerRenderOptions.customInitializer) {
14
+ if ((0, path_1.isAbsolute)(swaggerRenderOptions.customInitializer)) {
15
+ swaggerRenderOptions.customInitializer = (0, fs_1.readFileSync)(swaggerRenderOptions.customInitializer);
16
+ }
17
+ else {
18
+ throw new core_1.MidwayInvalidConfigPropertyError('swagger.swaggerRenderOptions.customInitializer', ['Buffer', 'String']);
19
+ }
20
+ }
12
21
  function replaceInfo(content) {
13
22
  let str = `location.href.replace('${swaggerConfig.swaggerPath}/index.html', '${swaggerConfig.swaggerPath}/index.json'),\n validatorUrl: null,`;
14
23
  if (swaggerConfig.displayOptions) {
@@ -33,6 +42,10 @@ function renderSwaggerUIDist(swaggerConfig, swaggerExplorer) {
33
42
  lastName = 'index.html';
34
43
  }
35
44
  let content = (0, fs_1.readFileSync)((0, path_1.join)(swaggerUiAssetPath, lastName));
45
+ if (lastName === 'swagger-initializer.js' &&
46
+ (swaggerRenderOptions === null || swaggerRenderOptions === void 0 ? void 0 : swaggerRenderOptions.customInitializer)) {
47
+ return { ext: 'js', content: swaggerRenderOptions.customInitializer };
48
+ }
36
49
  if (lastName === 'index.html' || lastName === 'swagger-initializer.js') {
37
50
  content = content.toString('utf8');
38
51
  content = replaceInfo(content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.13.10-beta.2",
3
+ "version": "3.14.1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -10,10 +10,10 @@
10
10
  "index.html"
11
11
  ],
12
12
  "devDependencies": {
13
- "@midwayjs/core": "^3.13.7",
14
- "@midwayjs/koa": "^3.13.9",
15
- "@midwayjs/mock": "^3.13.9",
16
- "@midwayjs/validate": "^3.13.9",
13
+ "@midwayjs/core": "^3.14.0",
14
+ "@midwayjs/koa": "^3.14.0",
15
+ "@midwayjs/mock": "^3.14.0",
16
+ "@midwayjs/validate": "^3.14.0",
17
17
  "swagger-ui-dist": "4.19.1"
18
18
  },
19
19
  "author": "Kurten Chan <chinkurten@gmail.com>",
@@ -27,5 +27,6 @@
27
27
  "repository": {
28
28
  "type": "git",
29
29
  "url": "https://github.com/midwayjs/midway.git"
30
- }
30
+ },
31
+ "gitHead": "74e08b9ee946e1aad1197c7d23f6b3165393a4a2"
31
32
  }
package/dist/ui.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { SwaggerOptions } from './interfaces';
2
- export declare function renderSwaggerUI(swaggerConfig: SwaggerOptions): (pathname: string) => Promise<{
3
- ext: string;
4
- content: any;
5
- }>;
6
- export declare function renderJSON(swaggerConfig: SwaggerOptions): (pathname: string) => Promise<{
7
- ext: string;
8
- content: any;
9
- }>;
10
- //# sourceMappingURL=ui.d.ts.map
package/dist/ui.js DELETED
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.renderJSON = exports.renderSwaggerUI = void 0;
4
- const core_1 = require("@midwayjs/core");
5
- const fs_1 = require("fs");
6
- const path_1 = require("path");
7
- function renderSwaggerUI(swaggerConfig) {
8
- const { getAbsoluteFSPath } = (0, core_1.safeRequire)('swagger-ui-dist');
9
- if (!getAbsoluteFSPath) {
10
- throw new core_1.MidwayCommonError('swagger-ui-dist is not installed');
11
- }
12
- function replaceInfo(content) {
13
- let str = `location.href.replace('${this.swaggerConfig.swaggerPath}/index.html', '${this.swaggerConfig.swaggerPath}/index.json'),\n validatorUrl: null,`;
14
- if (this.swaggerConfig.displayOptions) {
15
- Object.keys(this.swaggerConfig.displayOptions).forEach(key => {
16
- str += `\n${key}: ${this.swaggerConfig.displayOptions[key]},`;
17
- });
18
- }
19
- return content.replace('"https://petstore.swagger.io/v2/swagger.json",', str);
20
- }
21
- const swaggerUiAssetPath = getAbsoluteFSPath();
22
- return async (pathname) => {
23
- if (!swaggerUiAssetPath ||
24
- pathname.indexOf(swaggerConfig.swaggerPath) === -1) {
25
- return;
26
- }
27
- const arr = pathname.split('/');
28
- let lastName = arr.pop();
29
- if (lastName === 'index.json') {
30
- return { ext: 'json', content: this.swaggerExplorer.getData() };
31
- }
32
- if (!lastName) {
33
- lastName = 'index.html';
34
- }
35
- let content = (0, fs_1.readFileSync)((0, path_1.join)(swaggerUiAssetPath, lastName));
36
- if (lastName === 'index.html' || lastName === 'swagger-initializer.js') {
37
- content = content.toString('utf8');
38
- content = replaceInfo(content);
39
- }
40
- const ext = (0, path_1.extname)(lastName);
41
- return { ext, content };
42
- };
43
- }
44
- exports.renderSwaggerUI = renderSwaggerUI;
45
- function renderJSON(swaggerConfig) {
46
- return async (pathname) => {
47
- if (pathname.indexOf(swaggerConfig.swaggerPath) === -1) {
48
- return;
49
- }
50
- if (pathname === 'index.json') {
51
- return { ext: 'json', content: this.swaggerExplorer.getData() };
52
- }
53
- return;
54
- };
55
- }
56
- exports.renderJSON = renderJSON;
57
- //# sourceMappingURL=ui.js.map