@midwayjs/swagger 1.0.5 → 1.2.0
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/CHANGELOG.md +22 -0
- package/dist/configuration.js +2 -2
- package/dist/controller/swagger.js +19 -17
- package/dist/index.js +2 -1
- package/dist/lib/createAPI.d.ts +8 -6
- package/dist/lib/createAPI.js +3 -3
- package/dist/lib/document.d.ts +4 -4
- package/dist/lib/generator.d.ts +1 -0
- package/dist/lib/generator.js +107 -20
- package/dist/service/generator.d.ts +1 -1
- package/dist/service/generator.js +4 -4
- package/package.json +6 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.0.7](https://github.com/midwayjs/midway-component/compare/@midwayjs/swagger@1.0.6...@midwayjs/swagger@1.0.7) (2021-03-22)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* [#26](https://github.com/midwayjs/midway-component/issues/26) check the route info is iterable or not ([#27](https://github.com/midwayjs/midway-component/issues/27)) ([f0d4798](https://github.com/midwayjs/midway-component/commit/f0d479832aef0b558bfeac04b32a28cc83192810))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [1.0.6](https://github.com/midwayjs/midway-component/compare/@midwayjs/swagger@1.0.5...@midwayjs/swagger@1.0.6) (2020-12-09)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* array type with rule decorator ([#22](https://github.com/midwayjs/midway-component/issues/22)) ([f3b1164](https://github.com/midwayjs/midway-component/commit/f3b1164381b5c02b1d1192acfaf30f01bb34650a))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
6
28
|
## [1.0.5](https://github.com/midwayjs/midway-component/compare/@midwayjs/swagger@1.0.4...@midwayjs/swagger@1.0.5) (2020-12-02)
|
|
7
29
|
|
|
8
30
|
|
package/dist/configuration.js
CHANGED
|
@@ -18,9 +18,9 @@ let AutoConfiguration = class AutoConfiguration {
|
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
AutoConfiguration = __decorate([
|
|
21
|
-
decorator_1.Configuration({
|
|
21
|
+
(0, decorator_1.Configuration)({
|
|
22
22
|
namespace: 'swagger',
|
|
23
|
-
importConfigs: [path_1.join(__dirname, 'config')],
|
|
23
|
+
importConfigs: [(0, path_1.join)(__dirname, 'config')],
|
|
24
24
|
})
|
|
25
25
|
], AutoConfiguration);
|
|
26
26
|
exports.AutoConfiguration = AutoConfiguration;
|
|
@@ -20,8 +20,10 @@ const core_1 = require("@midwayjs/core");
|
|
|
20
20
|
const generator_1 = require("../service/generator");
|
|
21
21
|
let SwaggerController = class SwaggerController {
|
|
22
22
|
constructor() {
|
|
23
|
-
const { getAbsoluteFSPath } = core_1.safeRequire('swagger-ui-dist');
|
|
24
|
-
|
|
23
|
+
const { getAbsoluteFSPath } = (0, core_1.safeRequire)('swagger-ui-dist');
|
|
24
|
+
if (getAbsoluteFSPath) {
|
|
25
|
+
this.swaggerUiAssetPath = getAbsoluteFSPath();
|
|
26
|
+
}
|
|
25
27
|
}
|
|
26
28
|
async renderJSON() {
|
|
27
29
|
return this.swaggerGenerator.generate();
|
|
@@ -33,61 +35,61 @@ let SwaggerController = class SwaggerController {
|
|
|
33
35
|
if (!fileName) {
|
|
34
36
|
fileName = '/index.html';
|
|
35
37
|
}
|
|
36
|
-
const resourceAbsolutePath = path_1.join(this.swaggerUiAssetPath, fileName);
|
|
37
|
-
if (path_1.extname(fileName)) {
|
|
38
|
+
const resourceAbsolutePath = (0, path_1.join)(this.swaggerUiAssetPath, fileName);
|
|
39
|
+
if ((0, path_1.extname)(fileName)) {
|
|
38
40
|
// 7 天内使用缓存
|
|
39
41
|
if (this.app.getFrameworkType() === core_1.MidwayFrameworkType.WEB_EXPRESS) {
|
|
40
|
-
this.ctx.res.type(path_1.extname(fileName));
|
|
42
|
+
this.ctx.res.type((0, path_1.extname)(fileName));
|
|
41
43
|
this.ctx.res.set('cache-control', 'public, max-age=604800');
|
|
42
44
|
}
|
|
43
45
|
else {
|
|
44
|
-
this.ctx.type = path_1.extname(fileName);
|
|
46
|
+
this.ctx.type = (0, path_1.extname)(fileName);
|
|
45
47
|
this.ctx.set('cache-control', 'public, max-age=604800');
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
if (fileName.indexOf('index.html') !== -1) {
|
|
49
51
|
const htmlContent = this.getSwaggerUIResource(resourceAbsolutePath, 'utf-8');
|
|
50
|
-
return htmlContent.replace('https://petstore.swagger.io/v2/swagger.json', '/swagger-ui/json');
|
|
52
|
+
return htmlContent.replace('https://petstore.swagger.io/v2/swagger.json', this.ctx.path.replace('/swagger-ui/index.html', '/swagger-ui/json'));
|
|
51
53
|
}
|
|
52
54
|
else {
|
|
53
55
|
return this.getSwaggerUIResource(resourceAbsolutePath);
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
getSwaggerUIResource(requestPath, encoding) {
|
|
57
|
-
return fs_1.readFileSync(requestPath, {
|
|
59
|
+
return (0, fs_1.readFileSync)(requestPath, {
|
|
58
60
|
encoding,
|
|
59
61
|
});
|
|
60
62
|
}
|
|
61
63
|
};
|
|
62
64
|
__decorate([
|
|
63
|
-
decorator_1.App(),
|
|
65
|
+
(0, decorator_1.App)(),
|
|
64
66
|
__metadata("design:type", Object)
|
|
65
67
|
], SwaggerController.prototype, "app", void 0);
|
|
66
68
|
__decorate([
|
|
67
|
-
decorator_1.Inject(),
|
|
69
|
+
(0, decorator_1.Inject)(),
|
|
68
70
|
__metadata("design:type", Object)
|
|
69
71
|
], SwaggerController.prototype, "ctx", void 0);
|
|
70
72
|
__decorate([
|
|
71
|
-
decorator_1.Inject(),
|
|
73
|
+
(0, decorator_1.Inject)(),
|
|
72
74
|
__metadata("design:type", generator_1.SwaggerGenerator)
|
|
73
75
|
], SwaggerController.prototype, "swaggerGenerator", void 0);
|
|
74
76
|
__decorate([
|
|
75
|
-
decorator_1.Get('/json'),
|
|
77
|
+
(0, decorator_1.Get)('/json'),
|
|
76
78
|
__metadata("design:type", Function),
|
|
77
79
|
__metadata("design:paramtypes", []),
|
|
78
80
|
__metadata("design:returntype", Promise)
|
|
79
81
|
], SwaggerController.prototype, "renderJSON", null);
|
|
80
82
|
__decorate([
|
|
81
|
-
decorator_1.Get('/'),
|
|
82
|
-
decorator_1.Get('/:fileName'),
|
|
83
|
-
__param(0, decorator_1.Param()),
|
|
83
|
+
(0, decorator_1.Get)('/'),
|
|
84
|
+
(0, decorator_1.Get)('/:fileName'),
|
|
85
|
+
__param(0, (0, decorator_1.Param)()),
|
|
84
86
|
__metadata("design:type", Function),
|
|
85
87
|
__metadata("design:paramtypes", [String]),
|
|
86
88
|
__metadata("design:returntype", Promise)
|
|
87
89
|
], SwaggerController.prototype, "renderSwagger", null);
|
|
88
90
|
SwaggerController = __decorate([
|
|
89
|
-
decorator_1.Provide(),
|
|
90
|
-
decorator_1.Controller('/swagger-ui'),
|
|
91
|
+
(0, decorator_1.Provide)(),
|
|
92
|
+
(0, decorator_1.Controller)('/swagger-ui'),
|
|
91
93
|
__metadata("design:paramtypes", [])
|
|
92
94
|
], SwaggerController);
|
|
93
95
|
exports.SwaggerController = SwaggerController;
|
package/dist/index.js
CHANGED
|
@@ -7,9 +7,10 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
9
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.Configuration = void 0;
|
|
13
14
|
var configuration_1 = require("./configuration");
|
|
14
15
|
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.AutoConfiguration; } });
|
|
15
16
|
__exportStar(require("./controller/swagger"), exports);
|
package/dist/lib/createAPI.d.ts
CHANGED
|
@@ -6,12 +6,14 @@ export interface ApiFormat {
|
|
|
6
6
|
response: APIResponseFormat[];
|
|
7
7
|
}
|
|
8
8
|
export interface APIParamFormat {
|
|
9
|
-
name
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
name?: string;
|
|
10
|
+
type?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
required?: boolean;
|
|
13
|
+
deprecated?: boolean;
|
|
14
|
+
allowEmptyValue?: boolean;
|
|
15
|
+
example?: any;
|
|
16
|
+
$ref?: string;
|
|
15
17
|
}
|
|
16
18
|
export interface APIPropertyFormat {
|
|
17
19
|
description: string;
|
package/dist/lib/createAPI.js
CHANGED
|
@@ -61,7 +61,7 @@ class SwaggerAPI {
|
|
|
61
61
|
}
|
|
62
62
|
build() {
|
|
63
63
|
return (target, property) => {
|
|
64
|
-
decorator_1.savePropertyMetadata(exports.SWAGGER_DOCUMENT_KEY, this.buildJSON(), target, property);
|
|
64
|
+
(0, decorator_1.savePropertyMetadata)(exports.SWAGGER_DOCUMENT_KEY, this.buildJSON(), target, property);
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -77,8 +77,8 @@ function CreateApiDoc(data) {
|
|
|
77
77
|
exports.CreateApiDoc = CreateApiDoc;
|
|
78
78
|
function CreateApiPropertyDoc(description, options) {
|
|
79
79
|
return (target, propertyKey) => {
|
|
80
|
-
const metadata = decorator_1.getPropertyType(target, propertyKey);
|
|
81
|
-
decorator_1.attachClassMetadata(exports.SWAGGER_DOCUMENT_KEY, {
|
|
80
|
+
const metadata = (0, decorator_1.getPropertyType)(target, propertyKey);
|
|
81
|
+
(0, decorator_1.attachClassMetadata)(exports.SWAGGER_DOCUMENT_KEY, {
|
|
82
82
|
description,
|
|
83
83
|
type: metadata.name,
|
|
84
84
|
isBaseType: metadata.isBaseType,
|
package/dist/lib/document.d.ts
CHANGED
|
@@ -66,8 +66,8 @@ export declare class SwaggerDocumentRouter {
|
|
|
66
66
|
consumes: string[];
|
|
67
67
|
produces: string[];
|
|
68
68
|
parameters: SwaggerDocumentParameter[];
|
|
69
|
-
requestBody:
|
|
70
|
-
responses:
|
|
69
|
+
requestBody: Record<string, unknown>;
|
|
70
|
+
responses: Record<string, unknown>;
|
|
71
71
|
security: [];
|
|
72
72
|
constructor(method: any, url: any);
|
|
73
73
|
toJSON(): {
|
|
@@ -78,8 +78,8 @@ export declare class SwaggerDocumentRouter {
|
|
|
78
78
|
consumes: string[];
|
|
79
79
|
produces: string[];
|
|
80
80
|
parameters: any;
|
|
81
|
-
requestBody:
|
|
82
|
-
responses:
|
|
81
|
+
requestBody: Record<string, unknown>;
|
|
82
|
+
responses: Record<string, unknown>;
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
export declare class SwaggerDocumentParameter {
|
package/dist/lib/generator.d.ts
CHANGED
|
@@ -26,5 +26,6 @@ export declare class SwaggerMetaGenerator {
|
|
|
26
26
|
};
|
|
27
27
|
generateRouter(webRouterInfo: RouterOption, swaggerRouter: SwaggerDocumentRouter, module: any): void;
|
|
28
28
|
generateSwaggerDefinition(definitionClass: any): void;
|
|
29
|
+
generateSwaggerByJoiProperty(joiSchema: any, pathName?: string): any;
|
|
29
30
|
}
|
|
30
31
|
//# sourceMappingURL=generator.d.ts.map
|
package/dist/lib/generator.js
CHANGED
|
@@ -18,7 +18,7 @@ class SwaggerMetaGenerator {
|
|
|
18
18
|
this.document.info = info;
|
|
19
19
|
}
|
|
20
20
|
generateController(module) {
|
|
21
|
-
const controllerOption = decorator_1.getClassMetadata(decorator_1.CONTROLLER_KEY, module);
|
|
21
|
+
const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, module);
|
|
22
22
|
const prefix = controllerOption.prefix;
|
|
23
23
|
const tag = new document_1.SwaggerDocumentTag();
|
|
24
24
|
if (prefix !== '/') {
|
|
@@ -34,14 +34,16 @@ class SwaggerMetaGenerator {
|
|
|
34
34
|
this.document.tags.push(tag);
|
|
35
35
|
// const globalMiddleware = controllerOption.routerOptions.middleware;
|
|
36
36
|
// get router info
|
|
37
|
-
const webRouterInfo = decorator_1.getClassMetadata(decorator_1.WEB_ROUTER_KEY, module);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.WEB_ROUTER_KEY, module);
|
|
38
|
+
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
|
|
39
|
+
for (const webRouter of webRouterInfo) {
|
|
40
|
+
let url = (prefix + webRouter.path).replace('//', '/');
|
|
41
|
+
url = replaceUrl(url, parseParamsInPath(url));
|
|
42
|
+
const router = new document_1.SwaggerDocumentRouter(webRouter.requestMethod, url);
|
|
43
|
+
router.tags = [tag.name];
|
|
44
|
+
this.generateRouter(webRouter, router, module);
|
|
45
|
+
this.document.addRouter(router);
|
|
46
|
+
}
|
|
45
47
|
}
|
|
46
48
|
}
|
|
47
49
|
generate() {
|
|
@@ -49,7 +51,7 @@ class SwaggerMetaGenerator {
|
|
|
49
51
|
}
|
|
50
52
|
generateRouter(webRouterInfo, swaggerRouter, module) {
|
|
51
53
|
const ins = new module();
|
|
52
|
-
const swaggerApi = decorator_1.getPropertyMetadata(createAPI_1.SWAGGER_DOCUMENT_KEY, ins, webRouterInfo.method);
|
|
54
|
+
const swaggerApi = (0, decorator_1.getPropertyMetadata)(createAPI_1.SWAGGER_DOCUMENT_KEY, ins, webRouterInfo.method);
|
|
53
55
|
swaggerRouter.summary =
|
|
54
56
|
(swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.summary) || webRouterInfo.summary || webRouterInfo.routerName;
|
|
55
57
|
swaggerRouter.description =
|
|
@@ -58,9 +60,9 @@ class SwaggerMetaGenerator {
|
|
|
58
60
|
webRouterInfo.routerName;
|
|
59
61
|
// swaggerRouter.operationId = webRouterInfo.method;
|
|
60
62
|
swaggerRouter.parameters = [];
|
|
61
|
-
const routeArgsInfo = decorator_1.getPropertyDataFromClass(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouterInfo.method) || [];
|
|
63
|
+
const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouterInfo.method) || [];
|
|
62
64
|
// 获取方法参数类型
|
|
63
|
-
const paramTypes = decorator_1.getMethodParamTypes(ins, webRouterInfo.method);
|
|
65
|
+
const paramTypes = (0, decorator_1.getMethodParamTypes)(ins, webRouterInfo.method);
|
|
64
66
|
for (const routeArgs of routeArgsInfo) {
|
|
65
67
|
const swaggerParameter = new document_1.SwaggerDocumentParameter();
|
|
66
68
|
const argsApiInfo = swaggerApi === null || swaggerApi === void 0 ? void 0 : swaggerApi.params[routeArgs.index];
|
|
@@ -78,7 +80,7 @@ class SwaggerMetaGenerator {
|
|
|
78
80
|
continue;
|
|
79
81
|
}
|
|
80
82
|
}
|
|
81
|
-
if (decorator_1.isClass(paramTypes[routeArgs.index])) {
|
|
83
|
+
if ((0, decorator_1.isClass)(paramTypes[routeArgs.index])) {
|
|
82
84
|
this.generateSwaggerDefinition(paramTypes[routeArgs.index]);
|
|
83
85
|
swaggerParameter.schema = {
|
|
84
86
|
$ref: '#/components/schemas/' + paramTypes[routeArgs.index].name,
|
|
@@ -124,16 +126,16 @@ class SwaggerMetaGenerator {
|
|
|
124
126
|
const swaggerDefinition = new document_1.SwaggerDefinition();
|
|
125
127
|
swaggerDefinition.name = definitionClass.name;
|
|
126
128
|
swaggerDefinition.type = 'object';
|
|
127
|
-
const properties = decorator_1.getClassMetadata(createAPI_1.SWAGGER_DOCUMENT_KEY, definitionClass);
|
|
129
|
+
const properties = (0, decorator_1.getClassMetadata)(createAPI_1.SWAGGER_DOCUMENT_KEY, definitionClass);
|
|
128
130
|
for (const propertyName in properties) {
|
|
129
131
|
swaggerDefinition.properties[propertyName] = {
|
|
130
|
-
type: properties[propertyName].type,
|
|
132
|
+
type: properties[propertyName].type.toLowerCase(),
|
|
131
133
|
description: properties[propertyName].description,
|
|
132
134
|
example: properties[propertyName].example,
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
137
|
// for rule decorator
|
|
136
|
-
const rules = decorator_1.
|
|
138
|
+
const rules = (0, decorator_1.getClassExtendedMetadata)(decorator_1.RULES_KEY, definitionClass);
|
|
137
139
|
if (rules) {
|
|
138
140
|
const properties = Object.keys(rules);
|
|
139
141
|
for (const property of properties) {
|
|
@@ -141,25 +143,110 @@ class SwaggerMetaGenerator {
|
|
|
141
143
|
if (((_b = (_a = rules[property]) === null || _a === void 0 ? void 0 : _a._flags) === null || _b === void 0 ? void 0 : _b.presence) === 'required') {
|
|
142
144
|
swaggerDefinition.required.push(property);
|
|
143
145
|
}
|
|
146
|
+
// 没找到这行代码对应的装饰器
|
|
144
147
|
// get property description
|
|
145
|
-
|
|
146
|
-
|
|
148
|
+
let propertyInfo = (0, decorator_1.getPropertyMetadata)(createAPI_1.SWAGGER_DOCUMENT_KEY, definitionClass, property);
|
|
149
|
+
if (!propertyInfo) {
|
|
150
|
+
propertyInfo = this.generateSwaggerByJoiProperty(rules[property], joinCamel(definitionClass.name, property));
|
|
151
|
+
}
|
|
152
|
+
if (swaggerDefinition.properties[property] && !(propertyInfo === null || propertyInfo === void 0 ? void 0 : propertyInfo.$ref)) {
|
|
153
|
+
mixWhenPropertyEmpty(swaggerDefinition.properties[property], propertyInfo);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
swaggerDefinition.properties[property] = propertyInfo;
|
|
157
|
+
}
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
160
|
this.document.definitions.push(swaggerDefinition);
|
|
150
161
|
// DTO
|
|
151
162
|
for (const key in properties) {
|
|
152
163
|
// 必须加了属性装饰器
|
|
153
|
-
if (
|
|
164
|
+
if (!(rules === null || rules === void 0 ? void 0 : rules[key]) &&
|
|
165
|
+
properties[key].originDesign &&
|
|
166
|
+
!properties[key].isBaseType) {
|
|
154
167
|
this.generateSwaggerDefinition(properties[key].originDesign);
|
|
155
168
|
// 把复杂类型属性指向新的定义
|
|
156
169
|
swaggerDefinition.properties[key] = {};
|
|
157
|
-
swaggerDefinition.properties[key]['$ref'] =
|
|
170
|
+
swaggerDefinition.properties[key]['$ref'] =
|
|
171
|
+
'#/components/schemas/' + properties[key].type;
|
|
158
172
|
}
|
|
159
173
|
}
|
|
160
174
|
}
|
|
175
|
+
generateSwaggerByJoiProperty(joiSchema, pathName) {
|
|
176
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
177
|
+
const describe = joiSchema.describe();
|
|
178
|
+
if (((_a = describe.flags) === null || _a === void 0 ? void 0 : _a.presence) === 'forbidden') {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
let define = {
|
|
182
|
+
type: describe.type,
|
|
183
|
+
default: (_b = describe.flags) === null || _b === void 0 ? void 0 : _b.default,
|
|
184
|
+
description: (_c = describe.flags) === null || _c === void 0 ? void 0 : _c.description,
|
|
185
|
+
example: (_d = describe.examples) === null || _d === void 0 ? void 0 : _d.join(' '),
|
|
186
|
+
};
|
|
187
|
+
let min;
|
|
188
|
+
let max;
|
|
189
|
+
for (const r of describe.rules || []) {
|
|
190
|
+
if (r.name === 'min' && r.args) {
|
|
191
|
+
min = r.args.limit;
|
|
192
|
+
}
|
|
193
|
+
else if (r.name === 'max') {
|
|
194
|
+
max = r.args.limit;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if ((_e = describe.flags) === null || _e === void 0 ? void 0 : _e.only) {
|
|
198
|
+
define.enum = describe.allow;
|
|
199
|
+
}
|
|
200
|
+
if (describe.invalid) {
|
|
201
|
+
define.not = { enum: describe.invalid };
|
|
202
|
+
}
|
|
203
|
+
switch (describe.type) {
|
|
204
|
+
case 'string':
|
|
205
|
+
define.minLength = min;
|
|
206
|
+
define.maxLength = max;
|
|
207
|
+
break;
|
|
208
|
+
case 'number':
|
|
209
|
+
define.minimum = min;
|
|
210
|
+
define.maximum = max;
|
|
211
|
+
if ((_f = describe.rules) === null || _f === void 0 ? void 0 : _f.some(r => r.name === 'integer')) {
|
|
212
|
+
define.type = 'integer';
|
|
213
|
+
}
|
|
214
|
+
break;
|
|
215
|
+
case 'date':
|
|
216
|
+
define.type = 'string';
|
|
217
|
+
define.format = 'date-time';
|
|
218
|
+
break;
|
|
219
|
+
case 'object': {
|
|
220
|
+
const swaggerDefinition = new document_1.SwaggerDefinition();
|
|
221
|
+
const schemaMeta = (_g = describe.metas) === null || _g === void 0 ? void 0 : _g.find(m => Boolean(m.id));
|
|
222
|
+
swaggerDefinition.name = (schemaMeta === null || schemaMeta === void 0 ? void 0 : schemaMeta.id) || pathName;
|
|
223
|
+
swaggerDefinition.type = 'object';
|
|
224
|
+
swaggerDefinition.required = [];
|
|
225
|
+
for (const prop of (_h = joiSchema.$_terms) === null || _h === void 0 ? void 0 : _h.keys) {
|
|
226
|
+
if (((_j = prop.schema._flags) === null || _j === void 0 ? void 0 : _j.presence) === 'required') {
|
|
227
|
+
swaggerDefinition.required.push(prop.key);
|
|
228
|
+
}
|
|
229
|
+
swaggerDefinition.properties[prop.key] =
|
|
230
|
+
this.generateSwaggerByJoiProperty(prop.schema, joinCamel(pathName, prop.key));
|
|
231
|
+
}
|
|
232
|
+
this.document.definitions.push(swaggerDefinition);
|
|
233
|
+
define = { $ref: '#/components/schemas/' + swaggerDefinition.name };
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case 'array':
|
|
237
|
+
define.minItems = min;
|
|
238
|
+
define.maxItems = max;
|
|
239
|
+
if (joiSchema.$_terms.items[0]) {
|
|
240
|
+
define.items = this.generateSwaggerByJoiProperty(joiSchema.$_terms.items[0], pathName);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return define;
|
|
244
|
+
}
|
|
161
245
|
}
|
|
162
246
|
exports.SwaggerMetaGenerator = SwaggerMetaGenerator;
|
|
247
|
+
function joinCamel(word1, word2) {
|
|
248
|
+
return `${word1}${word2[0].toUpperCase()}${word2.slice(1)}`;
|
|
249
|
+
}
|
|
163
250
|
function convertTypeToString(type) {
|
|
164
251
|
switch (type) {
|
|
165
252
|
case decorator_1.RouteParamTypes.HEADERS:
|
|
@@ -15,7 +15,7 @@ const generator_1 = require("../lib/generator");
|
|
|
15
15
|
const swagger_1 = require("../controller/swagger");
|
|
16
16
|
let SwaggerGenerator = class SwaggerGenerator {
|
|
17
17
|
generate() {
|
|
18
|
-
const controllerModules = decorator_1.listModule(decorator_1.CONTROLLER_KEY);
|
|
18
|
+
const controllerModules = (0, decorator_1.listModule)(decorator_1.CONTROLLER_KEY);
|
|
19
19
|
const generator = new generator_1.SwaggerMetaGenerator(this.swaggerConfig);
|
|
20
20
|
for (const module of controllerModules) {
|
|
21
21
|
if (module !== swagger_1.SwaggerController) {
|
|
@@ -26,12 +26,12 @@ let SwaggerGenerator = class SwaggerGenerator {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
__decorate([
|
|
29
|
-
decorator_1.Config('swagger'),
|
|
29
|
+
(0, decorator_1.Config)('swagger'),
|
|
30
30
|
__metadata("design:type", Object)
|
|
31
31
|
], SwaggerGenerator.prototype, "swaggerConfig", void 0);
|
|
32
32
|
SwaggerGenerator = __decorate([
|
|
33
|
-
decorator_1.Provide('swaggerGenerator'),
|
|
34
|
-
decorator_1.Scope(decorator_1.ScopeEnum.Singleton)
|
|
33
|
+
(0, decorator_1.Provide)('swaggerGenerator'),
|
|
34
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
35
35
|
], SwaggerGenerator);
|
|
36
36
|
exports.SwaggerGenerator = SwaggerGenerator;
|
|
37
37
|
//# sourceMappingURL=generator.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/swagger",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "dist/index",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"dist/**/*.d.ts"
|
|
9
9
|
],
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@midwayjs/cli": "^1.0.0",
|
|
12
11
|
"@midwayjs/core": "^2.3.0",
|
|
13
12
|
"@midwayjs/decorator": "^2.3.0",
|
|
14
13
|
"@midwayjs/koa": "^2.3.0",
|
|
@@ -18,14 +17,13 @@
|
|
|
18
17
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
19
18
|
"license": "MIT",
|
|
20
19
|
"scripts": {
|
|
21
|
-
"build": "
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"jest": "node --require=ts-node/register ../../node_modules/jest/bin/jest.js",
|
|
22
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest",
|
|
23
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit"
|
|
25
24
|
},
|
|
26
25
|
"repository": {
|
|
27
26
|
"type": "git",
|
|
28
27
|
"url": "http://github.com/midwayjs/midway-component.git"
|
|
29
|
-
}
|
|
30
|
-
"gitHead": "eb9c458fd32ce9676cb1e24b22b8aa39858d7d1e"
|
|
28
|
+
}
|
|
31
29
|
}
|