swagger2api-v3 1.0.5 → 1.0.6
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/dist/core/parser.js +34 -12
- package/package.json +1 -1
package/dist/core/parser.js
CHANGED
|
@@ -18,7 +18,15 @@ class SwaggerParser {
|
|
|
18
18
|
const apis = [];
|
|
19
19
|
const paths = this.document.paths;
|
|
20
20
|
for (const [path, pathItem] of Object.entries(paths)) {
|
|
21
|
-
const methods = [
|
|
21
|
+
const methods = [
|
|
22
|
+
'get',
|
|
23
|
+
'post',
|
|
24
|
+
'put',
|
|
25
|
+
'delete',
|
|
26
|
+
'patch',
|
|
27
|
+
'head',
|
|
28
|
+
'options'
|
|
29
|
+
];
|
|
22
30
|
for (const method of methods) {
|
|
23
31
|
const operation = pathItem[method];
|
|
24
32
|
if (!operation)
|
|
@@ -71,11 +79,15 @@ class SwaggerParser {
|
|
|
71
79
|
// 获取响应类型
|
|
72
80
|
const responseType = (0, utils_1.getResponseType)(operation.responses);
|
|
73
81
|
// 获取请求体类型
|
|
74
|
-
const bodyParam = allParameters.find(p => p.in === 'body');
|
|
75
|
-
const requestBodyType = bodyParam
|
|
82
|
+
const bodyParam = allParameters.find((p) => p.in === 'body');
|
|
83
|
+
const requestBodyType = bodyParam
|
|
84
|
+
? (0, utils_1.swaggerTypeToTsType)(bodyParam.schema)
|
|
85
|
+
: undefined;
|
|
76
86
|
// 解析参数信息
|
|
77
|
-
const parameters = allParameters.map(param => {
|
|
78
|
-
const type = param.schema
|
|
87
|
+
const parameters = allParameters.map((param) => {
|
|
88
|
+
const type = param.schema
|
|
89
|
+
? (0, utils_1.swaggerTypeToTsType)(param.schema)
|
|
90
|
+
: (0, utils_1.swaggerTypeToTsType)({ type: param.type || 'string' });
|
|
79
91
|
return {
|
|
80
92
|
name: param.name,
|
|
81
93
|
type,
|
|
@@ -133,7 +145,9 @@ class SwaggerParser {
|
|
|
133
145
|
.map(([key, value]) => {
|
|
134
146
|
const optional = schema.required?.includes(key) ? '' : '?';
|
|
135
147
|
const type = (0, utils_1.swaggerTypeToTsType)(value);
|
|
136
|
-
const comment = value.description
|
|
148
|
+
const comment = value.description
|
|
149
|
+
? ` /** ${value.description} */`
|
|
150
|
+
: '';
|
|
137
151
|
return `${comment}\n ${key}${optional}: ${type};`;
|
|
138
152
|
})
|
|
139
153
|
.join('\n');
|
|
@@ -147,9 +161,9 @@ class SwaggerParser {
|
|
|
147
161
|
else if (schema.enum) {
|
|
148
162
|
// 枚举类型
|
|
149
163
|
const enumValues = schema.enum
|
|
150
|
-
.map((value) => ` ${
|
|
151
|
-
.join('
|
|
152
|
-
definition = `export
|
|
164
|
+
.map((value) => ` ${value.toUpperCase()} = '${value}'`)
|
|
165
|
+
.join(',\n');
|
|
166
|
+
definition = `export enum ${typeName} {\n${enumValues}\n}`;
|
|
153
167
|
}
|
|
154
168
|
else {
|
|
155
169
|
// 其他类型
|
|
@@ -188,15 +202,23 @@ class SwaggerParser {
|
|
|
188
202
|
const tags = new Set();
|
|
189
203
|
// 从文档标签中获取
|
|
190
204
|
if (this.document.tags) {
|
|
191
|
-
this.document.tags.forEach(tag => tags.add(tag.name));
|
|
205
|
+
this.document.tags.forEach((tag) => tags.add(tag.name));
|
|
192
206
|
}
|
|
193
207
|
// 从路径操作中获取
|
|
194
208
|
for (const pathItem of Object.values(this.document.paths)) {
|
|
195
|
-
const methods = [
|
|
209
|
+
const methods = [
|
|
210
|
+
'get',
|
|
211
|
+
'post',
|
|
212
|
+
'put',
|
|
213
|
+
'delete',
|
|
214
|
+
'patch',
|
|
215
|
+
'head',
|
|
216
|
+
'options'
|
|
217
|
+
];
|
|
196
218
|
for (const method of methods) {
|
|
197
219
|
const operation = pathItem[method];
|
|
198
220
|
if (operation?.tags) {
|
|
199
|
-
operation.tags.forEach(tag => tags.add(tag));
|
|
221
|
+
operation.tags.forEach((tag) => tags.add(tag));
|
|
200
222
|
}
|
|
201
223
|
}
|
|
202
224
|
}
|