swagger2api-v3 1.0.6 → 1.0.8
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/README.md +18 -15
- package/dist/cli/index.js +37 -37
- package/dist/core/generator.d.ts +13 -0
- package/dist/core/generator.js +85 -23
- package/dist/core/parser.js +16 -1
- package/dist/utils/index.js +106 -35
- package/package.json +68 -68
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Swagger2API-v3
|
|
2
2
|
|
|
3
|
-
一个强大的命令行工具,用于从 Swagger
|
|
3
|
+
一个强大的命令行工具,用于从 Swagger(OAS3.0) 文档自动生成 TypeScript 接口代码。
|
|
4
4
|
|
|
5
5
|
## ✨ 特性
|
|
6
6
|
|
|
@@ -26,7 +26,7 @@ npm install swagger2api-v3
|
|
|
26
26
|
### 1. 初始化配置文件
|
|
27
27
|
|
|
28
28
|
```bash
|
|
29
|
-
swagger2api-v3 init
|
|
29
|
+
npx swagger2api-v3 init
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
### 2. 配置文件说明
|
|
@@ -64,7 +64,7 @@ export default config;
|
|
|
64
64
|
### 3. 生成接口代码
|
|
65
65
|
|
|
66
66
|
```bash
|
|
67
|
-
swagger2api-v3 generate
|
|
67
|
+
npx swagger2api-v3 generate
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
## ⚙️ 配置选项
|
|
@@ -128,18 +128,20 @@ export interface UserInfo {
|
|
|
128
128
|
### 生成的 API 接口
|
|
129
129
|
|
|
130
130
|
```typescript
|
|
131
|
-
//
|
|
131
|
+
// authController/index.ts
|
|
132
132
|
import { request } from '@/utils/request';
|
|
133
|
-
import type { LoginDto,
|
|
133
|
+
import type { LoginDto, LoginRespDto } from '../types';
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
136
|
+
* 登录
|
|
137
137
|
* @param data 登录参数
|
|
138
|
+
* @param config 可选的请求配置
|
|
138
139
|
*/
|
|
139
|
-
export const
|
|
140
|
-
return request.post<
|
|
141
|
-
url: '/auth/login',
|
|
142
|
-
data
|
|
140
|
+
export const authControllerLoginPost = (data: LoginDto, config?: any) => {
|
|
141
|
+
return request.post<LoginRespDto>({
|
|
142
|
+
url: '/admin/auth/login',
|
|
143
|
+
data,
|
|
144
|
+
...config
|
|
143
145
|
});
|
|
144
146
|
};
|
|
145
147
|
```
|
|
@@ -148,16 +150,16 @@ export const login = (data: LoginDto) => {
|
|
|
148
150
|
|
|
149
151
|
```bash
|
|
150
152
|
# 初始化配置文件
|
|
151
|
-
swagger2api-v3 init [--force]
|
|
153
|
+
npx swagger2api-v3 init [--force]
|
|
152
154
|
|
|
153
155
|
# 生成接口代码
|
|
154
|
-
swagger2api-v3 generate [--config <path>]
|
|
156
|
+
npx swagger2api-v3 generate [--config <path>]
|
|
155
157
|
|
|
156
158
|
# 验证配置文件
|
|
157
|
-
swagger2api-v3 validate [--config <path>]
|
|
159
|
+
npx swagger2api-v3 validate [--config <path>]
|
|
158
160
|
|
|
159
161
|
# 查看帮助
|
|
160
|
-
swagger2api-v3 --help
|
|
162
|
+
npx swagger2api-v3 --help
|
|
161
163
|
```
|
|
162
164
|
|
|
163
165
|
## 📝 NPM 脚本
|
|
@@ -168,7 +170,8 @@ swagger2api-v3 --help
|
|
|
168
170
|
{
|
|
169
171
|
"scripts": {
|
|
170
172
|
"api:generate": "swagger2api-v3 generate",
|
|
171
|
-
"api:init": "swagger2api-v3 init"
|
|
173
|
+
"api:init": "swagger2api-v3 init",
|
|
174
|
+
"api:validate": "swagger2api-v3 validate"
|
|
172
175
|
}
|
|
173
176
|
}
|
|
174
177
|
```
|
package/dist/cli/index.js
CHANGED
|
@@ -109,43 +109,43 @@ program
|
|
|
109
109
|
console.warn('⚠️ 无法读取package.json,使用默认CommonJS格式');
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
-
const configContent = `/**
|
|
113
|
-
* Swagger2API 配置文件
|
|
114
|
-
* 用于配置从 Swagger JSON 生成前端接口的参数
|
|
115
|
-
*/
|
|
116
|
-
const config = {
|
|
117
|
-
// Swagger JSON 文件路径或 URL
|
|
118
|
-
input: 'http://localhost:3000/admin/docs/json',
|
|
119
|
-
|
|
120
|
-
// 输出目录
|
|
121
|
-
output: './src/api',
|
|
122
|
-
|
|
123
|
-
// request 导入路径模板
|
|
124
|
-
importTemplate: "import { request } from '@/utils/request';",
|
|
125
|
-
|
|
126
|
-
// 生成器类型
|
|
127
|
-
generator: 'typescript',
|
|
128
|
-
|
|
129
|
-
// 按标签分组生成文件
|
|
130
|
-
groupByTags: true,
|
|
131
|
-
|
|
132
|
-
// 是否覆盖更新,默认为true。为true时会先删除输出目录下的所有文件
|
|
133
|
-
overwrite: true,
|
|
134
|
-
|
|
135
|
-
// 接口路径公共前缀,默认为空字符串
|
|
136
|
-
prefix: '',
|
|
137
|
-
|
|
138
|
-
// 代码格式化命令(可选)
|
|
139
|
-
lint: 'prettier --write',
|
|
140
|
-
|
|
141
|
-
// 生成选项
|
|
142
|
-
options: {
|
|
143
|
-
// 是否添加注释
|
|
144
|
-
addComments: true
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
${isESModule ? 'export default config;' : 'module.exports = config;'}
|
|
112
|
+
const configContent = `/**
|
|
113
|
+
* Swagger2API 配置文件
|
|
114
|
+
* 用于配置从 Swagger JSON 生成前端接口的参数
|
|
115
|
+
*/
|
|
116
|
+
const config = {
|
|
117
|
+
// Swagger JSON 文件路径或 URL
|
|
118
|
+
input: 'http://localhost:3000/admin/docs/json',
|
|
119
|
+
|
|
120
|
+
// 输出目录
|
|
121
|
+
output: './src/api',
|
|
122
|
+
|
|
123
|
+
// request 导入路径模板
|
|
124
|
+
importTemplate: "import { request } from '@/utils/request';",
|
|
125
|
+
|
|
126
|
+
// 生成器类型
|
|
127
|
+
generator: 'typescript',
|
|
128
|
+
|
|
129
|
+
// 按标签分组生成文件
|
|
130
|
+
groupByTags: true,
|
|
131
|
+
|
|
132
|
+
// 是否覆盖更新,默认为true。为true时会先删除输出目录下的所有文件
|
|
133
|
+
overwrite: true,
|
|
134
|
+
|
|
135
|
+
// 接口路径公共前缀,默认为空字符串
|
|
136
|
+
prefix: '',
|
|
137
|
+
|
|
138
|
+
// 代码格式化命令(可选)
|
|
139
|
+
lint: 'prettier --write',
|
|
140
|
+
|
|
141
|
+
// 生成选项
|
|
142
|
+
options: {
|
|
143
|
+
// 是否添加注释
|
|
144
|
+
addComments: true
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
${isESModule ? 'export default config;' : 'module.exports = config;'}
|
|
149
149
|
`;
|
|
150
150
|
try {
|
|
151
151
|
fs.writeFileSync(configPath, configContent, 'utf-8');
|
package/dist/core/generator.d.ts
CHANGED
|
@@ -67,12 +67,25 @@ export declare class CodeGenerator {
|
|
|
67
67
|
* @returns 使用的类型名称数组
|
|
68
68
|
*/
|
|
69
69
|
private collectUsedTypes;
|
|
70
|
+
/**
|
|
71
|
+
* 从类型字符串中提取所有类型名称(包括泛型参数)
|
|
72
|
+
* @param typeStr 类型字符串,如 "ResOp<UserListRespDto>"
|
|
73
|
+
* @returns 类型名称数组,如 ["ResOp", "UserListRespDto"]
|
|
74
|
+
*/
|
|
75
|
+
private extractTypeNames;
|
|
70
76
|
/**
|
|
71
77
|
* 判断是否为基础类型
|
|
72
78
|
* @param type 类型名称
|
|
73
79
|
* @returns 是否为基础类型
|
|
74
80
|
*/
|
|
75
81
|
private isPrimitiveType;
|
|
82
|
+
/**
|
|
83
|
+
* 检测是否为通用响应容器类型
|
|
84
|
+
* @param type 类型信息
|
|
85
|
+
* @param definition 类型定义
|
|
86
|
+
* @returns 是否为通用响应容器类型
|
|
87
|
+
*/
|
|
88
|
+
private isGenericResponseContainer;
|
|
76
89
|
/**
|
|
77
90
|
* 生成请求配置
|
|
78
91
|
* @param api API接口信息
|
package/dist/core/generator.js
CHANGED
|
@@ -102,11 +102,19 @@ class CodeGenerator {
|
|
|
102
102
|
''
|
|
103
103
|
].join('\n');
|
|
104
104
|
const typeDefinitions = types
|
|
105
|
-
.map(type => {
|
|
105
|
+
.map((type) => {
|
|
106
106
|
const comment = type.description
|
|
107
107
|
? `/**\n * ${type.description}\n */\n`
|
|
108
108
|
: '';
|
|
109
|
-
|
|
109
|
+
// 通用处理:检测通用响应容器类型并转换为泛型接口
|
|
110
|
+
let definition = type.definition;
|
|
111
|
+
if (this.isGenericResponseContainer(type, definition)) {
|
|
112
|
+
const typeName = type.name;
|
|
113
|
+
definition = definition
|
|
114
|
+
.replace(`export interface ${typeName} {`, `export interface ${typeName}<T = Record<string, any>> {`)
|
|
115
|
+
.replace('data: Record<string, any>;', 'data: T;');
|
|
116
|
+
}
|
|
117
|
+
return `${comment}${definition}`;
|
|
110
118
|
})
|
|
111
119
|
.join('\n\n');
|
|
112
120
|
return `${header}${typeDefinitions}\n`;
|
|
@@ -163,7 +171,9 @@ class CodeGenerator {
|
|
|
163
171
|
header.push(`import type { ${typeNames} } from '${typesPath}';`);
|
|
164
172
|
}
|
|
165
173
|
header.push('');
|
|
166
|
-
const apiImplementations = apis
|
|
174
|
+
const apiImplementations = apis
|
|
175
|
+
.map((api) => this.generateApiFunction(api))
|
|
176
|
+
.join('\n\n');
|
|
167
177
|
return `${header.join('\n')}${apiImplementations}\n`;
|
|
168
178
|
}
|
|
169
179
|
/**
|
|
@@ -175,7 +185,7 @@ class CodeGenerator {
|
|
|
175
185
|
const parts = [];
|
|
176
186
|
// 生成注释
|
|
177
187
|
if (this.config.options?.addComments !== false) {
|
|
178
|
-
const swaggerParams = api.parameters.map(p => ({
|
|
188
|
+
const swaggerParams = api.parameters.map((p) => ({
|
|
179
189
|
name: p.name,
|
|
180
190
|
in: p.in,
|
|
181
191
|
required: p.required,
|
|
@@ -186,7 +196,7 @@ class CodeGenerator {
|
|
|
186
196
|
parts.push(comment);
|
|
187
197
|
}
|
|
188
198
|
// 生成函数签名
|
|
189
|
-
const swaggerParameters = api.parameters.map(p => ({
|
|
199
|
+
const swaggerParameters = api.parameters.map((p) => ({
|
|
190
200
|
name: p.name,
|
|
191
201
|
in: p.in,
|
|
192
202
|
required: p.required,
|
|
@@ -211,34 +221,36 @@ class CodeGenerator {
|
|
|
211
221
|
*/
|
|
212
222
|
generateDirectParameters(parameters) {
|
|
213
223
|
const params = [];
|
|
214
|
-
const queryParams = parameters.filter(p => p.in === 'query');
|
|
215
|
-
const pathParams = parameters.filter(p => p.in === 'path');
|
|
216
|
-
const bodyParams = parameters.filter(p => p.in === 'body');
|
|
217
|
-
const formParams = parameters.filter(p => p.in === 'formData');
|
|
224
|
+
const queryParams = parameters.filter((p) => p.in === 'query');
|
|
225
|
+
const pathParams = parameters.filter((p) => p.in === 'path');
|
|
226
|
+
const bodyParams = parameters.filter((p) => p.in === 'body');
|
|
227
|
+
const formParams = parameters.filter((p) => p.in === 'formData');
|
|
218
228
|
// 合并路径参数和查询参数为一个params对象
|
|
219
229
|
const allParams = [...pathParams, ...queryParams];
|
|
220
230
|
if (allParams.length > 0) {
|
|
221
231
|
const paramType = allParams
|
|
222
|
-
.map(p => {
|
|
232
|
+
.map((p) => {
|
|
223
233
|
const optional = p.required ? '' : '?';
|
|
224
234
|
return `${p.name}${optional}: ${p.type}`;
|
|
225
235
|
})
|
|
226
236
|
.join(', ');
|
|
227
237
|
// 检查是否所有参数都是可选的
|
|
228
|
-
const allOptional = allParams.every(p => !p.required);
|
|
238
|
+
const allOptional = allParams.every((p) => !p.required);
|
|
229
239
|
const optionalModifier = allOptional ? '?' : '';
|
|
230
240
|
params.push(`params${optionalModifier}: { ${paramType} }`);
|
|
231
241
|
}
|
|
232
242
|
// 请求体参数
|
|
233
243
|
if (bodyParams.length > 0) {
|
|
234
244
|
const bodyParam = bodyParams[0];
|
|
235
|
-
const bodyType = bodyParam.schema
|
|
245
|
+
const bodyType = bodyParam.schema
|
|
246
|
+
? this.getTypeFromSchema(bodyParam.schema)
|
|
247
|
+
: bodyParam.type;
|
|
236
248
|
params.push(`data: ${bodyType}`);
|
|
237
249
|
}
|
|
238
250
|
// 表单参数
|
|
239
251
|
if (formParams.length > 0) {
|
|
240
252
|
const formType = formParams
|
|
241
|
-
.map(p => {
|
|
253
|
+
.map((p) => {
|
|
242
254
|
const optional = p.required ? '' : '?';
|
|
243
255
|
return `${p.name}${optional}: ${p.type}`;
|
|
244
256
|
})
|
|
@@ -267,17 +279,22 @@ class CodeGenerator {
|
|
|
267
279
|
*/
|
|
268
280
|
collectUsedTypes(apis) {
|
|
269
281
|
const usedTypes = new Set();
|
|
270
|
-
apis.forEach(api => {
|
|
282
|
+
apis.forEach((api) => {
|
|
271
283
|
// 收集响应类型
|
|
272
284
|
if (api.responseType && api.responseType !== 'any') {
|
|
273
|
-
|
|
285
|
+
// 提取泛型类型中的所有类型名称
|
|
286
|
+
this.extractTypeNames(api.responseType).forEach((typeName) => {
|
|
287
|
+
usedTypes.add(typeName);
|
|
288
|
+
});
|
|
274
289
|
}
|
|
275
290
|
// 收集参数类型
|
|
276
|
-
api.parameters.forEach(param => {
|
|
291
|
+
api.parameters.forEach((param) => {
|
|
277
292
|
if (param.schema) {
|
|
278
293
|
const type = this.getTypeFromSchema(param.schema);
|
|
279
294
|
if (type && type !== 'any' && !this.isPrimitiveType(type)) {
|
|
280
|
-
|
|
295
|
+
this.extractTypeNames(type).forEach((typeName) => {
|
|
296
|
+
usedTypes.add(typeName);
|
|
297
|
+
});
|
|
281
298
|
}
|
|
282
299
|
}
|
|
283
300
|
else if (param.type && !this.isPrimitiveType(param.type)) {
|
|
@@ -287,15 +304,60 @@ class CodeGenerator {
|
|
|
287
304
|
});
|
|
288
305
|
return Array.from(usedTypes).sort();
|
|
289
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* 从类型字符串中提取所有类型名称(包括泛型参数)
|
|
309
|
+
* @param typeStr 类型字符串,如 "ResOp<UserListRespDto>"
|
|
310
|
+
* @returns 类型名称数组,如 ["ResOp", "UserListRespDto"]
|
|
311
|
+
*/
|
|
312
|
+
extractTypeNames(typeStr) {
|
|
313
|
+
const typeNames = new Set();
|
|
314
|
+
// 匹配所有标识符(类型名称)
|
|
315
|
+
const matches = typeStr.match(/[A-Za-z_][A-Za-z0-9_]*/g);
|
|
316
|
+
if (matches) {
|
|
317
|
+
matches.forEach((match) => {
|
|
318
|
+
if (!this.isPrimitiveType(match)) {
|
|
319
|
+
typeNames.add(match);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return Array.from(typeNames);
|
|
324
|
+
}
|
|
290
325
|
/**
|
|
291
326
|
* 判断是否为基础类型
|
|
292
327
|
* @param type 类型名称
|
|
293
328
|
* @returns 是否为基础类型
|
|
294
329
|
*/
|
|
295
330
|
isPrimitiveType(type) {
|
|
296
|
-
const primitiveTypes = [
|
|
331
|
+
const primitiveTypes = [
|
|
332
|
+
'string',
|
|
333
|
+
'number',
|
|
334
|
+
'boolean',
|
|
335
|
+
'object',
|
|
336
|
+
'array',
|
|
337
|
+
'any',
|
|
338
|
+
'void',
|
|
339
|
+
'null',
|
|
340
|
+
'undefined'
|
|
341
|
+
];
|
|
297
342
|
return primitiveTypes.includes(type.toLowerCase());
|
|
298
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* 检测是否为通用响应容器类型
|
|
346
|
+
* @param type 类型信息
|
|
347
|
+
* @param definition 类型定义
|
|
348
|
+
* @returns 是否为通用响应容器类型
|
|
349
|
+
*/
|
|
350
|
+
isGenericResponseContainer(type, definition) {
|
|
351
|
+
// 检查是否为接口定义
|
|
352
|
+
if (!definition.includes(`export interface ${type.name} {`)) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
// 检查是否包含 data 字段且类型为 Record<string, any>
|
|
356
|
+
const hasDataField = definition.includes('data: Record<string, any>;');
|
|
357
|
+
// 检查是否包含其他常见的响应容器字段
|
|
358
|
+
const hasCommonFields = ['code', 'message', 'success', 'status'].some((field) => definition.includes(`${field}:`));
|
|
359
|
+
return hasDataField && hasCommonFields;
|
|
360
|
+
}
|
|
299
361
|
/**
|
|
300
362
|
* 生成请求配置
|
|
301
363
|
* @param api API接口信息
|
|
@@ -309,11 +371,11 @@ class CodeGenerator {
|
|
|
309
371
|
if (this.config.prefix) {
|
|
310
372
|
url = this.config.prefix + url;
|
|
311
373
|
}
|
|
312
|
-
const pathParams = api.parameters.filter(p => p.in === 'path');
|
|
313
|
-
const queryParams = api.parameters.filter(p => p.in === 'query');
|
|
374
|
+
const pathParams = api.parameters.filter((p) => p.in === 'path');
|
|
375
|
+
const queryParams = api.parameters.filter((p) => p.in === 'query');
|
|
314
376
|
if (pathParams.length > 0) {
|
|
315
377
|
// 替换路径参数,从params对象中获取
|
|
316
|
-
pathParams.forEach(param => {
|
|
378
|
+
pathParams.forEach((param) => {
|
|
317
379
|
url = url.replace(`{${param.name}}`, `\${params.${param.name}}`);
|
|
318
380
|
});
|
|
319
381
|
config.push(`url: \`${url}\``);
|
|
@@ -327,8 +389,8 @@ class CodeGenerator {
|
|
|
327
389
|
config.push('params');
|
|
328
390
|
}
|
|
329
391
|
// 请求体数据
|
|
330
|
-
const bodyParams = api.parameters.filter(p => p.in === 'body');
|
|
331
|
-
const formParams = api.parameters.filter(p => p.in === 'formData');
|
|
392
|
+
const bodyParams = api.parameters.filter((p) => p.in === 'body');
|
|
393
|
+
const formParams = api.parameters.filter((p) => p.in === 'formData');
|
|
332
394
|
if (bodyParams.length > 0) {
|
|
333
395
|
config.push('data');
|
|
334
396
|
}
|
package/dist/core/parser.js
CHANGED
|
@@ -161,7 +161,22 @@ class SwaggerParser {
|
|
|
161
161
|
else if (schema.enum) {
|
|
162
162
|
// 枚举类型
|
|
163
163
|
const enumValues = schema.enum
|
|
164
|
-
.map((value) =>
|
|
164
|
+
.map((value, index) => {
|
|
165
|
+
let key = value;
|
|
166
|
+
// 优先使用 x-enum-varnames 或 x-enumNames 扩展字段
|
|
167
|
+
if ((schema['x-enum-varnames'] && schema['x-enum-varnames'][index]) ||
|
|
168
|
+
(schema['x-enumNames'] && schema['x-enumNames'][index])) {
|
|
169
|
+
key = schema['x-enum-varnames']?.[index] || schema['x-enumNames']?.[index];
|
|
170
|
+
}
|
|
171
|
+
else if (/^\d+$/.test(value)) {
|
|
172
|
+
// 对于数字枚举,使用 VALUE_ 前缀
|
|
173
|
+
key = `VALUE_${value}`;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
key = value.toUpperCase();
|
|
177
|
+
}
|
|
178
|
+
return ` ${key} = '${value}'`;
|
|
179
|
+
})
|
|
165
180
|
.join(',\n');
|
|
166
181
|
definition = `export enum ${typeName} {\n${enumValues}\n}`;
|
|
167
182
|
}
|
package/dist/utils/index.js
CHANGED
|
@@ -120,32 +120,65 @@ function swaggerTypeToTsType(schema) {
|
|
|
120
120
|
if (!schema) {
|
|
121
121
|
return 'any';
|
|
122
122
|
}
|
|
123
|
+
let baseType;
|
|
123
124
|
// 处理 allOf 类型组合
|
|
124
125
|
if (schema.allOf && schema.allOf.length > 0) {
|
|
125
|
-
//
|
|
126
|
+
// 检查是否为泛型模式:第一个是引用类型,第二个定义了扩展属性
|
|
126
127
|
const firstSchema = schema.allOf[0];
|
|
127
|
-
if (firstSchema.$ref) {
|
|
128
|
+
if (firstSchema.$ref && schema.allOf.length > 1) {
|
|
128
129
|
const refName = firstSchema.$ref.split('/').pop();
|
|
129
|
-
|
|
130
|
+
const secondSchema = schema.allOf[1];
|
|
131
|
+
// 检查第二个 schema 是否定义了对象属性(可能没有 type 字段)
|
|
132
|
+
if (secondSchema.properties) {
|
|
133
|
+
// 获取所有扩展属性的类型
|
|
134
|
+
const propertyTypes = [];
|
|
135
|
+
for (const [propName, propSchema] of Object.entries(secondSchema.properties)) {
|
|
136
|
+
const propType = swaggerTypeToTsType(propSchema);
|
|
137
|
+
propertyTypes.push(propType);
|
|
138
|
+
}
|
|
139
|
+
// 如果只有一个属性,直接作为泛型参数
|
|
140
|
+
if (propertyTypes.length === 1) {
|
|
141
|
+
baseType = `${refName}<${propertyTypes[0]}>`;
|
|
142
|
+
}
|
|
143
|
+
// 如果有多个属性,组合成联合类型或对象类型
|
|
144
|
+
else if (propertyTypes.length > 1) {
|
|
145
|
+
const combinedType = `{ ${Object.entries(secondSchema.properties)
|
|
146
|
+
.map(([key, value]) => {
|
|
147
|
+
const optional = secondSchema.required?.includes(key) ? '' : '?';
|
|
148
|
+
const type = swaggerTypeToTsType(value);
|
|
149
|
+
return `${key}${optional}: ${type}`;
|
|
150
|
+
})
|
|
151
|
+
.join('; ')} }`;
|
|
152
|
+
baseType = `${refName}<${combinedType}>`;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
baseType = refName || 'any';
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
baseType = refName || 'any';
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
// 如果不是引用,尝试合并所有类型
|
|
164
|
+
const types = schema.allOf
|
|
165
|
+
.map((s) => swaggerTypeToTsType(s))
|
|
166
|
+
.filter((t) => t !== 'any');
|
|
167
|
+
baseType = types.length > 0 ? types[0] : 'any';
|
|
130
168
|
}
|
|
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
169
|
}
|
|
137
170
|
// 处理引用类型
|
|
138
|
-
if (schema.$ref) {
|
|
171
|
+
else if (schema.$ref) {
|
|
139
172
|
const refName = schema.$ref.split('/').pop();
|
|
140
|
-
|
|
173
|
+
baseType = refName || 'any';
|
|
141
174
|
}
|
|
142
175
|
// 处理数组类型
|
|
143
|
-
if (schema.type === 'array') {
|
|
176
|
+
else if (schema.type === 'array') {
|
|
144
177
|
const itemType = swaggerTypeToTsType(schema.items);
|
|
145
|
-
|
|
178
|
+
baseType = `${itemType}[]`;
|
|
146
179
|
}
|
|
147
180
|
// 处理对象类型
|
|
148
|
-
if (schema.type === 'object') {
|
|
181
|
+
else if (schema.type === 'object') {
|
|
149
182
|
if (schema.properties) {
|
|
150
183
|
const properties = Object.entries(schema.properties)
|
|
151
184
|
.map(([key, value]) => {
|
|
@@ -154,27 +187,43 @@ function swaggerTypeToTsType(schema) {
|
|
|
154
187
|
return ` ${key}${optional}: ${type};`;
|
|
155
188
|
})
|
|
156
189
|
.join('\n');
|
|
157
|
-
|
|
190
|
+
baseType = `{\n${properties}\n}`;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
baseType = 'Record<string, any>';
|
|
158
194
|
}
|
|
159
|
-
return 'Record<string, any>';
|
|
160
195
|
}
|
|
161
196
|
// 处理基本类型
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
197
|
+
else {
|
|
198
|
+
switch (schema.type) {
|
|
199
|
+
case 'integer':
|
|
200
|
+
case 'number':
|
|
201
|
+
baseType = 'number';
|
|
202
|
+
break;
|
|
203
|
+
case 'string':
|
|
204
|
+
if (schema.enum) {
|
|
205
|
+
baseType = schema.enum.map((value) => `'${value}'`).join(' | ');
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
baseType = 'string';
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
case 'boolean':
|
|
212
|
+
baseType = 'boolean';
|
|
213
|
+
break;
|
|
214
|
+
case 'file':
|
|
215
|
+
baseType = 'File';
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
baseType = 'any';
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// 处理 nullable 属性
|
|
223
|
+
if (schema.nullable === true) {
|
|
224
|
+
return `${baseType} | null`;
|
|
177
225
|
}
|
|
226
|
+
return baseType;
|
|
178
227
|
}
|
|
179
228
|
/**
|
|
180
229
|
* 从Swagger参数生成TypeScript参数类型
|
|
@@ -290,11 +339,33 @@ function generateApiComment(operation, parameters) {
|
|
|
290
339
|
comments.push(` * ${operation.description}`);
|
|
291
340
|
}
|
|
292
341
|
if (parameters && parameters.length > 0) {
|
|
293
|
-
|
|
294
|
-
parameters.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
342
|
+
// 收集不同类型的参数
|
|
343
|
+
const queryParams = parameters.filter((p) => p.in === 'query');
|
|
344
|
+
const pathParams = parameters.filter((p) => p.in === 'path');
|
|
345
|
+
const bodyParams = parameters.filter((p) => p.in === 'body');
|
|
346
|
+
const formParams = parameters.filter((p) => p.in === 'formData');
|
|
347
|
+
const hasParams = queryParams.length > 0 || pathParams.length > 0;
|
|
348
|
+
const hasData = bodyParams.length > 0 || formParams.length > 0;
|
|
349
|
+
if (hasParams || hasData) {
|
|
350
|
+
comments.push(' *');
|
|
351
|
+
// 如果有查询参数或路径参数,添加params注释
|
|
352
|
+
if (hasParams) {
|
|
353
|
+
const paramDescriptions = [...pathParams, ...queryParams]
|
|
354
|
+
.map(p => p.description || '')
|
|
355
|
+
.filter(desc => desc)
|
|
356
|
+
.join(', ');
|
|
357
|
+
const description = paramDescriptions || '请求参数';
|
|
358
|
+
comments.push(` * @param params ${description}`);
|
|
359
|
+
}
|
|
360
|
+
// 如果有请求体参数,添加data注释
|
|
361
|
+
if (hasData) {
|
|
362
|
+
const dataParam = bodyParams[0] || formParams[0];
|
|
363
|
+
const description = dataParam?.description || '请求数据';
|
|
364
|
+
comments.push(` * @param data ${description}`);
|
|
365
|
+
}
|
|
366
|
+
// 添加config参数注释
|
|
367
|
+
comments.push(` * @param config 可选的请求配置`);
|
|
368
|
+
}
|
|
298
369
|
}
|
|
299
370
|
if (operation.deprecated) {
|
|
300
371
|
comments.push(' * @deprecated');
|
package/package.json
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "swagger2api-v3",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "从 Swagger/OpenAPI 文档生成 TypeScript API 接口的命令行工具",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"type": "commonjs",
|
|
8
|
-
"bin": {
|
|
9
|
-
"swagger2api-v3": "dist/cli/index.js"
|
|
10
|
-
},
|
|
11
|
-
"files": [
|
|
12
|
-
"dist",
|
|
13
|
-
"README.md",
|
|
14
|
-
"package.json"
|
|
15
|
-
],
|
|
16
|
-
"scripts": {
|
|
17
|
-
"test": "jest",
|
|
18
|
-
"test:watch": "jest --watch",
|
|
19
|
-
"test:coverage": "jest --coverage",
|
|
20
|
-
"test:ci": "jest --ci --coverage --watchAll=false",
|
|
21
|
-
"build": "tsc",
|
|
22
|
-
"clean": "rimraf dist",
|
|
23
|
-
"prebuild": "npm run clean",
|
|
24
|
-
"start": "node dist/cli/index.js",
|
|
25
|
-
"dev": "npm run build && npm start",
|
|
26
|
-
"lint": "prettier --write",
|
|
27
|
-
"generate": "npm run build && node dist/cli/index.js generate",
|
|
28
|
-
"init": "npm run build && node dist/cli/index.js init",
|
|
29
|
-
"validate": "npm run build && node dist/cli/index.js validate",
|
|
30
|
-
"swagger:generate": "npm run generate",
|
|
31
|
-
"swagger:run": "npm run generate",
|
|
32
|
-
"swagger:init": "npm run init",
|
|
33
|
-
"swagger:validate": "npm run validate"
|
|
34
|
-
},
|
|
35
|
-
"keywords": [
|
|
36
|
-
"swagger",
|
|
37
|
-
"openapi",
|
|
38
|
-
"typescript",
|
|
39
|
-
"api",
|
|
40
|
-
"generator",
|
|
41
|
-
"cli",
|
|
42
|
-
"code-generation"
|
|
43
|
-
],
|
|
44
|
-
"author": "xiaoyang",
|
|
45
|
-
"license": "MIT",
|
|
46
|
-
"homepage": "https://github.com/xiaoyang33/swagger2api-v3#readme",
|
|
47
|
-
"repository": {
|
|
48
|
-
"type": "git",
|
|
49
|
-
"url": "https://github.com/xiaoyang33/swagger2api-v3.git"
|
|
50
|
-
},
|
|
51
|
-
"bugs": {
|
|
52
|
-
"url": "https://github.com/xiaoyang33/swagger2api-v3/issues"
|
|
53
|
-
},
|
|
54
|
-
"packageManager": "pnpm@10.11.0",
|
|
55
|
-
"devDependencies": {
|
|
56
|
-
"@types/jest": "^29.5.0",
|
|
57
|
-
"@types/node": "^24.3.1",
|
|
58
|
-
"jest": "^29.5.0",
|
|
59
|
-
"prettier": "^3.6.2",
|
|
60
|
-
"rimraf": "^6.0.1",
|
|
61
|
-
"ts-jest": "^29.1.0",
|
|
62
|
-
"typescript": "^5.9.2"
|
|
63
|
-
},
|
|
64
|
-
"dependencies": {
|
|
65
|
-
"axios": "^1.11.0",
|
|
66
|
-
"commander": "^12.0.0"
|
|
67
|
-
}
|
|
68
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "swagger2api-v3",
|
|
3
|
+
"version": "1.0.8",
|
|
4
|
+
"description": "从 Swagger/OpenAPI 文档生成 TypeScript API 接口的命令行工具",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"bin": {
|
|
9
|
+
"swagger2api-v3": "dist/cli/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "jest",
|
|
18
|
+
"test:watch": "jest --watch",
|
|
19
|
+
"test:coverage": "jest --coverage",
|
|
20
|
+
"test:ci": "jest --ci --coverage --watchAll=false",
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"clean": "rimraf dist",
|
|
23
|
+
"prebuild": "npm run clean",
|
|
24
|
+
"start": "node dist/cli/index.js",
|
|
25
|
+
"dev": "npm run build && npm start",
|
|
26
|
+
"lint": "prettier --write",
|
|
27
|
+
"generate": "npm run build && node dist/cli/index.js generate",
|
|
28
|
+
"init": "npm run build && node dist/cli/index.js init",
|
|
29
|
+
"validate": "npm run build && node dist/cli/index.js validate",
|
|
30
|
+
"swagger:generate": "npm run generate",
|
|
31
|
+
"swagger:run": "npm run generate",
|
|
32
|
+
"swagger:init": "npm run init",
|
|
33
|
+
"swagger:validate": "npm run validate"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"swagger",
|
|
37
|
+
"openapi",
|
|
38
|
+
"typescript",
|
|
39
|
+
"api",
|
|
40
|
+
"generator",
|
|
41
|
+
"cli",
|
|
42
|
+
"code-generation"
|
|
43
|
+
],
|
|
44
|
+
"author": "xiaoyang",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"homepage": "https://github.com/xiaoyang33/swagger2api-v3#readme",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/xiaoyang33/swagger2api-v3.git"
|
|
50
|
+
},
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/xiaoyang33/swagger2api-v3/issues"
|
|
53
|
+
},
|
|
54
|
+
"packageManager": "pnpm@10.11.0",
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/jest": "^29.5.0",
|
|
57
|
+
"@types/node": "^24.3.1",
|
|
58
|
+
"jest": "^29.5.0",
|
|
59
|
+
"prettier": "^3.6.2",
|
|
60
|
+
"rimraf": "^6.0.1",
|
|
61
|
+
"ts-jest": "^29.1.0",
|
|
62
|
+
"typescript": "^5.9.2"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"axios": "^1.11.0",
|
|
66
|
+
"commander": "^12.0.0"
|
|
67
|
+
}
|
|
68
|
+
}
|