swagger2api-v3 1.0.4 → 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.
@@ -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 = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
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 ? (0, utils_1.swaggerTypeToTsType)(bodyParam.schema) : undefined;
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 ? (0, utils_1.swaggerTypeToTsType)(param.schema) : (0, utils_1.swaggerTypeToTsType)({ type: param.type || 'string' });
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 ? ` /** ${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) => ` ${JSON.stringify(value)}`)
151
- .join(' |\n');
152
- definition = `export type ${typeName} =\n${enumValues};`;
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 = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
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
  }
@@ -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 && successResponse.content['application/json'] && successResponse.content['application/json'].schema) {
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger2api-v3",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "从 Swagger/OpenAPI 文档生成 TypeScript API 接口的命令行工具",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",