@paulpugovkin/api-docs-axios-ts-generator 1.0.16 → 1.1.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 +196 -11
- package/package.json +1 -1
- package/src/index.js +9 -5
package/README.md
CHANGED
|
@@ -16,13 +16,31 @@ Generate TypeScript interfaces and axios classes from OpenAPI (Swagger) specific
|
|
|
16
16
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
19
|
+
### As project dependency
|
|
20
|
+
|
|
19
21
|
```bash
|
|
20
|
-
npm install -
|
|
22
|
+
npm install --save-dev @paulpugovkin/api-docs-axios-ts-generator
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Add script to your `package.json`:
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"scripts": {
|
|
30
|
+
"generate-services": "npx @paulpugovkin/api-docs-axios-ts-generator --config api-docs-generator.config.js"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Now you can run generation:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm run generate-services
|
|
21
39
|
```
|
|
22
40
|
|
|
23
41
|
## Usage
|
|
24
42
|
|
|
25
|
-
### 1.
|
|
43
|
+
### 1. Create configuration file
|
|
26
44
|
|
|
27
45
|
```bash
|
|
28
46
|
npm install --save-dev @paulpugovkin/api-docs-axios-ts-generator
|
|
@@ -48,13 +66,7 @@ npm run generate-services
|
|
|
48
66
|
### 2. Use as global tool
|
|
49
67
|
|
|
50
68
|
```bash
|
|
51
|
-
#
|
|
52
|
-
npm install -g @paulpugovkin/api-docs-axios-ts-generator
|
|
53
|
-
|
|
54
|
-
# Run generation
|
|
55
|
-
api-docs-generator --config api-docs-generator.config.js
|
|
56
|
-
|
|
57
|
-
# Or via npx
|
|
69
|
+
# Run via npx (no installation required)
|
|
58
70
|
npx @paulpugovkin/api-docs-axios-ts-generator --config api-docs-generator.config.js
|
|
59
71
|
```
|
|
60
72
|
|
|
@@ -65,34 +77,207 @@ A default configuration file `api-docs-generator.config.js` is automatically cre
|
|
|
65
77
|
If you need to create it manually, create `api-docs-generator.config.js` file in the root of your project:
|
|
66
78
|
|
|
67
79
|
```javascript
|
|
80
|
+
/**
|
|
81
|
+
* Default configuration file for API Docs Axios TypeScript Generator
|
|
82
|
+
*
|
|
83
|
+
* This file is automatically created during package installation.
|
|
84
|
+
* You can modify this configuration to suit your needs.
|
|
85
|
+
*
|
|
86
|
+
* For more information, visit: https://github.com/PaulPugovkin/api-docs-axios-ts-generator
|
|
87
|
+
*/
|
|
88
|
+
|
|
68
89
|
module.exports = {
|
|
90
|
+
// ========================================
|
|
91
|
+
// API DOCS SOURCE
|
|
92
|
+
// ========================================
|
|
93
|
+
|
|
69
94
|
// URL to fetch OpenAPI specification from
|
|
95
|
+
// Use this option when your API documentation is available via HTTP/HTTPS
|
|
96
|
+
// Alternative: use 'apiDocsPath' for a local file
|
|
70
97
|
apiDocsUrl: 'https://api.example.com/v3/api-docs.json',
|
|
71
98
|
|
|
99
|
+
// Local path to OpenAPI specification file (alternative to apiDocsUrl)
|
|
100
|
+
// Use this option when you have a local JSON/YAML file with API specification
|
|
101
|
+
// Example: './api-docs.json' or './swagger.yaml'
|
|
102
|
+
// apiDocsPath: './api-docs.json',
|
|
103
|
+
|
|
104
|
+
// ========================================
|
|
105
|
+
// OUTPUT DIRECTORIES
|
|
106
|
+
// ========================================
|
|
107
|
+
|
|
72
108
|
// Output directory for generated files
|
|
73
|
-
|
|
109
|
+
// All generated TypeScript files will be placed in this directory
|
|
110
|
+
outputDir: './generated',
|
|
111
|
+
|
|
112
|
+
// Output directory for interfaces (optional)
|
|
113
|
+
// If not specified, interfaces will be placed in outputDir/interfaces
|
|
114
|
+
// interfacesDir: './generated/interfaces',
|
|
115
|
+
|
|
116
|
+
// Output directory for classes (optional)
|
|
117
|
+
// If not specified, classes will be placed in outputDir/classes
|
|
118
|
+
// classesDir: './generated/classes',
|
|
119
|
+
|
|
120
|
+
// ========================================
|
|
121
|
+
// TAG FILTERING
|
|
122
|
+
// ========================================
|
|
74
123
|
|
|
75
124
|
// Tag filtering options
|
|
76
125
|
tags: {
|
|
77
|
-
include
|
|
126
|
+
// Include only these tags (empty array = include all)
|
|
127
|
+
// Use this to generate API client only for specific endpoints
|
|
128
|
+
// Example: ['users', 'products', 'orders']
|
|
129
|
+
include: [],
|
|
130
|
+
|
|
131
|
+
// Exclude these tags
|
|
132
|
+
// Use this to exclude specific endpoints from generation
|
|
133
|
+
// Example: ['admin', 'internal', 'deprecated']
|
|
134
|
+
exclude: [],
|
|
135
|
+
|
|
136
|
+
// Tag prefix to remove from generated class names
|
|
137
|
+
// If your API tags have a common prefix, it will be removed from class names
|
|
138
|
+
// Example: If prefix is 'api_tag_' and tag is 'api_tag_users',
|
|
139
|
+
// the class name will be 'UsersApi'
|
|
140
|
+
prefix: 'api_tag_',
|
|
78
141
|
},
|
|
79
142
|
|
|
143
|
+
// ========================================
|
|
144
|
+
// GROUPING & CLASS GENERATION
|
|
145
|
+
// ========================================
|
|
146
|
+
|
|
80
147
|
// Grouping mode for API methods
|
|
148
|
+
// 'tag' - group by tags (creates multiple classes, one per tag)
|
|
149
|
+
// 'all' - all methods in one class
|
|
150
|
+
// 'path' - group by API paths (creates classes based on URL paths)
|
|
81
151
|
groupBy: 'tag',
|
|
82
152
|
|
|
83
153
|
// Class generation mode
|
|
154
|
+
// 'single' - one class for all methods (useful with groupBy: 'all')
|
|
155
|
+
// 'multiple' - multiple classes based on grouping (useful with groupBy: 'tag')
|
|
84
156
|
classMode: 'multiple',
|
|
85
157
|
|
|
158
|
+
// ========================================
|
|
159
|
+
// NAMING CONVENTIONS
|
|
160
|
+
// ========================================
|
|
161
|
+
|
|
162
|
+
// Naming functions for generated entities
|
|
163
|
+
// Customize how classes, interfaces, and methods are named
|
|
164
|
+
naming: {
|
|
165
|
+
// Function to generate class name from tag
|
|
166
|
+
// Receives the tag name as parameter
|
|
167
|
+
// Example: (tag) => tag.replace('api_tag_', '') + 'Api'
|
|
168
|
+
className: (tag) => tag.replace('api_tag_', '') + 'Api',
|
|
169
|
+
|
|
170
|
+
// Function to generate interface name from schema name
|
|
171
|
+
// Receives the schema name as parameter
|
|
172
|
+
// Example: (name) => name
|
|
173
|
+
interfaceName: (name) => name,
|
|
174
|
+
|
|
175
|
+
// Function to generate method name from operationId
|
|
176
|
+
// Receives the operationId as parameter
|
|
177
|
+
// Example: (operationId) => operationId
|
|
178
|
+
methodName: (operationId) => operationId,
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
// ========================================
|
|
182
|
+
// IMPORT PATHS
|
|
183
|
+
// ========================================
|
|
184
|
+
|
|
185
|
+
// Import paths configuration
|
|
186
|
+
imports: {
|
|
187
|
+
// Path to import axios from
|
|
188
|
+
// Default: 'axios'
|
|
189
|
+
// Change this if you want to use a custom axios instance or path
|
|
190
|
+
axiosPath: 'axios',
|
|
191
|
+
|
|
192
|
+
// Path to import BASE_URL constant from
|
|
193
|
+
// This is used when generateAxiosConfig is true
|
|
194
|
+
// The generated axios config file will be imported from this path
|
|
195
|
+
// Example: './config/axios/axios' or './src/config/axios'
|
|
196
|
+
baseUrlPath: './config/axios/axios',
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
// ========================================
|
|
200
|
+
// GENERATION OPTIONS
|
|
201
|
+
// ========================================
|
|
202
|
+
|
|
86
203
|
// Generation options
|
|
87
204
|
options: {
|
|
205
|
+
// Generate JSDoc comments for methods
|
|
206
|
+
// Adds documentation comments to generated methods based on OpenAPI descriptions
|
|
207
|
+
// Default: true
|
|
208
|
+
generateJSDoc: true,
|
|
209
|
+
|
|
210
|
+
// Generate index files for better imports
|
|
211
|
+
// Creates index.ts files in each directory for easier imports
|
|
212
|
+
// Example: import { UsersApi } from './generated/classes'
|
|
213
|
+
// Default: true
|
|
214
|
+
generateIndexFiles: true,
|
|
215
|
+
|
|
216
|
+
// Clean output directory before generation
|
|
217
|
+
// Removes all files in the output directory before generating new files
|
|
218
|
+
// Warning: This will delete all existing files in the output directory!
|
|
219
|
+
// Default: true
|
|
220
|
+
cleanOutputDir: true,
|
|
221
|
+
|
|
222
|
+
// Generate axios configuration file
|
|
223
|
+
// Creates an axios configuration file with interceptors and base settings
|
|
224
|
+
// Default: true
|
|
88
225
|
generateAxiosConfig: true,
|
|
226
|
+
|
|
227
|
+
// Allow spreading user axios configurations in method calls
|
|
228
|
+
// When true, API methods accept a second parameter for custom axios config
|
|
229
|
+
// Example: usersApi.getUserById(123, { headers: { 'X-Custom': 'value' } })
|
|
230
|
+
// Default: true
|
|
89
231
|
allowAxiosConfigSpread: true,
|
|
90
232
|
},
|
|
91
233
|
|
|
234
|
+
// ========================================
|
|
235
|
+
// AXIOS CONFIGURATION
|
|
236
|
+
// ========================================
|
|
237
|
+
|
|
92
238
|
// Axios configuration settings
|
|
239
|
+
// These settings will be used in the generated axios configuration
|
|
93
240
|
axios: {
|
|
241
|
+
// Base URL for all API requests
|
|
242
|
+
// This will be prepended to all request URLs
|
|
243
|
+
// Example: 'https://api.example.com' or 'https://api.example.com/v1'
|
|
94
244
|
baseURL: 'https://api.example.com',
|
|
245
|
+
|
|
246
|
+
// Request timeout in milliseconds
|
|
247
|
+
// How long to wait for a response before timing out
|
|
248
|
+
// Example: 30000 (30 seconds), 60000 (1 minute)
|
|
95
249
|
timeout: 30000,
|
|
250
|
+
|
|
251
|
+
// Default headers for all requests
|
|
252
|
+
// These headers will be included in every API request
|
|
253
|
+
// You can add authentication headers, content types, etc.
|
|
254
|
+
headers: {
|
|
255
|
+
'Content-Type': 'application/json',
|
|
256
|
+
// 'X-API-Key': 'your-api-key',
|
|
257
|
+
// 'Authorization': 'Bearer your-token',
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
// Send cookies with cross-domain requests
|
|
261
|
+
// Set to true if your API requires cookies for authentication
|
|
262
|
+
// Default: false
|
|
263
|
+
withCredentials: false,
|
|
264
|
+
|
|
265
|
+
// Response type (optional)
|
|
266
|
+
// Expected response type: 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
|
|
267
|
+
// Default: 'json'
|
|
268
|
+
// responseType: 'json',
|
|
269
|
+
|
|
270
|
+
// Transform request data before sending (optional)
|
|
271
|
+
// Function to transform request data before it's sent to the server
|
|
272
|
+
// transformRequest: [(data) => data],
|
|
273
|
+
|
|
274
|
+
// Transform response data after receiving (optional)
|
|
275
|
+
// Function to transform response data after it's received from the server
|
|
276
|
+
// transformResponse: [(data) => data],
|
|
277
|
+
|
|
278
|
+
// Validate status code (optional)
|
|
279
|
+
// Set to false to reject the promise if the status code is not 2xx
|
|
280
|
+
// validateStatus: (status) => status >= 200 && status < 300,
|
|
96
281
|
},
|
|
97
282
|
};
|
|
98
283
|
```
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -57,11 +57,15 @@ async function loadConfig(configPath, cliOptions = {}) {
|
|
|
57
57
|
throw new Error(`Failed to load configuration: ${error.message}`);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
// Merge CLI options with config
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
// Merge CLI options with config, filtering out undefined values
|
|
61
|
+
const mergedConfig = { ...config };
|
|
62
|
+
Object.keys(cliOptions).forEach(key => {
|
|
63
|
+
if (cliOptions[key] !== undefined) {
|
|
64
|
+
mergedConfig[key] = cliOptions[key];
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return mergedConfig;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
// Основной инструмент CLI
|