swagger2api-v3 1.0.8 → 1.0.9
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 +68 -66
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,39 +1,41 @@
|
|
|
1
1
|
# Swagger2API-v3
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
English | [中文](./README_CN.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A powerful command-line tool for automatically generating TypeScript interface code from Swagger (OAS3.0) documentation.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- 📁 **智能分组** - 支持按 Swagger 标签自动分组生成文件
|
|
9
|
-
- 📝 **详细注释** - 自动生成包含描述、参数、返回值的详细注释
|
|
10
|
-
- 🎨 **代码格式化** - 支持自定义格式化命令
|
|
11
|
-
- ⚙️ **环境适配** - 自动检测项目环境,生成对应格式的配置文件
|
|
12
|
-
- 🔧 **CLI 工具** - 提供完整的命令行工具
|
|
7
|
+
## ✨ Features
|
|
13
8
|
|
|
14
|
-
|
|
9
|
+
- 🚀 **Fast Generation** - Quickly generate TypeScript interface code from Swagger JSON
|
|
10
|
+
- 📁 **Smart Grouping** - Support automatic file grouping by Swagger tags
|
|
11
|
+
- 📝 **Detailed Comments** - Automatically generate detailed comments including descriptions, parameters, and return values
|
|
12
|
+
- 🎨 **Code Formatting** - Support custom formatting commands
|
|
13
|
+
- ⚙️ **Environment Adaptation** - Automatically detect project environment and generate corresponding configuration files
|
|
14
|
+
- 🔧 **CLI Tool** - Provide complete command-line tools
|
|
15
|
+
|
|
16
|
+
## 📦 Installation
|
|
15
17
|
|
|
16
18
|
```bash
|
|
17
|
-
#
|
|
19
|
+
# Global installation
|
|
18
20
|
npm install -g swagger2api-v3
|
|
19
21
|
|
|
20
|
-
#
|
|
22
|
+
# Project dependency
|
|
21
23
|
npm install swagger2api-v3
|
|
22
24
|
```
|
|
23
25
|
|
|
24
|
-
## 🚀
|
|
26
|
+
## 🚀 Quick Start
|
|
25
27
|
|
|
26
|
-
### 1.
|
|
28
|
+
### 1. Initialize Configuration File
|
|
27
29
|
|
|
28
30
|
```bash
|
|
29
31
|
npx swagger2api-v3 init
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
### 2.
|
|
34
|
+
### 2. Configuration File Description
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
The tool automatically generates configuration files in the corresponding format based on the project environment:
|
|
35
37
|
|
|
36
|
-
**CommonJS
|
|
38
|
+
**CommonJS Environment** (`"type": "commonjs"` or not set):
|
|
37
39
|
```javascript
|
|
38
40
|
const config = {
|
|
39
41
|
input: 'https://petstore.swagger.io/v2/swagger.json',
|
|
@@ -52,80 +54,80 @@ const config = {
|
|
|
52
54
|
module.exports = config;
|
|
53
55
|
```
|
|
54
56
|
|
|
55
|
-
**ES
|
|
57
|
+
**ES Module Environment** (`"type": "module"`):
|
|
56
58
|
```javascript
|
|
57
59
|
const config = {
|
|
58
|
-
// ...
|
|
60
|
+
// ... same configuration
|
|
59
61
|
};
|
|
60
62
|
|
|
61
63
|
export default config;
|
|
62
64
|
```
|
|
63
65
|
|
|
64
|
-
### 3.
|
|
66
|
+
### 3. Generate Interface Code
|
|
65
67
|
|
|
66
68
|
```bash
|
|
67
69
|
npx swagger2api-v3 generate
|
|
68
70
|
```
|
|
69
71
|
|
|
70
|
-
## ⚙️
|
|
72
|
+
## ⚙️ Configuration Options
|
|
71
73
|
|
|
72
|
-
|
|
|
73
|
-
|
|
74
|
-
| `input` | string | - | Swagger JSON
|
|
75
|
-
| `output` | string | `'./src/api'` |
|
|
76
|
-
| `generator` | string | `'typescript'` |
|
|
77
|
-
| `groupByTags` | boolean | `true` |
|
|
78
|
-
| `overwrite` | boolean | `true` |
|
|
79
|
-
| `prefix` | string | `''` |
|
|
80
|
-
| `importTemplate` | string | - | request
|
|
81
|
-
| `lint` | string | - |
|
|
82
|
-
| `options.addComments` | boolean | `true` |
|
|
74
|
+
| Option | Type | Default | Description |
|
|
75
|
+
|--------|------|---------|-------------|
|
|
76
|
+
| `input` | string | - | Swagger JSON file path or URL |
|
|
77
|
+
| `output` | string | `'./src/api'` | Output directory for generated code |
|
|
78
|
+
| `generator` | string | `'typescript'` | Code generator type |
|
|
79
|
+
| `groupByTags` | boolean | `true` | Whether to group files by tags |
|
|
80
|
+
| `overwrite` | boolean | `true` | Whether to overwrite existing files |
|
|
81
|
+
| `prefix` | string | `''` | Common prefix for API paths |
|
|
82
|
+
| `importTemplate` | string | - | Import statement template for request function |
|
|
83
|
+
| `lint` | string | - | Code formatting command (optional) |
|
|
84
|
+
| `options.addComments` | boolean | `true` | Whether to add detailed comments |
|
|
83
85
|
|
|
84
|
-
## 📁
|
|
86
|
+
## 📁 Generated File Structure
|
|
85
87
|
|
|
86
|
-
###
|
|
88
|
+
### Grouped by Tags (Recommended)
|
|
87
89
|
|
|
88
90
|
```
|
|
89
91
|
src/api/
|
|
90
|
-
├── types.ts #
|
|
91
|
-
├── user/ # User
|
|
92
|
+
├── types.ts # Data type definitions
|
|
93
|
+
├── user/ # User-related APIs
|
|
92
94
|
│ └── index.ts
|
|
93
|
-
├── auth/ # Auth
|
|
95
|
+
├── auth/ # Auth-related APIs
|
|
94
96
|
│ └── index.ts
|
|
95
|
-
└── index.ts #
|
|
97
|
+
└── index.ts # Entry file
|
|
96
98
|
```
|
|
97
99
|
|
|
98
|
-
###
|
|
100
|
+
### Not Grouped
|
|
99
101
|
|
|
100
102
|
```
|
|
101
103
|
src/api/
|
|
102
|
-
├── types.ts #
|
|
103
|
-
├── api.ts #
|
|
104
|
-
└── index.ts #
|
|
104
|
+
├── types.ts # Data type definitions
|
|
105
|
+
├── api.ts # All API interfaces
|
|
106
|
+
└── index.ts # Entry file
|
|
105
107
|
```
|
|
106
108
|
|
|
107
|
-
## 💡
|
|
109
|
+
## 💡 Usage Examples
|
|
108
110
|
|
|
109
|
-
###
|
|
111
|
+
### Generated Type Definitions
|
|
110
112
|
|
|
111
113
|
```typescript
|
|
112
114
|
// types.ts
|
|
113
115
|
export interface LoginDto {
|
|
114
|
-
/**
|
|
116
|
+
/** Account */
|
|
115
117
|
account: string;
|
|
116
|
-
/**
|
|
118
|
+
/** Password */
|
|
117
119
|
password: string;
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
export interface UserInfo {
|
|
121
|
-
/**
|
|
123
|
+
/** User ID */
|
|
122
124
|
id: string;
|
|
123
|
-
/**
|
|
125
|
+
/** Username */
|
|
124
126
|
username: string;
|
|
125
127
|
}
|
|
126
128
|
```
|
|
127
129
|
|
|
128
|
-
###
|
|
130
|
+
### Generated API Interfaces
|
|
129
131
|
|
|
130
132
|
```typescript
|
|
131
133
|
// authController/index.ts
|
|
@@ -133,9 +135,9 @@ import { request } from '@/utils/request';
|
|
|
133
135
|
import type { LoginDto, LoginRespDto } from '../types';
|
|
134
136
|
|
|
135
137
|
/**
|
|
136
|
-
*
|
|
137
|
-
* @param data
|
|
138
|
-
* @param config
|
|
138
|
+
* Login
|
|
139
|
+
* @param data Login parameters
|
|
140
|
+
* @param config Optional request configuration
|
|
139
141
|
*/
|
|
140
142
|
export const authControllerLoginPost = (data: LoginDto, config?: any) => {
|
|
141
143
|
return request.post<LoginRespDto>({
|
|
@@ -146,25 +148,25 @@ export const authControllerLoginPost = (data: LoginDto, config?: any) => {
|
|
|
146
148
|
};
|
|
147
149
|
```
|
|
148
150
|
|
|
149
|
-
## 🔧 CLI
|
|
151
|
+
## 🔧 CLI Commands
|
|
150
152
|
|
|
151
153
|
```bash
|
|
152
|
-
#
|
|
154
|
+
# Initialize configuration file
|
|
153
155
|
npx swagger2api-v3 init [--force]
|
|
154
156
|
|
|
155
|
-
#
|
|
157
|
+
# Generate interface code
|
|
156
158
|
npx swagger2api-v3 generate [--config <path>]
|
|
157
159
|
|
|
158
|
-
#
|
|
160
|
+
# Validate configuration file
|
|
159
161
|
npx swagger2api-v3 validate [--config <path>]
|
|
160
162
|
|
|
161
|
-
#
|
|
163
|
+
# Show help
|
|
162
164
|
npx swagger2api-v3 --help
|
|
163
165
|
```
|
|
164
166
|
|
|
165
|
-
## 📝 NPM
|
|
167
|
+
## 📝 NPM Scripts
|
|
166
168
|
|
|
167
|
-
|
|
169
|
+
Add to `package.json`:
|
|
168
170
|
|
|
169
171
|
```json
|
|
170
172
|
{
|
|
@@ -176,22 +178,22 @@ npx swagger2api-v3 --help
|
|
|
176
178
|
}
|
|
177
179
|
```
|
|
178
180
|
|
|
179
|
-
## 🎨
|
|
181
|
+
## 🎨 Code Formatting
|
|
180
182
|
|
|
181
|
-
|
|
183
|
+
Support automatic execution of formatting commands after generation:
|
|
182
184
|
|
|
183
185
|
```javascript
|
|
184
|
-
//
|
|
186
|
+
// In configuration file
|
|
185
187
|
const config = {
|
|
186
|
-
// ...
|
|
187
|
-
lint: 'prettier --write' //
|
|
188
|
+
// ... other configurations
|
|
189
|
+
lint: 'prettier --write' // or 'eslint --fix', etc.
|
|
188
190
|
};
|
|
189
191
|
```
|
|
190
192
|
|
|
191
|
-
## 🤝
|
|
193
|
+
## 🤝 Contributing
|
|
192
194
|
|
|
193
|
-
|
|
195
|
+
Issues and Pull Requests are welcome!
|
|
194
196
|
|
|
195
|
-
## 📄
|
|
197
|
+
## 📄 License
|
|
196
198
|
|
|
197
199
|
MIT License
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swagger2api-v3",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "从 Swagger
|
|
3
|
+
"version": "1.0.9",
|
|
4
|
+
"description": "从 Swagger(OAS3.0) 文档生成 TypeScript API 接口的命令行工具",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"type": "commonjs",
|