swagger2api-v3 1.0.0

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 ADDED
@@ -0,0 +1,471 @@
1
+ # Swagger2API
2
+
3
+ 一个强大的 npm 工具包,用于从 Swagger JSON 文档生成前端接口代码。支持 TypeScript,按标签分组,详细注释生成等功能。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **快速生成**: 从 Swagger JSON 快速生成 TypeScript 接口代码
8
+ - 📁 **按标签分组**: 支持按 Swagger 标签自动分组生成文件
9
+ - 📝 **详细注释**: 自动生成包含描述、参数、返回值的详细注释
10
+ - 🎨 **代码格式化**: 支持自定义 lint 命令,在代码生成完成后统一格式化
11
+ - ⚙️ **灵活配置**: 支持 `.swagger.config.ts` 配置文件
12
+ - 🔧 **CLI 工具**: 提供完整的命令行工具
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ # 使用 pnpm(推荐)
18
+ pnpm add swagger2api
19
+
20
+ # 使用 npm
21
+ npm install swagger2api
22
+
23
+ # 使用 yarn
24
+ yarn add swagger2api
25
+ ```
26
+
27
+ ## 快速开始
28
+
29
+ ### 1. 初始化配置文件
30
+
31
+ ```bash
32
+ # 创建基础配置文件
33
+ npx swagger2api init
34
+ ```
35
+
36
+ ### 2. 配置 `.swagger.config.ts`
37
+
38
+ ```typescript
39
+ const config = {
40
+ // Swagger JSON 文件路径或 URL
41
+ input: 'https://petstore.swagger.io/v2/swagger.json',
42
+
43
+ // 输出目录
44
+ output: './src/api',
45
+
46
+ // request 导入路径模板
47
+ importTemplate: "import { request } from '../../utils/request';",
48
+
49
+ // 生成器类型
50
+ generator: 'typescript',
51
+
52
+ // 按标签分组生成文件
53
+ groupByTags: true,
54
+
55
+ // 代码格式化命令(可选)
56
+ lint: 'npm run lint',
57
+
58
+ // 生成选项
59
+ options: {
60
+ // 是否添加注释
61
+ addComments: true
62
+ }
63
+ };
64
+
65
+ module.exports = config;
66
+ ```
67
+
68
+ ### 3. 生成接口代码
69
+
70
+ ```bash
71
+ # 使用配置文件生成
72
+ npx swagger2api generate
73
+
74
+ # 或者通过 npm script
75
+ npm run swagger:generate
76
+ ```
77
+
78
+ ## NPM 脚本
79
+
80
+ 在 `package.json` 中添加以下脚本:
81
+
82
+ ```json
83
+ {
84
+ "scripts": {
85
+ "swagger:generate": "npx swagger2api generate",
86
+ "swagger:run": "npx swagger2api run",
87
+ "swagger:init": "npx swagger2api init",
88
+ "swagger:validate": "npx swagger2api validate"
89
+ }
90
+ }
91
+ ```
92
+
93
+ ## CLI 命令
94
+
95
+ ### `swagger2api run`
96
+
97
+ 运行 `.swagger.config.ts` 配置文件生成接口代码:
98
+
99
+ ```bash
100
+ # 使用默认配置文件
101
+ swagger2api run
102
+
103
+ # 指定配置文件
104
+ swagger2api run -c ./custom.swagger.config.ts
105
+ ```
106
+
107
+ ### `swagger2api generate`
108
+
109
+ 使用配置文件生成接口代码:
110
+
111
+ ```bash
112
+ # 使用默认配置文件 (.swagger.config.ts)
113
+ swagger2api generate
114
+
115
+ # 或者通过 npm script
116
+ npm run swagger:generate
117
+ ```
118
+
119
+ ### `swagger2api init`
120
+
121
+ 初始化配置文件:
122
+
123
+ ```bash
124
+ # 创建基础配置
125
+ swagger2api init
126
+
127
+ # 创建示例配置
128
+ swagger2api init --example
129
+
130
+ # 指定输出路径
131
+ swagger2api init -o ./config/swagger.config.ts
132
+ ```
133
+
134
+ ### `swagger2api validate`
135
+
136
+ 验证 Swagger 文档:
137
+
138
+ ```bash
139
+ # 验证文件
140
+ swagger2api validate -i ./swagger.json
141
+
142
+ # 使用配置文件验证
143
+ swagger2api validate -c ./.swagger.config.ts
144
+ ```
145
+
146
+ ## 配置选项
147
+
148
+ ### 配置文件结构
149
+
150
+ ```typescript
151
+ interface SwaggerConfig {
152
+ // 必需配置
153
+ input: string; // Swagger JSON 文件路径或 URL
154
+ output: string; // 输出目录
155
+
156
+ // 可选配置
157
+ generator?: 'typescript' | 'javascript'; // 生成器类型,默认 'typescript'
158
+ groupByTags?: boolean; // 是否按标签分组,默认 false
159
+ overwrite?: boolean; // 是否覆盖更新,默认 true
160
+ importTemplate?: string; // request 导入路径模板
161
+ lint?: string; // 代码格式化命令(如:'npm run lint')
162
+
163
+ // 生成选项
164
+ options?: {
165
+ addComments?: boolean; // 添加注释,默认 true
166
+ };
167
+ }
168
+ ```
169
+
170
+ ### 配置说明
171
+
172
+ - **input**: Swagger JSON 文件的路径或 URL
173
+ - **output**: 生成代码的输出目录
174
+ - **generator**: 代码生成器类型,目前支持 'typescript'
175
+ - **groupByTags**: 是否按 Swagger 标签分组生成文件到不同文件夹
176
+ - **overwrite**: 是否覆盖更新,默认为 true。为 true 时会先删除输出目录下的所有文件再重新生成,为 false 时采用增量更新模式
177
+ - **prefix**: 接口路径公共前缀,默认为空字符串。如果配置了此项,生成的所有接口前面都会添加这个前缀
178
+ - **importTemplate**: 自定义 request 函数的导入语句模板
179
+ - **lint**: 可选的代码格式化命令,在所有文件生成完成后执行
180
+ - **options.addComments**: 是否在生成的代码中添加详细注释
181
+
182
+ ## 代码格式化 (Lint)
183
+
184
+ 工具支持在所有文件生成完成后执行自定义的代码格式化命令。
185
+
186
+ ### 配置 Lint
187
+
188
+ 在 `.swagger.config.ts` 中配置 `lint` 字段:
189
+
190
+ ```typescript
191
+ const config = {
192
+ // ... 其他配置
193
+
194
+ // 代码格式化命令
195
+ lint: 'npm run lint', // 或者 'prettier --write' 等
196
+ };
197
+ ```
198
+
199
+ ### Lint 执行时机
200
+
201
+ - **执行时机**: 在所有文件生成完成后统一执行
202
+ - **作用范围**: 对整个输出目录执行格式化
203
+ - **可选配置**: 如果不配置 `lint` 字段,则不会执行任何格式化命令
204
+
205
+ ### 常用 Lint 命令示例
206
+
207
+ ```typescript
208
+ // 使用 Prettier
209
+ lint: 'prettier --write'
210
+
211
+ // 使用 ESLint 修复
212
+ lint: 'eslint --fix'
213
+
214
+ // 使用项目的 npm script
215
+ lint: 'npm run lint'
216
+ lint: 'npm run format'
217
+
218
+ // 使用 pnpm
219
+ lint: 'pnpm run lint'
220
+ ```
221
+
222
+ ### 注意事项
223
+
224
+ - Lint 命令会在生成的输出目录中执行
225
+ - 请确保项目中已安装相应的格式化工具
226
+ - 如果 lint 命令执行失败,会显示错误信息但不会中断生成流程
227
+
228
+ ## 生成的文件结构
229
+
230
+ ### 不分组模式 (groupByTags: false)
231
+
232
+ ```
233
+ src/api/
234
+ ├── types.ts # 数据类型定义
235
+ ├── api.ts # 所有 API 接口
236
+ └── index.ts # 入口文件
237
+ ```
238
+
239
+ ### 按标签分组模式 (groupByTags: true)
240
+
241
+ ```
242
+ src/api/
243
+ ├── types.ts # 数据类型定义
244
+ ├── authcontroller/ # Auth 相关接口
245
+ │ └── index.ts
246
+ ├── usercontroller/ # User 相关接口
247
+ │ └── index.ts
248
+ ├── menucontroller/ # Menu 相关接口
249
+ │ └── index.ts
250
+ └── index.ts # 入口文件(导出所有模块)
251
+ ```
252
+
253
+ ## 生成的代码示例
254
+
255
+ ### 数据类型定义 (types.ts)
256
+
257
+ ```typescript
258
+ /**
259
+ * API 类型定义
260
+ * 此文件由 swagger2api 自动生成,请勿手动修改
261
+ */
262
+ export interface LoginDto {
263
+ /** 账号 */
264
+ account: string;
265
+ /** 密码 */
266
+ password: string;
267
+ }
268
+
269
+ export interface UserInfoRespDto {
270
+ /** 用户ID */
271
+ id: string;
272
+ /** 用户名 */
273
+ username: string;
274
+ /** 角色 */
275
+ roles: string[];
276
+ }
277
+
278
+ export interface LoginRespDto {
279
+ /** 用户信息 */
280
+ user: any;
281
+ /** token */
282
+ token: string;
283
+ }
284
+ ```
285
+
286
+ ### API 接口文件 (authcontroller/index.ts)
287
+
288
+ ```typescript
289
+ /**
290
+ * AuthController API 接口
291
+ * 此文件由 swagger2api 自动生成,请勿手动修改
292
+ */
293
+
294
+ import { request } from '../../utils/request';;
295
+ import type { LoginDto, LoginRespDto } from '../types';
296
+
297
+ /**
298
+ * 登录
299
+ *
300
+ * @param data 登录参数
301
+ * @param config 请求配置
302
+ */
303
+ export const authControllerLogin = (data: LoginDto, config?: any) => {
304
+ return request.post<LoginRespDto>({
305
+ url: '/admin/auth/login',
306
+ data,
307
+ ...config
308
+ });
309
+ }
310
+ ```
311
+
312
+ ### 入口文件 (index.ts)
313
+
314
+ ```typescript
315
+ /**
316
+ * API 入口文件
317
+ * 此文件由 swagger2api 自动生成,请勿手动修改
318
+ */
319
+
320
+ export * from './types';
321
+ export * from './authcontroller';
322
+ export * from './usercontroller';
323
+ export * from './menucontroller';
324
+ ```
325
+
326
+ ## 使用示例
327
+
328
+ ### 基础配置示例
329
+
330
+ ```typescript
331
+ // .swagger.config.ts
332
+ const config = {
333
+ input: 'http://localhost:3000/api/docs/json',
334
+ output: './src/api',
335
+ generator: 'typescript',
336
+ groupByTags: false,
337
+ overwrite: true,
338
+ prefix: '',
339
+ options: {
340
+ addComments: true
341
+ }
342
+ };
343
+
344
+ module.exports = config;
345
+ ```
346
+
347
+ ### 完整配置示例
348
+
349
+ ```typescript
350
+ // .swagger.config.ts
351
+ const config = {
352
+ input: 'https://petstore.swagger.io/v2/swagger.json',
353
+ output: './src/api',
354
+ importTemplate: "import { request } from '../utils/request';",
355
+ generator: 'typescript',
356
+ groupByTags: true,
357
+ overwrite: true,
358
+ prefix: '/api',
359
+ lint: 'prettier --write',
360
+ options: {
361
+ addComments: true
362
+ }
363
+ };
364
+
365
+ module.exports = config;
366
+ ```
367
+
368
+ ## 技术栈
369
+
370
+ - **TypeScript** - 类型安全的 JavaScript
371
+ - **Commander.js** - 命令行工具框架
372
+ - **Prettier** - 代码格式化
373
+ - **Axios** - HTTP 客户端(生成的代码中使用)
374
+
375
+ ## 测试
376
+
377
+ 项目包含完整的测试套件,包括单元测试和集成测试。
378
+
379
+ ### 运行测试
380
+
381
+ ```bash
382
+ # 运行所有测试
383
+ npm test
384
+
385
+ # 运行测试并监听文件变化
386
+ npm run test:watch
387
+
388
+ # 运行测试并生成覆盖率报告
389
+ npm run test:coverage
390
+
391
+ # CI环境下运行测试
392
+ npm run test:ci
393
+ ```
394
+
395
+ ### 测试结构
396
+
397
+ ```
398
+ test/
399
+ ├── config.test.ts # 配置管理测试
400
+ ├── typescript-generator.test.ts # TypeScript生成器测试
401
+ ├── cli.test.ts # CLI功能测试
402
+ ├── integration.test.ts # 集成测试
403
+ └── setup.ts # 测试环境设置
404
+ ```
405
+
406
+ ### 测试覆盖率
407
+
408
+ 项目要求测试覆盖率达到80%以上,包括:
409
+ - 分支覆盖率: 80%
410
+ - 函数覆盖率: 80%
411
+ - 行覆盖率: 80%
412
+ - 语句覆盖率: 80%
413
+
414
+ ## 开发与贡献
415
+
416
+ ### 开发环境设置
417
+
418
+ ```bash
419
+ # 克隆项目
420
+ git clone <repository-url>
421
+ cd swagger2api
422
+
423
+ # 安装依赖
424
+ npm install
425
+
426
+ # 构建项目
427
+ npm run build
428
+
429
+ # 运行测试
430
+ npm test
431
+
432
+ # 启动开发模式(监听文件变化)
433
+ npm run test:watch
434
+ ```
435
+
436
+ ### 代码质量
437
+
438
+ 项目使用以下工具确保代码质量:
439
+
440
+ - **TypeScript**: 类型安全
441
+ - **Prettier**: 代码格式化
442
+ - **Jest**: 测试框架
443
+ - **ESLint**: 代码规范(推荐)
444
+
445
+ ### 贡献指南
446
+
447
+ 1. Fork 项目
448
+ 2. 创建功能分支 (`git checkout -b feature/AmazingFeature`)
449
+ 3. 编写测试用例
450
+ 4. 确保所有测试通过 (`npm test`)
451
+ 5. 确保代码格式正确 (`npm run format`)
452
+ 6. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
453
+ 7. 推送到分支 (`git push origin feature/AmazingFeature`)
454
+ 8. 打开 Pull Request
455
+
456
+ ### 发布流程
457
+
458
+ ```bash
459
+ # 构建项目
460
+ npm run build
461
+
462
+ # 运行所有测试
463
+ npm run test:ci
464
+
465
+ # 发布到npm(需要权限)
466
+ npm publish
467
+ ```
468
+
469
+ ## 许可证
470
+
471
+ MIT License - 查看 [LICENSE](LICENSE) 文件了解详情。
@@ -0,0 +1,11 @@
1
+ /**
2
+ * AuthController API 接口
3
+ * 此文件由 swagger2api 自动生成,请勿手动修改
4
+ */
5
+ import type { LoginDto, LoginRespDto } from '../types';
6
+ /**
7
+ * 登录
8
+ *
9
+ * @param body
10
+ */
11
+ export declare const authControllerLogin: (data: LoginDto, config?: any) => Promise<import("../../utils/request").IReqBoay<LoginRespDto>>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ /**
3
+ * AuthController API 接口
4
+ * 此文件由 swagger2api 自动生成,请勿手动修改
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.authControllerLogin = void 0;
8
+ const request_1 = require("../../utils/request");
9
+ /**
10
+ * 登录
11
+ *
12
+ * @param body
13
+ */
14
+ const authControllerLogin = (data, config) => {
15
+ return request_1.request.post({
16
+ url: '/admin/auth/login',
17
+ data,
18
+ ...config
19
+ });
20
+ };
21
+ exports.authControllerLogin = authControllerLogin;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * API 入口文件
3
+ * 此文件由 swagger2api 自动生成,请勿手动修改
4
+ */
5
+ export * from './types';
6
+ export * from './authcontroller';
7
+ export * from './usercontroller';
8
+ export * from './menucontroller';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ /**
3
+ * API 入口文件
4
+ * 此文件由 swagger2api 自动生成,请勿手动修改
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ __exportStar(require("./types"), exports);
22
+ __exportStar(require("./authcontroller"), exports);
23
+ __exportStar(require("./usercontroller"), exports);
24
+ __exportStar(require("./menucontroller"), exports);
@@ -0,0 +1,9 @@
1
+ /**
2
+ * MenuController API 接口
3
+ * 此文件由 swagger2api 自动生成,请勿手动修改
4
+ */
5
+ import type { MenuListRespDto } from '../types';
6
+ /**
7
+ * 菜单列表
8
+ */
9
+ export declare const menuControllerList: (config?: any) => Promise<import("../../utils/request").IReqBoay<MenuListRespDto>>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ /**
3
+ * MenuController API 接口
4
+ * 此文件由 swagger2api 自动生成,请勿手动修改
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.menuControllerList = void 0;
8
+ const request_1 = require("../../utils/request");
9
+ /**
10
+ * 菜单列表
11
+ */
12
+ const menuControllerList = (config) => {
13
+ return request_1.request.post({
14
+ url: '/admin/system/menus/list',
15
+ ...config
16
+ });
17
+ };
18
+ exports.menuControllerList = menuControllerList;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * API 类型定义
3
+ * 此文件由 swagger2api 自动生成,请勿手动修改
4
+ */
5
+ export interface LoginDto {
6
+ /** 账号 */
7
+ account: string;
8
+ /** 密码 */
9
+ password: string;
10
+ }
11
+ export interface UserInfoRespDto {
12
+ /** 用户ID */
13
+ id: string;
14
+ /** 用户名 */
15
+ username: string;
16
+ /** 角色 */
17
+ roles: string[];
18
+ }
19
+ export interface LoginRespDto {
20
+ /** 用户信息 */
21
+ user: any;
22
+ /** token */
23
+ token: string;
24
+ }
25
+ export interface UserListDto {
26
+ /** 页码 */
27
+ pageNum?: number;
28
+ /** 每页数量 */
29
+ pageSize?: number;
30
+ /** 用户名 */
31
+ username: string;
32
+ }
33
+ export interface UserListRespDto {
34
+ /** 列表 */
35
+ list: UserInfoRespDto[];
36
+ /** 总条数 */
37
+ total: number;
38
+ }
39
+ export interface CreateUserDto {
40
+ /** 用户名 */
41
+ username: string;
42
+ /** 密码 */
43
+ password: string;
44
+ role: {
45
+ id?: string;
46
+ };
47
+ }
48
+ export interface UpdateUserDto {
49
+ /** 用户名 */
50
+ username: string;
51
+ }
52
+ export interface MenuInfoRespDto {
53
+ /** 菜单ID */
54
+ id: string;
55
+ /** 菜单名称 */
56
+ name: string;
57
+ /** 角色 */
58
+ roles: string[];
59
+ }
60
+ export interface MenuListRespDto {
61
+ /** 列表 */
62
+ list: MenuInfoRespDto[];
63
+ /** 总条数 */
64
+ total: number;
65
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,41 @@
1
+ /**
2
+ * UserController API 接口
3
+ * 此文件由 swagger2api 自动生成,请勿手动修改
4
+ */
5
+ import type { CreateUserDto, UpdateUserDto, UserListDto, UserListRespDto } from '../types';
6
+ /**
7
+ * 用户列表
8
+ *
9
+ * @param body
10
+ */
11
+ export declare const userControllerList: (data: UserListDto, config?: any) => Promise<import("../../utils/request").IReqBoay<UserListRespDto>>;
12
+ /**
13
+ * 创建用户
14
+ *
15
+ * @param body
16
+ */
17
+ export declare const userControllerCreate: (data: CreateUserDto, config?: any) => Promise<import("../../utils/request").IReqBoay<any>>;
18
+ /**
19
+ * 更新用户
20
+ *
21
+ * @param body
22
+ */
23
+ export declare const userControllerUpdate: (data: UpdateUserDto, config?: any) => Promise<import("../../utils/request").IReqBoay<any>>;
24
+ /**
25
+ *
26
+ * @param pageNum 页码
27
+ * @param pageSize 每页数量
28
+ * @param username 用户名
29
+ */
30
+ export declare const userControllerDetail: (params: {
31
+ pageNum?: number;
32
+ pageSize?: number;
33
+ username: string;
34
+ }, config?: any) => Promise<import("../../utils/request").IReqBoay<any>>;
35
+ /**
36
+ *
37
+ * @param id
38
+ */
39
+ export declare const userControllerDetail1: (params: {
40
+ id: string;
41
+ }, config?: any) => Promise<import("../../utils/request").IReqBoay<any>>;