swagger2api-v3 1.1.2 → 1.1.3
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 +24 -16
- package/dist/cli/index.js +5 -0
- package/dist/core/parser.js +18 -6
- package/dist/types/index.d.ts +2 -0
- package/dist/utils/index.d.ts +10 -2
- package/dist/utils/index.js +65 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,6 +36,7 @@ npx swagger2api-v3 init
|
|
|
36
36
|
The tool automatically generates configuration files in the corresponding format based on the project environment:
|
|
37
37
|
|
|
38
38
|
**CommonJS Environment** (`"type": "commonjs"` or not set):
|
|
39
|
+
|
|
39
40
|
```javascript
|
|
40
41
|
const config = {
|
|
41
42
|
input: 'https://petstore.swagger.io/v2/swagger.json',
|
|
@@ -55,6 +56,7 @@ module.exports = config;
|
|
|
55
56
|
```
|
|
56
57
|
|
|
57
58
|
**ES Module Environment** (`"type": "module"`):
|
|
59
|
+
|
|
58
60
|
```javascript
|
|
59
61
|
const config = {
|
|
60
62
|
// ... same configuration
|
|
@@ -71,19 +73,20 @@ npx swagger2api-v3 generate
|
|
|
71
73
|
|
|
72
74
|
## ⚙️ Configuration Options
|
|
73
75
|
|
|
74
|
-
| Option
|
|
75
|
-
|
|
76
|
-
| `input`
|
|
77
|
-
| `output`
|
|
78
|
-
| `generator`
|
|
79
|
-
| `groupByTags`
|
|
80
|
-
| `overwrite`
|
|
81
|
-
| `prefix`
|
|
82
|
-
| `importTemplate`
|
|
83
|
-
| `requestStyle`
|
|
84
|
-
| `lint`
|
|
85
|
-
| `methodNameIgnorePrefix` | string[]
|
|
86
|
-
| `
|
|
76
|
+
| Option | Type | Default | Description |
|
|
77
|
+
| ------------------------ | --------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
78
|
+
| `input` | string | - | Swagger JSON file path or URL |
|
|
79
|
+
| `output` | string | `'./src/api'` | Output directory for generated code |
|
|
80
|
+
| `generator` | string | `'typescript'` | Code generator type. Supports `'typescript'` and `'javascript'`. `'javascript'` outputs `.js` files and skips type file generation |
|
|
81
|
+
| `groupByTags` | boolean | `true` | Whether to group files by tags |
|
|
82
|
+
| `overwrite` | boolean | `true` | Whether to overwrite existing files |
|
|
83
|
+
| `prefix` | string | `''` | Common prefix for API paths |
|
|
84
|
+
| `importTemplate` | string | - | Import statement template for request function |
|
|
85
|
+
| `requestStyle` | 'method' \| 'generic' | `'generic'` | Request call style: `method` uses `request.get/post`, `generic` uses `request({ method })` |
|
|
86
|
+
| `lint` | string | - | Code formatting command (optional) |
|
|
87
|
+
| `methodNameIgnorePrefix` | string[] | `[]` | Array of prefixes to ignore when generating method names. For example, `['api', 'auth']` will transform `apiGetName` to `getName` and `authUserInfo` to `userInfo` |
|
|
88
|
+
| `addMethodSuffix` | boolean | `true` | Whether to add HTTP method suffix to generated function names. `true` generates `userListPost`, `false` generates `userList` |
|
|
89
|
+
| `options.addComments` | boolean | `true` | Whether to add detailed comments |
|
|
87
90
|
|
|
88
91
|
## 📁 Generated File Structure
|
|
89
92
|
|
|
@@ -119,7 +122,12 @@ Example generated API function (generic style):
|
|
|
119
122
|
|
|
120
123
|
```javascript
|
|
121
124
|
export const codeAuth = (data, config) => {
|
|
122
|
-
return request({
|
|
125
|
+
return request({
|
|
126
|
+
url: '/api/auth/codeAuth',
|
|
127
|
+
method: 'POST',
|
|
128
|
+
data,
|
|
129
|
+
...config
|
|
130
|
+
});
|
|
123
131
|
};
|
|
124
132
|
```
|
|
125
133
|
|
|
@@ -222,7 +230,7 @@ Support automatic execution of formatting commands after generation:
|
|
|
222
230
|
// In configuration file
|
|
223
231
|
const config = {
|
|
224
232
|
// ... other configurations
|
|
225
|
-
lint: 'prettier --write'
|
|
233
|
+
lint: 'prettier --write' // or 'eslint --fix', etc.
|
|
226
234
|
};
|
|
227
235
|
```
|
|
228
236
|
|
|
@@ -232,4 +240,4 @@ Issues and Pull Requests are welcome!
|
|
|
232
240
|
|
|
233
241
|
## 📄 License
|
|
234
242
|
|
|
235
|
-
MIT License
|
|
243
|
+
MIT License
|
package/dist/cli/index.js
CHANGED
|
@@ -147,6 +147,11 @@ const config = {
|
|
|
147
147
|
// 如果方法名是 apiAuthGetName,会依次移除所有匹配的前缀,最终变成 getName
|
|
148
148
|
methodNameIgnorePrefix: [],
|
|
149
149
|
|
|
150
|
+
// 是否在生成的方法名中添加 HTTP method 后缀,默认为 true
|
|
151
|
+
// true: userListPost, userDetailGet
|
|
152
|
+
// false: userList, userDetail
|
|
153
|
+
addMethodSuffix: true,
|
|
154
|
+
|
|
150
155
|
// 生成选项
|
|
151
156
|
options: {
|
|
152
157
|
// 是否添加注释
|
package/dist/core/parser.js
CHANGED
|
@@ -70,11 +70,21 @@ class SwaggerParser {
|
|
|
70
70
|
}
|
|
71
71
|
// 生成函数名
|
|
72
72
|
let functionName = operation.operationId || (0, utils_1.pathToFunctionName)(method, path);
|
|
73
|
-
//
|
|
73
|
+
// 根据配置决定是否添加/保留 HTTP method 后缀
|
|
74
|
+
const shouldAddMethodSuffix = this.config.addMethodSuffix !== false; // 默认为 true
|
|
74
75
|
if (operation.operationId) {
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
// 如果使用了 operationId,根据配置决定是否添加 HTTP 方法后缀
|
|
77
|
+
if (shouldAddMethodSuffix) {
|
|
78
|
+
const methodSuffix = method.charAt(0).toUpperCase() + method.slice(1).toLowerCase();
|
|
79
|
+
functionName = functionName + methodSuffix;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
// 如果没有 operationId,pathToFunctionName 已经包含了 method 后缀
|
|
84
|
+
// 如果配置为 false,需要移除后缀
|
|
85
|
+
if (!shouldAddMethodSuffix) {
|
|
86
|
+
functionName = (0, utils_1.removeMethodSuffix)(functionName, method);
|
|
87
|
+
}
|
|
78
88
|
}
|
|
79
89
|
// 应用前缀忽略规则
|
|
80
90
|
functionName = (0, utils_1.stripMethodNamePrefixes)(functionName, this.config.methodNameIgnorePrefix);
|
|
@@ -129,14 +139,16 @@ class SwaggerParser {
|
|
|
129
139
|
// 解析 Swagger 2.0 definitions
|
|
130
140
|
if (this.document.definitions) {
|
|
131
141
|
for (const [name, schema] of Object.entries(this.document.definitions)) {
|
|
132
|
-
const
|
|
142
|
+
const sanitizedName = (0, utils_1.sanitizeTypeName)(name); // Use it
|
|
143
|
+
const typeInfo = this.parseTypeDefinition(sanitizedName, schema);
|
|
133
144
|
types.push(typeInfo);
|
|
134
145
|
}
|
|
135
146
|
}
|
|
136
147
|
// 解析 OpenAPI 3.0 components.schemas
|
|
137
148
|
if (this.document.components?.schemas) {
|
|
138
149
|
for (const [name, schema] of Object.entries(this.document.components.schemas)) {
|
|
139
|
-
const
|
|
150
|
+
const sanitizedName = (0, utils_1.sanitizeTypeName)(name); // Use it
|
|
151
|
+
const typeInfo = this.parseTypeDefinition(sanitizedName, schema);
|
|
140
152
|
types.push(typeInfo);
|
|
141
153
|
}
|
|
142
154
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -31,6 +31,8 @@ export interface SwaggerConfig {
|
|
|
31
31
|
lint?: string;
|
|
32
32
|
/** 生成方法名时需要忽略的前缀数组,如 ['api', 'auth'] */
|
|
33
33
|
methodNameIgnorePrefix?: string[];
|
|
34
|
+
/** 是否在生成的方法名中添加 HTTP method 后缀,默认为 true。true: userListPost, false: userList */
|
|
35
|
+
addMethodSuffix?: boolean;
|
|
34
36
|
}
|
|
35
37
|
/**
|
|
36
38
|
* 标签分组配置
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SwaggerDocument,
|
|
1
|
+
import { SwaggerDocument, SwaggerParameter } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* 工具函数集合
|
|
4
4
|
*/
|
|
@@ -34,12 +34,19 @@ export declare function toCamelCase(str: string): string;
|
|
|
34
34
|
* @returns 移除前缀后的方法名
|
|
35
35
|
*/
|
|
36
36
|
export declare function stripMethodNamePrefixes(methodName: string, prefixes?: string[]): string;
|
|
37
|
+
/**
|
|
38
|
+
* 从函数名中移除 HTTP method 后缀
|
|
39
|
+
* @param functionName 函数名
|
|
40
|
+
* @param method HTTP 方法
|
|
41
|
+
* @returns 移除后缀后的函数名
|
|
42
|
+
*/
|
|
43
|
+
export declare function removeMethodSuffix(functionName: string, method: string): string;
|
|
37
44
|
/**
|
|
38
45
|
* 将Swagger类型转换为TypeScript类型
|
|
39
46
|
* @param schema Swagger模式
|
|
40
47
|
* @returns TypeScript类型字符串
|
|
41
48
|
*/
|
|
42
|
-
export declare function swaggerTypeToTsType(schema
|
|
49
|
+
export declare function swaggerTypeToTsType(schema: any): string;
|
|
43
50
|
/**
|
|
44
51
|
* 从Swagger参数生成TypeScript参数类型
|
|
45
52
|
* @param parameters Swagger参数数组
|
|
@@ -87,3 +94,4 @@ export declare function sanitizeFilename(filename: string): string;
|
|
|
87
94
|
* @returns TypeScript类型字符串
|
|
88
95
|
*/
|
|
89
96
|
export declare function getResponseType(responses: any): string;
|
|
97
|
+
export declare function sanitizeTypeName(name: string): string;
|
package/dist/utils/index.js
CHANGED
|
@@ -41,6 +41,7 @@ exports.toKebabCase = toKebabCase;
|
|
|
41
41
|
exports.toPascalCase = toPascalCase;
|
|
42
42
|
exports.toCamelCase = toCamelCase;
|
|
43
43
|
exports.stripMethodNamePrefixes = stripMethodNamePrefixes;
|
|
44
|
+
exports.removeMethodSuffix = removeMethodSuffix;
|
|
44
45
|
exports.swaggerTypeToTsType = swaggerTypeToTsType;
|
|
45
46
|
exports.generateParameterTypes = generateParameterTypes;
|
|
46
47
|
exports.ensureDirectoryExists = ensureDirectoryExists;
|
|
@@ -50,6 +51,7 @@ exports.writeFile = writeFile;
|
|
|
50
51
|
exports.generateApiComment = generateApiComment;
|
|
51
52
|
exports.sanitizeFilename = sanitizeFilename;
|
|
52
53
|
exports.getResponseType = getResponseType;
|
|
54
|
+
exports.sanitizeTypeName = sanitizeTypeName;
|
|
53
55
|
const fs = __importStar(require("fs"));
|
|
54
56
|
const path = __importStar(require("path"));
|
|
55
57
|
const axios_1 = __importDefault(require("axios"));
|
|
@@ -150,26 +152,41 @@ function stripMethodNamePrefixes(methodName, prefixes) {
|
|
|
150
152
|
}
|
|
151
153
|
return result;
|
|
152
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* 从函数名中移除 HTTP method 后缀
|
|
157
|
+
* @param functionName 函数名
|
|
158
|
+
* @param method HTTP 方法
|
|
159
|
+
* @returns 移除后缀后的函数名
|
|
160
|
+
*/
|
|
161
|
+
function removeMethodSuffix(functionName, method) {
|
|
162
|
+
const methodSuffix = method.charAt(0).toUpperCase() + method.slice(1).toLowerCase();
|
|
163
|
+
if (functionName.endsWith(methodSuffix)) {
|
|
164
|
+
return functionName.slice(0, -methodSuffix.length);
|
|
165
|
+
}
|
|
166
|
+
return functionName;
|
|
167
|
+
}
|
|
153
168
|
/**
|
|
154
169
|
* 将Swagger类型转换为TypeScript类型
|
|
155
170
|
* @param schema Swagger模式
|
|
156
171
|
* @returns TypeScript类型字符串
|
|
157
172
|
*/
|
|
158
173
|
function swaggerTypeToTsType(schema) {
|
|
159
|
-
if (!schema)
|
|
174
|
+
if (!schema)
|
|
160
175
|
return 'any';
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
if (
|
|
168
|
-
const refName =
|
|
169
|
-
const
|
|
170
|
-
//
|
|
176
|
+
let baseType = 'any';
|
|
177
|
+
// 处理 allOf (通常用于继承或泛型)
|
|
178
|
+
if (schema.allOf) {
|
|
179
|
+
// 简单处理:如果是引用 + 对象定义,可能是泛型包装
|
|
180
|
+
const refSchema = schema.allOf.find((s) => s.$ref);
|
|
181
|
+
const secondSchema = schema.allOf.find((s) => !s.$ref);
|
|
182
|
+
if (refSchema && secondSchema) {
|
|
183
|
+
const refName = refSchema.$ref.split('/').pop();
|
|
184
|
+
const sanitizedRefName = sanitizeTypeName(refName || '');
|
|
185
|
+
// 检查是否是泛型容器 (如 ResOp)
|
|
186
|
+
// 注意:secondSchema 可能没有显式声明 type: 'object',但如果有 properties,则视为对象
|
|
171
187
|
if (secondSchema.properties) {
|
|
172
|
-
//
|
|
188
|
+
// 尝试提取泛型参数类型
|
|
189
|
+
// 这里假设泛型参数是 properties 中的第一个属性
|
|
173
190
|
const propertyTypes = [];
|
|
174
191
|
for (const [propName, propSchema] of Object.entries(secondSchema.properties)) {
|
|
175
192
|
const propType = swaggerTypeToTsType(propSchema);
|
|
@@ -177,7 +194,7 @@ function swaggerTypeToTsType(schema) {
|
|
|
177
194
|
}
|
|
178
195
|
// 如果只有一个属性,直接作为泛型参数
|
|
179
196
|
if (propertyTypes.length === 1) {
|
|
180
|
-
baseType = `${
|
|
197
|
+
baseType = `${sanitizedRefName}<${propertyTypes[0]}>`;
|
|
181
198
|
}
|
|
182
199
|
// 如果有多个属性,组合成联合类型或对象类型
|
|
183
200
|
else if (propertyTypes.length > 1) {
|
|
@@ -188,14 +205,14 @@ function swaggerTypeToTsType(schema) {
|
|
|
188
205
|
return `${key}${optional}: ${type}`;
|
|
189
206
|
})
|
|
190
207
|
.join('; ')} }`;
|
|
191
|
-
baseType = `${
|
|
208
|
+
baseType = `${sanitizedRefName}<${combinedType}>`;
|
|
192
209
|
}
|
|
193
210
|
else {
|
|
194
|
-
baseType =
|
|
211
|
+
baseType = sanitizedRefName || 'any';
|
|
195
212
|
}
|
|
196
213
|
}
|
|
197
214
|
else {
|
|
198
|
-
baseType =
|
|
215
|
+
baseType = sanitizedRefName || 'any';
|
|
199
216
|
}
|
|
200
217
|
}
|
|
201
218
|
else {
|
|
@@ -206,10 +223,29 @@ function swaggerTypeToTsType(schema) {
|
|
|
206
223
|
baseType = types.length > 0 ? types[0] : 'any';
|
|
207
224
|
}
|
|
208
225
|
}
|
|
226
|
+
// 处理 anyOf 或 oneOf
|
|
227
|
+
else if (schema.anyOf || schema.oneOf) {
|
|
228
|
+
const types = (schema.anyOf || schema.oneOf)
|
|
229
|
+
.map((s) => {
|
|
230
|
+
// 特殊处理 type: 'null'
|
|
231
|
+
if (s.type === 'null')
|
|
232
|
+
return 'null';
|
|
233
|
+
return swaggerTypeToTsType(s);
|
|
234
|
+
})
|
|
235
|
+
.filter((t) => t !== 'any');
|
|
236
|
+
// 去重
|
|
237
|
+
const uniqueTypes = Array.from(new Set(types));
|
|
238
|
+
if (uniqueTypes.length > 0) {
|
|
239
|
+
baseType = uniqueTypes.join(' | ');
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
baseType = 'any';
|
|
243
|
+
}
|
|
244
|
+
}
|
|
209
245
|
// 处理引用类型
|
|
210
246
|
else if (schema.$ref) {
|
|
211
247
|
const refName = schema.$ref.split('/').pop();
|
|
212
|
-
baseType = refName || 'any';
|
|
248
|
+
baseType = sanitizeTypeName(refName || 'any');
|
|
213
249
|
}
|
|
214
250
|
// 处理数组类型
|
|
215
251
|
else if (schema.type === 'array') {
|
|
@@ -253,6 +289,9 @@ function swaggerTypeToTsType(schema) {
|
|
|
253
289
|
case 'file':
|
|
254
290
|
baseType = 'File';
|
|
255
291
|
break;
|
|
292
|
+
case 'null': // Add this
|
|
293
|
+
baseType = 'null';
|
|
294
|
+
break;
|
|
256
295
|
default:
|
|
257
296
|
baseType = 'any';
|
|
258
297
|
break;
|
|
@@ -452,3 +491,12 @@ function getResponseType(responses) {
|
|
|
452
491
|
}
|
|
453
492
|
return 'any';
|
|
454
493
|
}
|
|
494
|
+
function sanitizeTypeName(name) {
|
|
495
|
+
if (!name)
|
|
496
|
+
return name;
|
|
497
|
+
// 1. 替换非法字符(包括点号)为下划线
|
|
498
|
+
const replaced = name.replace(/[^a-zA-Z0-9_]/g, '_');
|
|
499
|
+
// 2. 转换为 PascalCase
|
|
500
|
+
// 使用现有的 toPascalCase,它能处理下划线
|
|
501
|
+
return toPascalCase(replaced);
|
|
502
|
+
}
|
package/package.json
CHANGED