swagger2api-v3 1.0.7 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Swagger2API-v3
2
2
 
3
- 一个强大的命令行工具,用于从 Swagger/OpenAPI 文档自动生成 TypeScript 接口代码。
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
  ## ⚙️ 配置选项
@@ -150,16 +150,16 @@ export const authControllerLoginPost = (data: LoginDto, config?: any) => {
150
150
 
151
151
  ```bash
152
152
  # 初始化配置文件
153
- swagger2api-v3 init [--force]
153
+ npx swagger2api-v3 init [--force]
154
154
 
155
155
  # 生成接口代码
156
- swagger2api-v3 generate [--config <path>]
156
+ npx swagger2api-v3 generate [--config <path>]
157
157
 
158
158
  # 验证配置文件
159
- swagger2api-v3 validate [--config <path>]
159
+ npx swagger2api-v3 validate [--config <path>]
160
160
 
161
161
  # 查看帮助
162
- swagger2api-v3 --help
162
+ npx swagger2api-v3 --help
163
163
  ```
164
164
 
165
165
  ## 📝 NPM 脚本
@@ -170,7 +170,8 @@ swagger2api-v3 --help
170
170
  {
171
171
  "scripts": {
172
172
  "api:generate": "swagger2api-v3 generate",
173
- "api:init": "swagger2api-v3 init"
173
+ "api:init": "swagger2api-v3 init",
174
+ "api:validate": "swagger2api-v3 validate"
174
175
  }
175
176
  }
176
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');
@@ -120,6 +120,7 @@ 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
  // 检查是否为泛型模式:第一个是引用类型,第二个定义了扩展属性
@@ -137,7 +138,7 @@ function swaggerTypeToTsType(schema) {
137
138
  }
138
139
  // 如果只有一个属性,直接作为泛型参数
139
140
  if (propertyTypes.length === 1) {
140
- return `${refName}<${propertyTypes[0]}>`;
141
+ baseType = `${refName}<${propertyTypes[0]}>`;
141
142
  }
142
143
  // 如果有多个属性,组合成联合类型或对象类型
143
144
  else if (propertyTypes.length > 1) {
@@ -148,29 +149,36 @@ function swaggerTypeToTsType(schema) {
148
149
  return `${key}${optional}: ${type}`;
149
150
  })
150
151
  .join('; ')} }`;
151
- return `${refName}<${combinedType}>`;
152
+ baseType = `${refName}<${combinedType}>`;
152
153
  }
154
+ else {
155
+ baseType = refName || 'any';
156
+ }
157
+ }
158
+ else {
159
+ baseType = refName || 'any';
153
160
  }
154
- return refName || 'any';
155
161
  }
156
- // 如果不是引用,尝试合并所有类型
157
- const types = schema.allOf
158
- .map((s) => swaggerTypeToTsType(s))
159
- .filter((t) => t !== 'any');
160
- return types.length > 0 ? types[0] : 'any';
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';
168
+ }
161
169
  }
162
170
  // 处理引用类型
163
- if (schema.$ref) {
171
+ else if (schema.$ref) {
164
172
  const refName = schema.$ref.split('/').pop();
165
- return refName || 'any';
173
+ baseType = refName || 'any';
166
174
  }
167
175
  // 处理数组类型
168
- if (schema.type === 'array') {
176
+ else if (schema.type === 'array') {
169
177
  const itemType = swaggerTypeToTsType(schema.items);
170
- return `${itemType}[]`;
178
+ baseType = `${itemType}[]`;
171
179
  }
172
180
  // 处理对象类型
173
- if (schema.type === 'object') {
181
+ else if (schema.type === 'object') {
174
182
  if (schema.properties) {
175
183
  const properties = Object.entries(schema.properties)
176
184
  .map(([key, value]) => {
@@ -179,27 +187,43 @@ function swaggerTypeToTsType(schema) {
179
187
  return ` ${key}${optional}: ${type};`;
180
188
  })
181
189
  .join('\n');
182
- return `{\n${properties}\n}`;
190
+ baseType = `{\n${properties}\n}`;
191
+ }
192
+ else {
193
+ baseType = 'Record<string, any>';
183
194
  }
184
- return 'Record<string, any>';
185
195
  }
186
196
  // 处理基本类型
187
- switch (schema.type) {
188
- case 'integer':
189
- case 'number':
190
- return 'number';
191
- case 'string':
192
- if (schema.enum) {
193
- return schema.enum.map((value) => `'${value}'`).join(' | ');
194
- }
195
- return 'string';
196
- case 'boolean':
197
- return 'boolean';
198
- case 'file':
199
- return 'File';
200
- default:
201
- return 'any';
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`;
202
225
  }
226
+ return baseType;
203
227
  }
204
228
  /**
205
229
  * 从Swagger参数生成TypeScript参数类型
@@ -315,11 +339,33 @@ function generateApiComment(operation, parameters) {
315
339
  comments.push(` * ${operation.description}`);
316
340
  }
317
341
  if (parameters && parameters.length > 0) {
318
- comments.push(' *');
319
- parameters.forEach((param) => {
320
- const description = param.description || '';
321
- comments.push(` * @param ${param.name} ${description}`);
322
- });
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
+ }
323
369
  }
324
370
  if (operation.deprecated) {
325
371
  comments.push(' * @deprecated');
package/package.json CHANGED
@@ -1,68 +1,68 @@
1
- {
2
- "name": "swagger2api-v3",
3
- "version": "1.0.7",
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
+ }