swagger2api-v3 1.1.0 → 1.1.1

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 CHANGED
@@ -82,6 +82,7 @@ npx swagger2api-v3 generate
82
82
  | `importTemplate` | string | - | Import statement template for request function |
83
83
  | `requestStyle` | 'method' \| 'generic' | `'generic'` | Request call style: `method` uses `request.get/post`, `generic` uses `request({ method })` |
84
84
  | `lint` | string | - | Code formatting command (optional) |
85
+ | `methodNameIgnorePrefix` | string[] | `[]` | Array of prefixes to ignore when generating method names. For example, `['api', 'auth']` will transform `apiGetName` to `getName` and `authUserInfo` to `userInfo` |
85
86
  | `options.addComments` | boolean | `true` | Whether to add detailed comments |
86
87
 
87
88
  ## 📁 Generated File Structure
package/dist/cli/index.js CHANGED
@@ -142,6 +142,11 @@ const config = {
142
142
  // 代码格式化命令(可选)
143
143
  lint: 'prettier --write',
144
144
 
145
+ // 生成方法名时需要忽略的前缀(可选)
146
+ // 例如:配置 ['api', 'auth'] 后,apiGetName 会变成 getName,authUserInfo 会变成 userInfo
147
+ // 如果方法名是 apiAuthGetName,会依次移除所有匹配的前缀,最终变成 getName
148
+ methodNameIgnorePrefix: [],
149
+
145
150
  // 生成选项
146
151
  options: {
147
152
  // 是否添加注释
@@ -76,6 +76,8 @@ class SwaggerParser {
76
76
  const methodSuffix = method.charAt(0).toUpperCase() + method.slice(1).toLowerCase();
77
77
  functionName = functionName + methodSuffix;
78
78
  }
79
+ // 应用前缀忽略规则
80
+ functionName = (0, utils_1.stripMethodNamePrefixes)(functionName, this.config.methodNameIgnorePrefix);
79
81
  // 获取响应类型
80
82
  const responseType = (0, utils_1.getResponseType)(operation.responses);
81
83
  // 获取请求体类型
@@ -29,6 +29,8 @@ export interface SwaggerConfig {
29
29
  importTemplate?: string;
30
30
  /** 格式化代码命令 */
31
31
  lint?: string;
32
+ /** 生成方法名时需要忽略的前缀数组,如 ['api', 'auth'] */
33
+ methodNameIgnorePrefix?: string[];
32
34
  }
33
35
  /**
34
36
  * 标签分组配置
@@ -27,6 +27,13 @@ export declare function toPascalCase(str: string): string;
27
27
  * @returns camelCase字符串
28
28
  */
29
29
  export declare function toCamelCase(str: string): string;
30
+ /**
31
+ * 从方法名中移除指定的前缀
32
+ * @param methodName 方法名
33
+ * @param prefixes 需要移除的前缀数组
34
+ * @returns 移除前缀后的方法名
35
+ */
36
+ export declare function stripMethodNamePrefixes(methodName: string, prefixes?: string[]): string;
30
37
  /**
31
38
  * 将Swagger类型转换为TypeScript类型
32
39
  * @param schema Swagger模式
@@ -40,6 +40,7 @@ exports.pathToFunctionName = pathToFunctionName;
40
40
  exports.toKebabCase = toKebabCase;
41
41
  exports.toPascalCase = toPascalCase;
42
42
  exports.toCamelCase = toCamelCase;
43
+ exports.stripMethodNamePrefixes = stripMethodNamePrefixes;
43
44
  exports.swaggerTypeToTsType = swaggerTypeToTsType;
44
45
  exports.generateParameterTypes = generateParameterTypes;
45
46
  exports.ensureDirectoryExists = ensureDirectoryExists;
@@ -111,6 +112,44 @@ function toCamelCase(str) {
111
112
  const pascalCase = toPascalCase(str);
112
113
  return pascalCase.charAt(0).toLowerCase() + pascalCase.slice(1);
113
114
  }
115
+ /**
116
+ * 从方法名中移除指定的前缀
117
+ * @param methodName 方法名
118
+ * @param prefixes 需要移除的前缀数组
119
+ * @returns 移除前缀后的方法名
120
+ */
121
+ function stripMethodNamePrefixes(methodName, prefixes) {
122
+ if (!prefixes || prefixes.length === 0) {
123
+ return methodName;
124
+ }
125
+ let result = methodName;
126
+ // 循环移除所有匹配的前缀,直到没有前缀可以移除
127
+ let changed = true;
128
+ while (changed) {
129
+ changed = false;
130
+ for (const prefix of prefixes) {
131
+ if (!prefix)
132
+ continue;
133
+ // 将前缀转换为小驼峰格式进行匹配
134
+ const camelPrefix = toCamelCase(prefix);
135
+ // 检查方法名是否以该前缀开头(不区分大小写)
136
+ const lowerMethodName = result.toLowerCase();
137
+ const lowerPrefix = camelPrefix.toLowerCase();
138
+ if (lowerMethodName.startsWith(lowerPrefix)) {
139
+ // 移除前缀,保持后续字符的大小写
140
+ const remaining = result.substring(camelPrefix.length);
141
+ // 如果移除前缀后还有内容,则更新结果
142
+ if (remaining.length > 0) {
143
+ // 确保首字母小写
144
+ result = remaining.charAt(0).toLowerCase() + remaining.slice(1);
145
+ changed = true;
146
+ break; // 重新开始检查
147
+ }
148
+ }
149
+ }
150
+ }
151
+ return result;
152
+ }
114
153
  /**
115
154
  * 将Swagger类型转换为TypeScript类型
116
155
  * @param schema Swagger模式
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger2api-v3",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A command-line tool for generating TypeScript API interfaces from Swagger (OAS 3.0) documentation",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -65,4 +65,4 @@
65
65
  "axios": "^1.11.0",
66
66
  "commander": "^12.0.0"
67
67
  }
68
- }
68
+ }