swagger2api-v3 1.0.3 → 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/cli/index.js CHANGED
@@ -115,7 +115,7 @@ program
115
115
  */
116
116
  const config = {
117
117
  // Swagger JSON 文件路径或 URL
118
- input: 'https://petstore.swagger.io/v2/swagger.json',
118
+ input: 'http://localhost:3000/admin/docs/json',
119
119
 
120
120
  // 输出目录
121
121
  output: './src/api',
@@ -61,7 +61,13 @@ class SwaggerParser {
61
61
  }
62
62
  }
63
63
  // 生成函数名
64
- const functionName = operation.operationId || (0, utils_1.pathToFunctionName)(method, path);
64
+ let functionName = operation.operationId || (0, utils_1.pathToFunctionName)(method, path);
65
+ // 如果使用了operationId,需要手动添加HTTP方法后缀
66
+ if (operation.operationId) {
67
+ // 将HTTP方法转换为首字母大写的形式并添加到末尾
68
+ const methodSuffix = method.charAt(0).toUpperCase() + method.slice(1).toLowerCase();
69
+ functionName = functionName + methodSuffix;
70
+ }
65
71
  // 获取响应类型
66
72
  const responseType = (0, utils_1.getResponseType)(operation.responses);
67
73
  // 获取请求体类型
@@ -65,20 +65,21 @@ 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);
69
- // 将方法名添加到开头
70
- const parts = [method.toLowerCase(), ...segments];
71
- // 转换为小驼峰命名
72
- return parts
73
- .map((part, index) => {
68
+ const segments = cleanPath.split('/').filter((segment) => segment.length > 0);
69
+ // 将路径段转换为驼峰命名
70
+ const pathParts = segments.map((part, index) => {
74
71
  // 移除特殊字符并转换为小驼峰
75
72
  const cleanPart = part.replace(/[^a-zA-Z0-9]/g, '');
76
73
  if (index === 0) {
77
74
  return cleanPart.toLowerCase();
78
75
  }
79
76
  return cleanPart.charAt(0).toUpperCase() + cleanPart.slice(1).toLowerCase();
80
- })
81
- .join('');
77
+ });
78
+ // 将HTTP方法转换为首字母大写的形式并添加到末尾
79
+ const methodSuffix = method.charAt(0).toUpperCase() + method.slice(1).toLowerCase();
80
+ // 组合路径名称和方法名称
81
+ const baseName = pathParts.join('');
82
+ return baseName + methodSuffix;
82
83
  }
83
84
  /**
84
85
  * 将字符串转换为kebab-case
@@ -98,8 +99,8 @@ function toKebabCase(str) {
98
99
  */
99
100
  function toPascalCase(str) {
100
101
  return str
101
- .replace(/[\s-_]+(.)?/g, (_, char) => char ? char.toUpperCase() : '')
102
- .replace(/^(.)/, char => char.toUpperCase());
102
+ .replace(/[\s-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ''))
103
+ .replace(/^(.)/, (char) => char.toUpperCase());
103
104
  }
104
105
  /**
105
106
  * 将字符串转换为camelCase
@@ -119,6 +120,20 @@ function swaggerTypeToTsType(schema) {
119
120
  if (!schema) {
120
121
  return 'any';
121
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
+ }
122
137
  // 处理引用类型
123
138
  if (schema.$ref) {
124
139
  const refName = schema.$ref.split('/').pop();
@@ -150,7 +165,7 @@ function swaggerTypeToTsType(schema) {
150
165
  return 'number';
151
166
  case 'string':
152
167
  if (schema.enum) {
153
- return schema.enum.map(value => `'${value}'`).join(' | ');
168
+ return schema.enum.map((value) => `'${value}'`).join(' | ');
154
169
  }
155
170
  return 'string';
156
171
  case 'boolean':
@@ -170,27 +185,27 @@ function generateParameterTypes(parameters) {
170
185
  if (!parameters || parameters.length === 0) {
171
186
  return '';
172
187
  }
173
- const queryParams = parameters.filter(p => p.in === 'query');
174
- const pathParams = parameters.filter(p => p.in === 'path');
175
- const bodyParams = parameters.filter(p => p.in === 'body');
176
- 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');
177
192
  const types = [];
178
193
  // 路径参数
179
194
  if (pathParams.length > 0) {
180
195
  const pathType = pathParams
181
- .map(p => `${p.name}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`)
196
+ .map((p) => `${p.name}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`)
182
197
  .join(', ');
183
198
  types.push(`pathParams: { ${pathType} }`);
184
199
  }
185
200
  // 查询参数
186
201
  if (queryParams.length > 0) {
187
202
  const queryType = queryParams
188
- .map(p => {
203
+ .map((p) => {
189
204
  const optional = p.required ? '' : '?';
190
205
  return `${p.name}${optional}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`;
191
206
  })
192
207
  .join(', ');
193
- types.push(`queryParams${queryParams.every(p => !p.required) ? '?' : ''}: { ${queryType} }`);
208
+ types.push(`queryParams${queryParams.every((p) => !p.required) ? '?' : ''}: { ${queryType} }`);
194
209
  }
195
210
  // 请求体参数
196
211
  if (bodyParams.length > 0) {
@@ -201,7 +216,7 @@ function generateParameterTypes(parameters) {
201
216
  // 表单参数
202
217
  if (formParams.length > 0) {
203
218
  const formType = formParams
204
- .map(p => {
219
+ .map((p) => {
205
220
  const optional = p.required ? '' : '?';
206
221
  return `${p.name}${optional}: ${swaggerTypeToTsType({ type: p.type || 'string' })}`;
207
222
  })
@@ -276,7 +291,7 @@ function generateApiComment(operation, parameters) {
276
291
  }
277
292
  if (parameters && parameters.length > 0) {
278
293
  comments.push(' *');
279
- parameters.forEach(param => {
294
+ parameters.forEach((param) => {
280
295
  const description = param.description || '';
281
296
  comments.push(` * @param ${param.name} ${description}`);
282
297
  });
@@ -307,7 +322,9 @@ function getResponseType(responses) {
307
322
  return 'any';
308
323
  }
309
324
  // 支持OpenAPI 3.0格式 (content.application/json.schema)
310
- if (successResponse.content && successResponse.content['application/json'] && successResponse.content['application/json'].schema) {
325
+ if (successResponse.content &&
326
+ successResponse.content['application/json'] &&
327
+ successResponse.content['application/json'].schema) {
311
328
  return swaggerTypeToTsType(successResponse.content['application/json'].schema);
312
329
  }
313
330
  // 支持Swagger 2.0格式 (直接schema)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger2api-v3",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "从 Swagger/OpenAPI 文档生成 TypeScript API 接口的命令行工具",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",