@midwayjs/swagger 1.3.0 → 3.0.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +11 -83
  2. package/LICENSE +21 -0
  3. package/README.md +8 -22
  4. package/dist/common/enum.utils.d.ts +9 -0
  5. package/dist/common/enum.utils.js +63 -0
  6. package/dist/common/httpStatus.d.ts +51 -0
  7. package/dist/common/httpStatus.js +55 -0
  8. package/dist/config/config.default.d.ts +2 -8
  9. package/dist/config/config.default.js +6 -6
  10. package/dist/configuration.d.ts +4 -2
  11. package/dist/configuration.js +27 -11
  12. package/dist/constants.d.ts +15 -0
  13. package/dist/constants.js +18 -0
  14. package/dist/decorators/api-basic.decorator.d.ts +2 -0
  15. package/dist/decorators/api-basic.decorator.js +9 -0
  16. package/dist/decorators/api-bearer.decorator.d.ts +2 -0
  17. package/dist/decorators/api-bearer.decorator.js +9 -0
  18. package/dist/decorators/api-body.decorator.d.ts +24 -0
  19. package/dist/decorators/api-body.decorator.js +35 -0
  20. package/dist/decorators/api-cookie.decorator.d.ts +2 -0
  21. package/dist/decorators/api-cookie.decorator.js +9 -0
  22. package/dist/decorators/api-exclude-controller.decorator.d.ts +2 -0
  23. package/dist/decorators/api-exclude-controller.decorator.js +10 -0
  24. package/dist/decorators/api-exclude-endpoint.decorator.d.ts +2 -0
  25. package/dist/decorators/api-exclude-endpoint.decorator.js +12 -0
  26. package/dist/decorators/api-extension.decorator.d.ts +2 -0
  27. package/dist/decorators/api-extension.decorator.js +16 -0
  28. package/dist/decorators/api-header.decorator.d.ts +7 -0
  29. package/dist/decorators/api-header.decorator.js +43 -0
  30. package/dist/decorators/api-oauth2.decorator.d.ts +2 -0
  31. package/dist/decorators/api-oauth2.decorator.js +9 -0
  32. package/dist/decorators/api-operation.decorator.d.ts +4 -0
  33. package/dist/decorators/api-operation.decorator.js +16 -0
  34. package/dist/decorators/api-param.decorator.d.ts +15 -0
  35. package/dist/decorators/api-param.decorator.js +30 -0
  36. package/dist/decorators/api-property.decorator.d.ts +11 -0
  37. package/dist/decorators/api-property.decorator.js +55 -0
  38. package/dist/decorators/api-query.decorator.d.ts +17 -0
  39. package/dist/decorators/api-query.decorator.js +31 -0
  40. package/dist/decorators/api-response.decorator.d.ts +41 -0
  41. package/dist/decorators/api-response.decorator.js +149 -0
  42. package/dist/decorators/api-security.decorator.d.ts +3 -0
  43. package/dist/decorators/api-security.decorator.js +17 -0
  44. package/dist/decorators/api-tags.decorator.d.ts +2 -0
  45. package/dist/decorators/api-tags.decorator.js +10 -0
  46. package/dist/decorators/helpers.d.ts +7 -0
  47. package/dist/decorators/helpers.js +41 -0
  48. package/dist/decorators/index.d.ts +18 -0
  49. package/dist/decorators/index.js +35 -0
  50. package/dist/documentBuilder.d.ts +25 -0
  51. package/dist/documentBuilder.js +192 -0
  52. package/dist/index.d.ts +5 -5
  53. package/dist/index.js +8 -6
  54. package/dist/interfaces/index.d.ts +339 -0
  55. package/dist/interfaces/index.js +7 -0
  56. package/dist/swaggerExplorer.d.ts +30 -0
  57. package/dist/swaggerExplorer.js +601 -0
  58. package/dist/swaggerMiddleware.d.ts +10 -0
  59. package/dist/swaggerMiddleware.js +126 -0
  60. package/index.d.ts +8 -0
  61. package/package.json +11 -10
  62. package/dist/controller/swagger.d.ts +0 -31
  63. package/dist/controller/swagger.js +0 -96
  64. package/dist/interface.d.ts +0 -16
  65. package/dist/interface.js +0 -3
  66. package/dist/lib/createAPI.d.ts +0 -52
  67. package/dist/lib/createAPI.js +0 -276
  68. package/dist/lib/document.d.ts +0 -117
  69. package/dist/lib/document.js +0 -131
  70. package/dist/lib/generator.d.ts +0 -31
  71. package/dist/lib/generator.js +0 -310
  72. package/dist/service/generator.d.ts +0 -24
  73. package/dist/service/generator.js +0 -37
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SwaggerMiddleware = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ const decorator_1 = require("@midwayjs/decorator");
15
+ const fs_1 = require("fs");
16
+ const path_1 = require("path");
17
+ const swaggerExplorer_1 = require("./swaggerExplorer");
18
+ let SwaggerMiddleware = class SwaggerMiddleware {
19
+ async init() {
20
+ const { getAbsoluteFSPath } = (0, core_1.safeRequire)('swagger-ui-dist');
21
+ if (getAbsoluteFSPath) {
22
+ this.swaggerUiAssetPath = getAbsoluteFSPath();
23
+ }
24
+ }
25
+ resolve(app) {
26
+ if (app.getFrameworkType() === decorator_1.MidwayFrameworkType.WEB_EXPRESS) {
27
+ return async (req, res, next) => {
28
+ const pathname = req.path;
29
+ if (!this.swaggerUiAssetPath ||
30
+ pathname.indexOf(this.swaggerConfig.swaggerPath) === -1) {
31
+ return next();
32
+ }
33
+ const arr = pathname.split('/');
34
+ let lastName = arr.pop();
35
+ if (lastName === 'index.json') {
36
+ res.send(this.swaggerExplorer.getData());
37
+ return;
38
+ }
39
+ if (!lastName) {
40
+ lastName = 'index.html';
41
+ }
42
+ let content = (0, fs_1.readFileSync)((0, path_1.join)(this.swaggerUiAssetPath, lastName), {
43
+ encoding: 'utf-8',
44
+ });
45
+ if (lastName === 'index.html') {
46
+ content = content.replace('"https://petstore.swagger.io/v2/swagger.json"', `location.href.replace('${this.swaggerConfig.swaggerPath}/index.html', '${this.swaggerConfig.swaggerPath}/index.json')`);
47
+ }
48
+ const ext = (0, path_1.extname)(lastName);
49
+ if (ext === '.js') {
50
+ res.type('application/javascript');
51
+ }
52
+ else if (ext === '.map') {
53
+ res.type('application/json');
54
+ }
55
+ else if (ext === '.css') {
56
+ res.type('text/css');
57
+ }
58
+ else if (ext === '.png') {
59
+ res.type('image/png');
60
+ }
61
+ res.send(content);
62
+ };
63
+ }
64
+ else {
65
+ return async (ctx, next) => {
66
+ const pathname = ctx.path;
67
+ if (!this.swaggerUiAssetPath ||
68
+ pathname.indexOf(this.swaggerConfig.swaggerPath) === -1) {
69
+ return next();
70
+ }
71
+ const arr = pathname.split('/');
72
+ let lastName = arr.pop();
73
+ if (lastName === 'index.json') {
74
+ ctx.body = this.swaggerExplorer.getData();
75
+ return;
76
+ }
77
+ if (!lastName) {
78
+ lastName = 'index.html';
79
+ }
80
+ let content = (0, fs_1.readFileSync)((0, path_1.join)(this.swaggerUiAssetPath, lastName), {
81
+ encoding: 'utf-8',
82
+ });
83
+ if (lastName === 'index.html') {
84
+ content = content.replace('"https://petstore.swagger.io/v2/swagger.json"', `location.href.replace('${this.swaggerConfig.swaggerPath}/index.html', '${this.swaggerConfig.swaggerPath}/index.json')`);
85
+ }
86
+ const ext = (0, path_1.extname)(lastName);
87
+ if (ext === '.js') {
88
+ ctx.set('Content-Type', 'application/javascript');
89
+ }
90
+ else if (ext === '.map') {
91
+ ctx.set('Content-Type', 'application/json');
92
+ }
93
+ else if (ext === '.css') {
94
+ ctx.set('Content-Type', 'text/css');
95
+ }
96
+ else if (ext === '.png') {
97
+ ctx.set('Content-Type', 'image/png');
98
+ }
99
+ ctx.body = content;
100
+ };
101
+ }
102
+ }
103
+ static getName() {
104
+ return 'swagger';
105
+ }
106
+ };
107
+ __decorate([
108
+ (0, decorator_1.Config)('swagger'),
109
+ __metadata("design:type", Object)
110
+ ], SwaggerMiddleware.prototype, "swaggerConfig", void 0);
111
+ __decorate([
112
+ (0, decorator_1.Inject)(),
113
+ __metadata("design:type", swaggerExplorer_1.SwaggerExplorer)
114
+ ], SwaggerMiddleware.prototype, "swaggerExplorer", void 0);
115
+ __decorate([
116
+ (0, decorator_1.Init)(),
117
+ __metadata("design:type", Function),
118
+ __metadata("design:paramtypes", []),
119
+ __metadata("design:returntype", Promise)
120
+ ], SwaggerMiddleware.prototype, "init", null);
121
+ SwaggerMiddleware = __decorate([
122
+ (0, decorator_1.Provide)(),
123
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
124
+ ], SwaggerMiddleware);
125
+ exports.SwaggerMiddleware = SwaggerMiddleware;
126
+ //# sourceMappingURL=swaggerMiddleware.js.map
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { SwaggerOptions } from './dist/index';
2
+ export * from './dist/index';
3
+
4
+ declare module '@midwayjs/core/dist/interface' {
5
+ interface MidwayConfig {
6
+ swagger: Partial<SwaggerOptions>;
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,20 +1,21 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "1.3.0",
3
+ "version": "3.0.0-beta.13",
4
4
  "main": "dist/index",
5
- "typings": "dist/index.d.ts",
5
+ "typings": "index.d.ts",
6
6
  "files": [
7
7
  "dist/**/*.js",
8
- "dist/**/*.d.ts"
8
+ "dist/**/*.d.ts",
9
+ "index.d.ts"
9
10
  ],
10
11
  "devDependencies": {
11
- "@midwayjs/core": "^2.3.0",
12
- "@midwayjs/decorator": "^2.3.0",
13
- "@midwayjs/koa": "^2.3.0",
14
- "@midwayjs/mock": "^2.3.0",
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",
15
16
  "swagger-ui-dist": "^3.34.0"
16
17
  },
17
- "author": "Harry Chen <czy88840616@gmail.com>",
18
+ "author": "Kurten Chan <chinkurten@gmail.com>",
18
19
  "license": "MIT",
19
20
  "scripts": {
20
21
  "build": "tsc",
@@ -24,7 +25,7 @@
24
25
  },
25
26
  "repository": {
26
27
  "type": "git",
27
- "url": "http://github.com/midwayjs/midway-component.git"
28
+ "url": "https://github.com/midwayjs/midway.git"
28
29
  },
29
- "gitHead": "caf84abfd0372651dc04eb1064d15b7712710096"
30
+ "gitHead": "d3c47770fee9dce33a8d148882173fd7782864ad"
30
31
  }
@@ -1,31 +0,0 @@
1
- import { IMidwayApplication } from '@midwayjs/core';
2
- import { SwaggerGenerator } from '../service/generator';
3
- export declare class SwaggerController {
4
- swaggerUiAssetPath: string;
5
- app: IMidwayApplication;
6
- ctx: any;
7
- swaggerGenerator: SwaggerGenerator;
8
- constructor();
9
- renderJSON(): Promise<{
10
- openapi: string;
11
- info: {
12
- description: string;
13
- version: string;
14
- title: string;
15
- termsOfService: any;
16
- contact: any;
17
- license: any;
18
- };
19
- host: string;
20
- basePath: string;
21
- tags: any;
22
- schemas: string[];
23
- paths: {};
24
- components: {
25
- schemas: {};
26
- };
27
- }>;
28
- renderSwagger(fileName: string): Promise<string>;
29
- getSwaggerUIResource(requestPath: any, encoding?: 'utf-8'): string;
30
- }
31
- //# sourceMappingURL=swagger.d.ts.map
@@ -1,96 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SwaggerController = void 0;
16
- const decorator_1 = require("@midwayjs/decorator");
17
- const fs_1 = require("fs");
18
- const path_1 = require("path");
19
- const core_1 = require("@midwayjs/core");
20
- const generator_1 = require("../service/generator");
21
- let SwaggerController = class SwaggerController {
22
- constructor() {
23
- const { getAbsoluteFSPath } = (0, core_1.safeRequire)('swagger-ui-dist');
24
- if (getAbsoluteFSPath) {
25
- this.swaggerUiAssetPath = getAbsoluteFSPath();
26
- }
27
- }
28
- async renderJSON() {
29
- return this.swaggerGenerator.generate();
30
- }
31
- async renderSwagger(fileName) {
32
- if (!this.swaggerUiAssetPath) {
33
- return 'please run "npm install swagger-ui-dist" first';
34
- }
35
- if (!fileName) {
36
- fileName = '/index.html';
37
- }
38
- const resourceAbsolutePath = (0, path_1.join)(this.swaggerUiAssetPath, fileName);
39
- if ((0, path_1.extname)(fileName)) {
40
- // 7 天内使用缓存
41
- if (this.app.getFrameworkType() === core_1.MidwayFrameworkType.WEB_EXPRESS) {
42
- this.ctx.res.type((0, path_1.extname)(fileName));
43
- this.ctx.res.set('cache-control', 'public, max-age=604800');
44
- }
45
- else {
46
- this.ctx.type = (0, path_1.extname)(fileName);
47
- this.ctx.set('cache-control', 'public, max-age=604800');
48
- }
49
- }
50
- if (fileName.indexOf('index.html') !== -1) {
51
- const htmlContent = this.getSwaggerUIResource(resourceAbsolutePath, 'utf-8');
52
- return htmlContent.replace('"https://petstore.swagger.io/v2/swagger.json"', "location.href.replace('/swagger-ui/index.html', '/swagger-ui/json')");
53
- }
54
- else {
55
- return this.getSwaggerUIResource(resourceAbsolutePath);
56
- }
57
- }
58
- getSwaggerUIResource(requestPath, encoding) {
59
- return (0, fs_1.readFileSync)(requestPath, {
60
- encoding,
61
- });
62
- }
63
- };
64
- __decorate([
65
- (0, decorator_1.App)(),
66
- __metadata("design:type", Object)
67
- ], SwaggerController.prototype, "app", void 0);
68
- __decorate([
69
- (0, decorator_1.Inject)(),
70
- __metadata("design:type", Object)
71
- ], SwaggerController.prototype, "ctx", void 0);
72
- __decorate([
73
- (0, decorator_1.Inject)(),
74
- __metadata("design:type", generator_1.SwaggerGenerator)
75
- ], SwaggerController.prototype, "swaggerGenerator", void 0);
76
- __decorate([
77
- (0, decorator_1.Get)('/json'),
78
- __metadata("design:type", Function),
79
- __metadata("design:paramtypes", []),
80
- __metadata("design:returntype", Promise)
81
- ], SwaggerController.prototype, "renderJSON", null);
82
- __decorate([
83
- (0, decorator_1.Get)('/'),
84
- (0, decorator_1.Get)('/:fileName'),
85
- __param(0, (0, decorator_1.Param)()),
86
- __metadata("design:type", Function),
87
- __metadata("design:paramtypes", [String]),
88
- __metadata("design:returntype", Promise)
89
- ], SwaggerController.prototype, "renderSwagger", null);
90
- SwaggerController = __decorate([
91
- (0, decorator_1.Provide)(),
92
- (0, decorator_1.Controller)('/swagger-ui'),
93
- __metadata("design:paramtypes", [])
94
- ], SwaggerController);
95
- exports.SwaggerController = SwaggerController;
96
- //# sourceMappingURL=swagger.js.map
@@ -1,16 +0,0 @@
1
- export interface SwaggerGeneratorInfoOptions {
2
- title: string;
3
- description: string;
4
- version: string;
5
- termsOfService: string;
6
- contact: {
7
- name: string;
8
- url: string;
9
- email: string;
10
- };
11
- license: {
12
- name: string;
13
- url: string;
14
- };
15
- }
16
- //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=interface.js.map
@@ -1,52 +0,0 @@
1
- export declare const SWAGGER_DOCUMENT_KEY = "common:swagger_doc_api";
2
- export interface ApiFormat {
3
- summary: string;
4
- description: string;
5
- params: APIParamFormat[];
6
- response: APIResponseFormat[];
7
- }
8
- export interface APIParamFormat {
9
- name?: string;
10
- type?: string;
11
- description?: string;
12
- required?: boolean;
13
- deprecated?: boolean;
14
- allowEmptyValue?: boolean;
15
- example?: any;
16
- $ref?: string;
17
- }
18
- export interface APIPropertyFormat {
19
- description: string;
20
- example: any;
21
- }
22
- export interface APIResponseFormat {
23
- status: string;
24
- description: string;
25
- headers: {
26
- [name: string]: {
27
- description?: string;
28
- type?: string;
29
- };
30
- };
31
- properties: any;
32
- content: any;
33
- example: any;
34
- }
35
- export declare class SwaggerAPI {
36
- private _summary;
37
- private _description;
38
- private _params;
39
- private _response;
40
- summary(summary: string): SwaggerAPI;
41
- description(desc: string): SwaggerAPI;
42
- param(description: Partial<APIParamFormat>): SwaggerAPI;
43
- param(description: string, options?: Partial<APIParamFormat>): SwaggerAPI;
44
- respond(status: number, description?: string, respondType?: string, options?: Partial<APIResponseFormat>): SwaggerAPI;
45
- buildJSON(): ApiFormat;
46
- build(): (target: any, property: string) => void;
47
- }
48
- export declare function CreateApiDoc(): SwaggerAPI;
49
- export declare function CreateApiDoc(data: any): MethodDecorator;
50
- export declare function CreateApiPropertyDoc(description: Partial<APIPropertyFormat>): PropertyDecorator;
51
- export declare function CreateApiPropertyDoc(description: string, options?: Partial<APIPropertyFormat>): PropertyDecorator;
52
- //# sourceMappingURL=createAPI.d.ts.map
@@ -1,276 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CreateApiPropertyDoc = exports.CreateApiDoc = exports.SwaggerAPI = exports.SWAGGER_DOCUMENT_KEY = void 0;
4
- const decorator_1 = require("@midwayjs/decorator");
5
- exports.SWAGGER_DOCUMENT_KEY = 'common:swagger_doc_api';
6
- class SwaggerAPI {
7
- constructor() {
8
- this._params = [];
9
- this._response = [];
10
- }
11
- summary(summary) {
12
- this._summary = summary;
13
- return this;
14
- }
15
- description(desc) {
16
- this._description = desc;
17
- return this;
18
- }
19
- param(description, options) {
20
- if (typeof description === 'string') {
21
- this._params.push({
22
- description,
23
- ...options,
24
- });
25
- }
26
- else {
27
- if (!options) {
28
- options = {};
29
- }
30
- options.example = convertExample(description === null || description === void 0 ? void 0 : description.example);
31
- this._params.push(options);
32
- }
33
- return this;
34
- }
35
- respond(status, description, respondType, options) {
36
- const respondContentType = convertRespondType(respondType, options);
37
- if (respondContentType) {
38
- this._response.push(convertRespondOptions({
39
- status,
40
- description: description || '',
41
- content: respondContentType,
42
- ...options,
43
- }));
44
- }
45
- else {
46
- this._response.push(convertRespondOptions({
47
- status,
48
- description: description || '',
49
- ...options,
50
- }));
51
- }
52
- return this;
53
- }
54
- buildJSON() {
55
- return {
56
- summary: this._summary,
57
- description: this._description,
58
- params: this._params,
59
- response: this._response,
60
- };
61
- }
62
- build() {
63
- return (target, property) => {
64
- (0, decorator_1.savePropertyMetadata)(exports.SWAGGER_DOCUMENT_KEY, this.buildJSON(), target, property);
65
- };
66
- }
67
- }
68
- exports.SwaggerAPI = SwaggerAPI;
69
- function CreateApiDoc(data) {
70
- if (data) {
71
- return (target, property) => { };
72
- }
73
- else {
74
- return new SwaggerAPI();
75
- }
76
- }
77
- exports.CreateApiDoc = CreateApiDoc;
78
- function CreateApiPropertyDoc(description, options) {
79
- return (target, propertyKey) => {
80
- const metadata = (0, decorator_1.getPropertyType)(target, propertyKey);
81
- (0, decorator_1.attachClassMetadata)(exports.SWAGGER_DOCUMENT_KEY, {
82
- description,
83
- type: metadata.name,
84
- isBaseType: metadata.isBaseType,
85
- originDesign: metadata.originDesign,
86
- ...options,
87
- }, target, propertyKey);
88
- };
89
- }
90
- exports.CreateApiPropertyDoc = CreateApiPropertyDoc;
91
- function convertExample(example, respondType) {
92
- if (example === null || example === undefined) {
93
- return undefined;
94
- }
95
- switch (respondType) {
96
- case 'object':
97
- case 'json':
98
- case 'boolean':
99
- case 'number':
100
- return example;
101
- }
102
- return example.toString();
103
- }
104
- function convertProperties(example, properties) {
105
- if (!example && !properties) {
106
- return undefined;
107
- }
108
- if (!properties) {
109
- properties = {};
110
- const exampleKeys = Object.keys(example);
111
- for (let i = 0; i < exampleKeys.length; i++) {
112
- const value = example[exampleKeys[i]];
113
- const valueType = typeof value;
114
- switch (valueType) {
115
- case 'boolean':
116
- case 'number':
117
- case 'string':
118
- properties[exampleKeys[i]] = {
119
- type: valueType,
120
- };
121
- break;
122
- case 'object':
123
- if (Array.isArray(value)) {
124
- properties[exampleKeys[i]] = {
125
- type: 'array',
126
- items: {
127
- type: typeof value,
128
- properties: convertProperties(value[0]),
129
- },
130
- };
131
- }
132
- else {
133
- properties[exampleKeys[i]] = {
134
- type: 'object',
135
- properties: convertProperties(example[exampleKeys[i]]),
136
- };
137
- }
138
- break;
139
- default:
140
- break;
141
- }
142
- }
143
- }
144
- return properties;
145
- }
146
- function convertRespondType(respondType, options) {
147
- switch (respondType) {
148
- case 'text':
149
- return {
150
- 'text/plain': {
151
- schema: {
152
- type: 'string',
153
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
154
- },
155
- },
156
- };
157
- case 'object':
158
- return {
159
- 'application/json': {
160
- schema: {
161
- type: 'object',
162
- properties: convertProperties(options === null || options === void 0 ? void 0 : options.example, options.properties),
163
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
164
- },
165
- },
166
- };
167
- case 'json':
168
- return {
169
- 'application/json': {
170
- schema: {
171
- type: 'object',
172
- properties: convertProperties(options === null || options === void 0 ? void 0 : options.example, options.properties),
173
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
174
- },
175
- },
176
- };
177
- case 'boolean':
178
- return {
179
- 'text/plain': {
180
- schema: {
181
- type: 'string',
182
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
183
- },
184
- },
185
- };
186
- case 'number':
187
- return {
188
- 'text/plain': {
189
- schema: {
190
- type: 'string',
191
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
192
- },
193
- },
194
- };
195
- case 'html':
196
- return {
197
- 'text/html': {
198
- schema: {
199
- type: 'string',
200
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
201
- },
202
- },
203
- };
204
- case 'css':
205
- return {
206
- 'text/css': {
207
- schema: {
208
- type: 'string',
209
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
210
- },
211
- },
212
- };
213
- case 'js':
214
- return {
215
- 'application/javascript': {
216
- schema: {
217
- type: 'string',
218
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
219
- },
220
- },
221
- };
222
- case 'svg':
223
- return {
224
- 'image/svg+xml': {
225
- schema: {
226
- type: 'string',
227
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
228
- },
229
- },
230
- };
231
- case 'gif':
232
- return {
233
- 'image/gif': {
234
- schema: {
235
- type: 'string',
236
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
237
- },
238
- },
239
- };
240
- case 'jpg':
241
- return {
242
- 'image/jpeg': {
243
- schema: {
244
- type: 'string',
245
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
246
- },
247
- },
248
- };
249
- case 'png':
250
- return {
251
- 'mage/png': {
252
- schema: {
253
- type: 'string',
254
- example: convertExample(options === null || options === void 0 ? void 0 : options.example, respondType),
255
- },
256
- },
257
- };
258
- default:
259
- return undefined;
260
- }
261
- }
262
- function convertRespondOptions(respond) {
263
- if (respond.headers) {
264
- for (const headerName in respond.headers) {
265
- const originRespondHeader = respond.headers[headerName];
266
- respond.headers[headerName] = {
267
- description: originRespondHeader.description,
268
- schema: {
269
- type: originRespondHeader.type,
270
- },
271
- };
272
- }
273
- }
274
- return respond;
275
- }
276
- //# sourceMappingURL=createAPI.js.map