@paulpugovkin/api-docs-axios-ts-generator 1.1.0 → 1.2.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 CHANGED
@@ -1,495 +1,237 @@
1
- # API Docs Axios TS Generator
2
-
3
- Generate TypeScript interfaces and axios classes from OpenAPI (Swagger) specifications.
4
-
5
- ## Features
6
-
7
- - 🚀 Generate TypeScript interfaces from OpenAPI schemas
8
- - 📦 Generate axios classes with API methods
9
- - 🔧 Flexible configuration via TypeScript file
10
- - 🏷️ Tag filtering (include/exclude)
11
- - 📁 Generation modes: single class or multiple by tags
12
- - ⚙️ Automatic axios configuration generation
13
- - 🎯 Support for spreading user axios configurations
14
- - 📝 JSDoc comments for methods
15
- - 🔄 Backward compatibility with CLI arguments
16
-
17
- ## Installation
18
-
19
- ### As project dependency
20
-
21
- ```bash
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
39
- ```
40
-
41
- ## Usage
42
-
43
- ### 1. Create configuration file
44
-
45
- ```bash
46
- npm install --save-dev @paulpugovkin/api-docs-axios-ts-generator
47
- ```
48
-
49
- Add script to your `package.json`:
50
-
51
- ```json
52
- {
53
- "scripts": {
54
- "generate-services": "npx @paulpugovkin/api-docs-axios-ts-generator --config api-docs-generator.config.js"
55
- }
56
- }
57
- ```
58
-
59
- Now you can run generation:
60
-
61
- ```bash
62
- npm run generate-services
63
- ```
64
-
65
-
66
- ### 2. Use as global tool
67
-
68
- ```bash
69
- # Run via npx (no installation required)
70
- npx @paulpugovkin/api-docs-axios-ts-generator --config api-docs-generator.config.js
71
- ```
72
-
73
- ### 3. Create configuration file
74
-
75
- A default configuration file `api-docs-generator.config.js` is automatically created in your project root during installation. You can modify this file to suit your needs.
76
-
77
- If you need to create it manually, create `api-docs-generator.config.js` file in the root of your project:
78
-
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
-
89
- module.exports = {
90
- // ========================================
91
- // API DOCS SOURCE
92
- // ========================================
93
-
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
97
- apiDocsUrl: 'https://api.example.com/v3/api-docs.json',
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
-
108
- // Output directory for generated files
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
- // ========================================
123
-
124
- // Tag filtering options
125
- tags: {
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_',
141
- },
142
-
143
- // ========================================
144
- // GROUPING & CLASS GENERATION
145
- // ========================================
146
-
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)
151
- groupBy: 'tag',
152
-
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')
156
- classMode: 'multiple',
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
-
203
- // Generation options
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
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
231
- allowAxiosConfigSpread: true,
232
- },
233
-
234
- // ========================================
235
- // AXIOS CONFIGURATION
236
- // ========================================
237
-
238
- // Axios configuration settings
239
- // These settings will be used in the generated axios configuration
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'
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)
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,
281
- },
282
- };
283
- ```
284
-
285
- ### 4. Use generated API
286
-
287
- ```typescript
288
- import { UsersApi, ProductsApi } from './src/api';
289
-
290
- // Use API
291
- const usersApi = new UsersApi();
292
- const productsApi = new ProductsApi();
293
-
294
- // Get user
295
- const user = await usersApi.getUserById(123);
296
-
297
- // With custom axios configuration
298
- const user = await usersApi.getUserById(123, {
299
- headers: {
300
- 'X-Custom-Header': 'value',
301
- },
302
- timeout: 5000,
303
- });
304
- ```
305
-
306
- ## Configuration
307
-
308
- ### Main parameters
309
-
310
- | Parameter | Type | Required | Description |
311
- |-----------|------|--------------|------------|
312
- | `apiDocsUrl` | `string` | No* | URL to fetch OpenAPI specification |
313
- | `apiDocsPath` | `string` | No* | Local path to OpenAPI specification file |
314
- | `outputDir` | `string` | Yes | Output directory for generated files |
315
-
316
- *One of `apiDocsUrl` or `apiDocsPath` is required.
317
-
318
- ### Tag filtering
319
-
320
- ```typescript
321
- tags: {
322
- include?: string[]; // Tags to include
323
- exclude?: string[]; // Tags to exclude
324
- prefix?: string; // Tag prefix (default "api_tag_")
325
- }
326
- ```
327
-
328
- ### Grouping modes
329
-
330
- ```typescript
331
- groupBy: 'tag' | 'all' | 'path';
332
- ```
333
-
334
- - `'tag'` - grouping by tags (multiple classes)
335
- - `'all'` - all methods in one class
336
- - `'path'` - grouping by API paths
337
-
338
- ### Class generation modes
339
-
340
- ```typescript
341
- classMode: 'single' | 'multiple';
342
- ```
343
-
344
- - `'single'` - one class for all methods
345
- - `'multiple'` - multiple classes by grouping
346
-
347
- ### Naming options
348
-
349
- ```typescript
350
- naming: {
351
- className?: (tag: string) => string;
352
- interfaceName?: (name: string) => string;
353
- methodName?: (operationId: string) => string;
354
- }
355
- ```
356
-
357
- ### Import options
358
-
359
- ```typescript
360
- imports: {
361
- axiosPath?: string; // Path to import axios (default 'axios')
362
- baseUrlPath?: string; // Path to import BASE_URL
363
- }
364
- ```
365
-
366
- ### Generation options
367
-
368
- ```typescript
369
- options: {
370
- generateJSDoc?: boolean; // Generate JSDoc (default true)
371
- generateIndexFiles?: boolean; // Generate index files (default true)
372
- cleanOutputDir?: boolean; // Clean output directory (default true)
373
- generateAxiosConfig?: boolean; // Generate axios configuration (default true)
374
- allowAxiosConfigSpread?: boolean; // Allow spreading axios configs (default true)
375
- }
376
- ```
377
-
378
- ### Axios settings
379
-
380
- ```typescript
381
- axios: {
382
- baseURL?: string;
383
- timeout?: number;
384
- headers?: Record<string, string>;
385
- withCredentials?: boolean;
386
- }
387
- ```
388
-
389
- ## Spreading user axios configurations
390
-
391
- When `allowAxiosConfigSpread: true` is enabled, API methods support passing user configurations:
392
-
393
- ```typescript
394
- // Generated method
395
- async getUserById(id: number, config?: AxiosRequestConfig): Promise<AxiosResponse<User>>
396
-
397
- // Usage
398
- const response = await usersApi.getUserById(123, {
399
- headers: {
400
- 'X-Custom-Header': 'value',
401
- },
402
- timeout: 5000,
403
- params: {
404
- fields: 'id,name,email',
405
- },
406
- });
407
- ```
408
-
409
- ## Generated files structure
410
-
411
- ```
412
- outputDir/
413
- ├── config/
414
- │ └── axios/
415
- │ └── axios.ts # Axios configuration with interceptors
416
- ├── interfaces/
417
- │ ├── User.ts
418
- │ ├── Product.ts
419
- │ └── index.ts
420
- ├── classes/
421
- │ ├── UsersApi.ts
422
- │ ├── ProductsApi.ts
423
- │ └── index.ts
424
- └── index.ts
425
- ```
426
-
427
- ## CLI options
428
-
429
- ```
430
- Usage: api-docs-generator [options]
431
-
432
- Options:
433
- -V, --version output the version number
434
- -c, --config <path> Path to configuration file
435
- --api-docs-url <url> URL to fetch OpenAPI documentation
436
- --api-docs-path <path> Local path to OpenAPI documentation file
437
- --output-dir <dir> Output directory for generated files
438
- --clean Clean output directory before generation
439
- -h, --help display help for command
440
- ```
441
-
442
- ## Configuration examples
443
-
444
- ### Generate by tags (multiple classes)
445
-
446
- ```typescript
447
- {
448
- tags: {
449
- include: ['api_tag_users', 'api_tag_products'],
450
- },
451
- groupBy: 'tag',
452
- classMode: 'multiple',
453
- }
454
- ```
455
-
456
- ### Generate single class
457
-
458
- ```typescript
459
- {
460
- groupBy: 'all',
461
- classMode: 'single',
462
- }
463
- ```
464
-
465
- ### Exclude tags
466
-
467
- ```typescript
468
- {
469
- tags: {
470
- exclude: ['api_tag_internal', 'api_tag_admin'],
471
- },
472
- }
473
- ```
474
-
475
- ### Generate with axios configuration
476
-
477
- ```typescript
478
- {
479
- options: {
480
- generateAxiosConfig: true,
481
- },
482
- axios: {
483
- baseURL: 'https://api.example.com',
484
- timeout: 30000,
485
- headers: {
486
- 'Content-Type': 'application/json',
487
- 'X-API-Key': 'your-api-key',
488
- },
489
- },
490
- }
491
- ```
492
-
493
- ## License
494
-
495
- MIT
1
+ # API Docs Axios TS Generator
2
+
3
+ Generate TypeScript interfaces and axios classes from OpenAPI (Swagger) specifications.
4
+
5
+ ## Features
6
+
7
+ - Generate TypeScript interfaces from OpenAPI schemas
8
+ - Generate axios classes with API methods
9
+ - Flexible configuration via TypeScript file
10
+ - Tag filtering (include/exclude)
11
+ - Generation modes: single class or multiple by tags
12
+ - Automatic axios configuration generation
13
+ - Support for spreading user axios configurations
14
+ - JSDoc comments for methods
15
+ - Backward compatibility with CLI arguments
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install --save-dev @paulpugovkin/api-docs-axios-ts-generator
21
+ ```
22
+
23
+ Add script to your `package.json`:
24
+
25
+ ```json
26
+ {
27
+ "scripts": {
28
+ "generate-services": "api-docs-generator --config api-docs-generator.config.js"
29
+ }
30
+ }
31
+ ```
32
+
33
+ Now generate your API client:
34
+
35
+ ```bash
36
+ npm run generate-services
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Create configuration file
42
+
43
+ A default configuration file `api-docs-generator.config.js` is automatically created in your project root during installation. You can modify this file to suit your needs.
44
+
45
+ If you need to create it manually, see the template at `src/templates/default.config.template.js` or the [Configuration](#configuration) reference below.
46
+
47
+ ## Generated API usage
48
+
49
+ ```typescript
50
+ import { UsersApi, ProductsApi } from './generated';
51
+
52
+ // Use API
53
+ const usersApi = new UsersApi();
54
+ const productsApi = new ProductsApi();
55
+
56
+ // Get user
57
+ const user = await usersApi.getUserById(123);
58
+
59
+ // With custom axios configuration
60
+ const user = await usersApi.getUserById(123, {
61
+ headers: {
62
+ 'X-Custom-Header': 'value',
63
+ },
64
+ timeout: 5000,
65
+ });
66
+ ```
67
+
68
+ ## Configuration
69
+
70
+ ### Main parameters
71
+
72
+ | Parameter | Type | Required | Description |
73
+ |-----------|------|--------------|------------|
74
+ | `apiDocsUrl` | `string` | No* | URL to fetch OpenAPI specification |
75
+ | `apiDocsPath` | `string` | No* | Local path to OpenAPI specification file |
76
+ | `outputDir` | `string` | Yes | Output directory for generated files |
77
+
78
+ *One of `apiDocsUrl` or `apiDocsPath` is required.
79
+
80
+ ### Tag filtering
81
+
82
+ ```typescript
83
+ tags: {
84
+ include?: string[]; // Tags to include
85
+ exclude?: string[]; // Tags to exclude
86
+ prefix?: string; // Tag prefix (default "api_tag_")
87
+ }
88
+ ```
89
+
90
+ ### Grouping modes
91
+
92
+ ```typescript
93
+ groupBy: 'tag' | 'all' | 'path';
94
+ ```
95
+
96
+ - `'tag'` - grouping by tags (multiple classes)
97
+ - `'all'` - all methods in one class
98
+ - `'path'` - grouping by API paths
99
+
100
+ ### Class generation modes
101
+
102
+ ```typescript
103
+ classMode: 'single' | 'multiple';
104
+ ```
105
+
106
+ - `'single'` - one class for all methods
107
+ - `'multiple'` - multiple classes by grouping
108
+
109
+ ### Naming options
110
+
111
+ ```typescript
112
+ naming: {
113
+ className?: (tag: string) => string;
114
+ interfaceName?: (name: string) => string;
115
+ methodName?: (operationId: string) => string;
116
+ }
117
+ ```
118
+
119
+ ### Import options
120
+
121
+ ```typescript
122
+ imports: {
123
+ axiosPath?: string; // Path to import axios (default 'axios')
124
+ baseUrlPath?: string; // Path to import BASE_URL
125
+ }
126
+ ```
127
+
128
+ ### Generation options
129
+
130
+ ```typescript
131
+ options: {
132
+ generateJSDoc?: boolean; // Generate JSDoc (default true)
133
+ generateIndexFiles?: boolean; // Generate index files (default true)
134
+ cleanOutputDir?: boolean; // Clean output directory (default true)
135
+ generateAxiosConfig?: boolean; // Generate axios configuration (default true)
136
+ allowAxiosConfigSpread?: boolean; // Allow spreading axios configs (default true)
137
+ }
138
+ ```
139
+
140
+ ### Axios settings
141
+
142
+ ```typescript
143
+ axios: {
144
+ baseURL?: string;
145
+ timeout?: number;
146
+ headers?: Record<string, string>;
147
+ withCredentials?: boolean;
148
+ }
149
+ ```
150
+
151
+ ## Generated files structure
152
+
153
+ ```
154
+ outputDir/
155
+ ├── config/
156
+ │ └── axios/
157
+ │ └── axios.ts # Axios configuration with interceptors
158
+ ├── interfaces/
159
+ │ ├── User.ts
160
+ │ ├── Product.ts
161
+ │ └── index.ts
162
+ ├── classes/
163
+ │ ├── UsersApi.ts
164
+ │ ├── ProductsApi.ts
165
+ │ └── index.ts
166
+ └── index.ts
167
+ ```
168
+
169
+ ## CLI options
170
+
171
+ ```
172
+ Usage: api-docs-generator [options]
173
+
174
+ Options:
175
+ -V, --version output the version number
176
+ -c, --config <path> Path to configuration file
177
+ --api-docs-url <url> URL to fetch OpenAPI documentation
178
+ --api-docs-path <path> Local path to OpenAPI documentation file
179
+ --output-dir <dir> Output directory for generated files
180
+ --clean Clean output directory before generation
181
+ -h, --help display help for command
182
+ ```
183
+
184
+ ## Configuration examples
185
+
186
+ ### Generate by tags (multiple classes)
187
+
188
+ ```typescript
189
+ {
190
+ tags: {
191
+ include: ['api_tag_users', 'api_tag_products'],
192
+ },
193
+ groupBy: 'tag',
194
+ classMode: 'multiple',
195
+ }
196
+ ```
197
+
198
+ ### Generate single class
199
+
200
+ ```typescript
201
+ {
202
+ groupBy: 'all',
203
+ classMode: 'single',
204
+ }
205
+ ```
206
+
207
+ ### Exclude tags
208
+
209
+ ```typescript
210
+ {
211
+ tags: {
212
+ exclude: ['api_tag_internal', 'api_tag_admin'],
213
+ },
214
+ }
215
+ ```
216
+
217
+ ### Generate with axios configuration
218
+
219
+ ```typescript
220
+ {
221
+ options: {
222
+ generateAxiosConfig: true,
223
+ },
224
+ axios: {
225
+ baseURL: 'https://api.example.com',
226
+ timeout: 30000,
227
+ headers: {
228
+ 'Content-Type': 'application/json',
229
+ 'X-API-Key': 'your-api-key',
230
+ },
231
+ },
232
+ }
233
+ ```
234
+
235
+ ## License
236
+
237
+ MIT