swagger2api-v3 1.0.4 → 1.0.5
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/utils/index.js +30 -14
- package/package.json +1 -1
package/dist/utils/index.js
CHANGED
|
@@ -65,7 +65,7 @@ function pathToFunctionName(method, path) {
|
|
|
65
65
|
// 移除路径参数的大括号
|
|
66
66
|
const cleanPath = path.replace(/\{([^}]+)\}/g, '$1');
|
|
67
67
|
// 分割路径并过滤空字符串
|
|
68
|
-
const segments = cleanPath.split('/').filter(segment => segment.length > 0);
|
|
68
|
+
const segments = cleanPath.split('/').filter((segment) => segment.length > 0);
|
|
69
69
|
// 将路径段转换为驼峰命名
|
|
70
70
|
const pathParts = segments.map((part, index) => {
|
|
71
71
|
// 移除特殊字符并转换为小驼峰
|
|
@@ -99,8 +99,8 @@ function toKebabCase(str) {
|
|
|
99
99
|
*/
|
|
100
100
|
function toPascalCase(str) {
|
|
101
101
|
return str
|
|
102
|
-
.replace(/[\s-_]+(.)?/g, (_, char) => char ? char.toUpperCase() : '')
|
|
103
|
-
.replace(/^(.)/, char => char.toUpperCase());
|
|
102
|
+
.replace(/[\s-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ''))
|
|
103
|
+
.replace(/^(.)/, (char) => char.toUpperCase());
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* 将字符串转换为camelCase
|
|
@@ -120,6 +120,20 @@ function swaggerTypeToTsType(schema) {
|
|
|
120
120
|
if (!schema) {
|
|
121
121
|
return 'any';
|
|
122
122
|
}
|
|
123
|
+
// 处理 allOf 类型组合
|
|
124
|
+
if (schema.allOf && schema.allOf.length > 0) {
|
|
125
|
+
// 对于 allOf,通常第一个元素是引用类型
|
|
126
|
+
const firstSchema = schema.allOf[0];
|
|
127
|
+
if (firstSchema.$ref) {
|
|
128
|
+
const refName = firstSchema.$ref.split('/').pop();
|
|
129
|
+
return refName || 'any';
|
|
130
|
+
}
|
|
131
|
+
// 如果不是引用,尝试合并所有类型
|
|
132
|
+
const types = schema.allOf
|
|
133
|
+
.map((s) => swaggerTypeToTsType(s))
|
|
134
|
+
.filter((t) => t !== 'any');
|
|
135
|
+
return types.length > 0 ? types[0] : 'any';
|
|
136
|
+
}
|
|
123
137
|
// 处理引用类型
|
|
124
138
|
if (schema.$ref) {
|
|
125
139
|
const refName = schema.$ref.split('/').pop();
|
|
@@ -151,7 +165,7 @@ function swaggerTypeToTsType(schema) {
|
|
|
151
165
|
return 'number';
|
|
152
166
|
case 'string':
|
|
153
167
|
if (schema.enum) {
|
|
154
|
-
return schema.enum.map(value => `'${value}'`).join(' | ');
|
|
168
|
+
return schema.enum.map((value) => `'${value}'`).join(' | ');
|
|
155
169
|
}
|
|
156
170
|
return 'string';
|
|
157
171
|
case 'boolean':
|
|
@@ -171,27 +185,27 @@ function generateParameterTypes(parameters) {
|
|
|
171
185
|
if (!parameters || parameters.length === 0) {
|
|
172
186
|
return '';
|
|
173
187
|
}
|
|
174
|
-
const queryParams = parameters.filter(p => p.in === 'query');
|
|
175
|
-
const pathParams = parameters.filter(p => p.in === 'path');
|
|
176
|
-
const bodyParams = parameters.filter(p => p.in === 'body');
|
|
177
|
-
const formParams = parameters.filter(p => p.in === 'formData');
|
|
188
|
+
const queryParams = parameters.filter((p) => p.in === 'query');
|
|
189
|
+
const pathParams = parameters.filter((p) => p.in === 'path');
|
|
190
|
+
const bodyParams = parameters.filter((p) => p.in === 'body');
|
|
191
|
+
const formParams = parameters.filter((p) => p.in === 'formData');
|
|
178
192
|
const types = [];
|
|
179
193
|
// 路径参数
|
|
180
194
|
if (pathParams.length > 0) {
|
|
181
195
|
const pathType = pathParams
|
|
182
|
-
.map(p => `${p.name}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`)
|
|
196
|
+
.map((p) => `${p.name}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`)
|
|
183
197
|
.join(', ');
|
|
184
198
|
types.push(`pathParams: { ${pathType} }`);
|
|
185
199
|
}
|
|
186
200
|
// 查询参数
|
|
187
201
|
if (queryParams.length > 0) {
|
|
188
202
|
const queryType = queryParams
|
|
189
|
-
.map(p => {
|
|
203
|
+
.map((p) => {
|
|
190
204
|
const optional = p.required ? '' : '?';
|
|
191
205
|
return `${p.name}${optional}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`;
|
|
192
206
|
})
|
|
193
207
|
.join(', ');
|
|
194
|
-
types.push(`queryParams${queryParams.every(p => !p.required) ? '?' : ''}: { ${queryType} }`);
|
|
208
|
+
types.push(`queryParams${queryParams.every((p) => !p.required) ? '?' : ''}: { ${queryType} }`);
|
|
195
209
|
}
|
|
196
210
|
// 请求体参数
|
|
197
211
|
if (bodyParams.length > 0) {
|
|
@@ -202,7 +216,7 @@ function generateParameterTypes(parameters) {
|
|
|
202
216
|
// 表单参数
|
|
203
217
|
if (formParams.length > 0) {
|
|
204
218
|
const formType = formParams
|
|
205
|
-
.map(p => {
|
|
219
|
+
.map((p) => {
|
|
206
220
|
const optional = p.required ? '' : '?';
|
|
207
221
|
return `${p.name}${optional}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`;
|
|
208
222
|
})
|
|
@@ -277,7 +291,7 @@ function generateApiComment(operation, parameters) {
|
|
|
277
291
|
}
|
|
278
292
|
if (parameters && parameters.length > 0) {
|
|
279
293
|
comments.push(' *');
|
|
280
|
-
parameters.forEach(param => {
|
|
294
|
+
parameters.forEach((param) => {
|
|
281
295
|
const description = param.description || '';
|
|
282
296
|
comments.push(` * @param ${param.name} ${description}`);
|
|
283
297
|
});
|
|
@@ -308,7 +322,9 @@ function getResponseType(responses) {
|
|
|
308
322
|
return 'any';
|
|
309
323
|
}
|
|
310
324
|
// 支持OpenAPI 3.0格式 (content.application/json.schema)
|
|
311
|
-
if (successResponse.content &&
|
|
325
|
+
if (successResponse.content &&
|
|
326
|
+
successResponse.content['application/json'] &&
|
|
327
|
+
successResponse.content['application/json'].schema) {
|
|
312
328
|
return swaggerTypeToTsType(successResponse.content['application/json'].schema);
|
|
313
329
|
}
|
|
314
330
|
// 支持Swagger 2.0格式 (直接schema)
|