@moccona/apicodegen 0.1.1 β†’ 0.1.2

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
@@ -5,24 +5,71 @@ A powerful OpenAPI code generator that automatically generates TypeScript API cl
5
5
  [![npm version](https://badge.fury.io/js/@moccona%2Fapicodegen.svg)](https://www.npmjs.com/package/@moccona/apicodegen)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
7
  [![Node.js](https://img.shields.io/badge/node-%3E%3D24-blue.svg)](https://nodejs.org)
8
+ [![Test](https://img.shields.io/badge/tests-149%20passed-green)](https://github.com/freemode1614/api-codegen)
9
+ [![Build](https://img.shields.io/badge/build-tsdown-blue)](https://github.com/freemode1614/api-codegen)
10
+
11
+ ---
12
+
13
+ ## Table of Contents
14
+
15
+ - [Features](#-features)
16
+ - [Quick Start](#-quick-start)
17
+ - [Installation](#-installation)
18
+ - [CLI Usage](#-cli-usage)
19
+ - [Vite Plugin](#-vite-plugin)
20
+ - [Generated Code](#-generated-code)
21
+ - [Supported Features](#-supported-features)
22
+ - [Examples](#-examples)
23
+ - [Troubleshooting](#-troubleshooting)
24
+ - [Development](#-development)
25
+ - [Roadmap](#-roadmap)
26
+
27
+ ---
8
28
 
9
29
  ## ✨ Features
10
30
 
11
- - πŸš€ **Multi-version Support** - Full support for OpenAPI 2.0, 3.0, and 3.1
12
- - πŸ“ **TypeScript First** - Generates complete type definitions and type-safe API functions
13
- - πŸ”Œ **Multiple Adaptors** - Built-in `fetch` and `axios` HTTP client support
14
- - πŸ› οΈ **CLI Tool** - Simple and user-friendly command-line interface with retro ASCII banner
15
- - ⚑ **Vite Plugin** - Seamless integration into Vite build workflow
16
- - πŸ“¦ **File Upload** - Native support for multipart/form-data file uploads
17
- - 🎯 **Complete Types** - Supports enums, union types, intersection types, complex nested objects, and more
31
+ | Feature | Description |
32
+ |---------|-------------|
33
+ | **Multi-version Support** | Full support for OpenAPI 2.0, 3.0, and 3.1 |
34
+ | **TypeScript First** | Generates complete type definitions and type-safe API functions |
35
+ | **Multiple Adaptors** | Built-in `fetch` and `axios` HTTP client support |
36
+ | **CLI Tool** | Simple command-line interface with retro ASCII banner |
37
+ | **Vite Plugin** | Seamless integration into Vite build workflow |
38
+ | **File Upload** | Native support for multipart/form-data file uploads |
39
+ | **Complete Types** | Enums, union types, intersection types, complex nested objects |
40
+
41
+ ### Key Highlights
42
+
43
+ - **Zero Runtime Dependencies** - Lightweight generated code with no external runtime
44
+ - **Type-safe** - Full TypeScript support with strict type inference
45
+ - **Circular Reference Handling** - Properly handles `$ref` loops in OpenAPI schemas
46
+ - **JSDoc Generation** - Automatic documentation from OpenAPI descriptions
47
+ - **Watch Mode** - Auto-regenerate when OpenAPI spec changes
48
+
49
+ ---
50
+
51
+ ## πŸš€ Quick Start
52
+
53
+ ```bash
54
+ # Install globally
55
+ npm install -g @moccona/apicodegen
56
+
57
+ # Generate from URL
58
+ apicodegen https://petstore3.swagger.io/api/v3/openapi.json -o ./src/api/petstore.ts
59
+
60
+ # Or use npx (no installation)
61
+ npx @moccona/apicodegen ./openapi.json -o ./src/api.ts
62
+ ```
63
+
64
+ ---
18
65
 
19
66
  ## πŸ“¦ Installation
20
67
 
21
68
  ```bash
22
- # Global installation (recommended for CLI usage)
69
+ # Global installation (recommended for CLI)
23
70
  npm install -g @moccona/apicodegen
24
71
 
25
- # Local installation
72
+ # Local installation for project use
26
73
  npm install -D @moccona/apicodegen
27
74
 
28
75
  # Using pnpm
@@ -31,71 +78,49 @@ pnpm add -D @moccona/apicodegen
31
78
 
32
79
  ### Peer Dependencies
33
80
 
34
- This package includes optional peer dependencies:
35
-
36
- - `typescript` (v5) - Required for type checking generated code
37
- - `prettier` (v3) - Used for formatting output
38
- - `vite` (v7) - Required only if using the Vite plugin
81
+ | Package | Version | Required For |
82
+ |---------|---------|--------------|
83
+ | `typescript` | v5 | Type checking generated code |
84
+ | `prettier` | v3 | Formatting output |
85
+ | `vite` | v7 / v8 | Vite plugin only (optional) |
39
86
 
40
- ## πŸš€ Quick Start
87
+ ---
41
88
 
42
- ### CLI Usage
89
+ ## πŸ“‹ CLI Usage
43
90
 
44
- ```bash
45
- # Basic usage
46
- apicodegen <OpenAPIζ–‡ζ‘£URL> -o ./src/api.ts
47
-
48
- # Full example
49
- apicodegen https://api.example.com/openapi.json \
50
- -o ./src/api.ts \
51
- -a fetch \
52
- -b https://api.example.com \
53
- -v
54
- ```
55
-
56
- ### Options
91
+ ### Basic Options
57
92
 
58
93
  | Option | Short | Description | Default |
59
94
  |--------|-------|-------------|---------|
60
95
  | `--output` | `-o` | Output file path | `./output.ts` |
61
- | `--spec` | `-s` | OpenAPI spec file path or URL | - |
62
- | `--adaptor` | `-a` | HTTP client adaptor (`fetch` or `axios`) | `fetch` |
96
+ | `--spec` | `-s` | OpenAPI spec file path or URL | Required |
97
+ | `--adaptor` | `-a` | HTTP client (`fetch` or `axios`) | `fetch` |
63
98
  | `--baseURL` | `-b` | API base URL | - |
64
99
  | `--config` | `-c` | Path to config file | - |
65
- | `--watch` | `-w` | Watch for file changes | - |
100
+ | `--watch` | `-w` | Watch for file changes | `false` |
66
101
  | `--verbose` | `-v` | Enable verbose logging | `false` |
67
102
 
68
- ## πŸ“– Usage Examples
69
-
70
- ### 1. Basic Code Generation
103
+ ### Examples
71
104
 
72
105
  ```bash
73
- # From remote OpenAPI document
74
- apicodegen https://petstore3.swagger.io/api/v3/openapi.json -o ./api/petstore.ts
106
+ # From remote URL
107
+ apicodegen https://api.example.com/openapi.json -o ./src/api.ts
75
108
 
76
109
  # From local file
77
110
  apicodegen ./docs/openapi.json -o ./src/generated/api.ts
78
- ```
79
111
 
80
- ### 2. Using Axios Adaptor
112
+ # With axios and custom baseURL
113
+ apicodegen ./openapi.json -a axios -b https://api.example.com -o ./src/api.ts
81
114
 
82
- ```bash
83
- apicodegen https://api.example.com/openapi.json \
84
- -o ./src/api.ts \
85
- -a axios \
86
- -b https://api.example.com
115
+ # Watch mode for development
116
+ apicodegen ./openapi.yaml -w -o ./src/api.ts -v
87
117
  ```
88
118
 
89
- ### 3. Watch Mode
90
-
91
- ```bash
92
- # Watch for file changes and regenerate
93
- apicodegen ./openapi.yaml -w -o ./src/api.ts
94
- ```
119
+ ---
95
120
 
96
121
  ## πŸ”Œ Vite Plugin
97
122
 
98
- Automatically integrate code generation into your Vite project:
123
+ Automatically generate API clients during your Vite build:
99
124
 
100
125
  ```typescript
101
126
  // vite.config.ts
@@ -117,22 +142,23 @@ export default defineConfig({
117
142
  });
118
143
  ```
119
144
 
120
- ### Vite Plugin Options
145
+ ### Plugin Options
121
146
 
122
- | Option | Type | Description |
123
- |--------|------|-------------|
124
- | `name` | `string` | Human-readable API name (required) |
125
- | `spec` | `string` | OpenAPI spec file path or URL |
126
- | `output` | `string` | Output file path (required) |
127
- | `adaptor` | `'fetch' \| 'axios'` | HTTP client adaptor |
128
- | `baseURL` | `string` | API base URL |
129
- | `importClientSource` | `string` | Custom client import source (for advanced axios/fetch configuration) |
130
- | `verbose` | `boolean` | Enable verbose logging |
131
- | `typeCheck` | `boolean` | Run type check after generation (default: true) |
147
+ | Option | Type | Required | Description |
148
+ |--------|------|----------|-------------|
149
+ | `name` | `string` | Yes | Human-readable API name |
150
+ | `spec` | `string` | Yes* | OpenAPI spec file path or URL |
151
+ | `docURL` | `string` | Yes* | OpenAPI document URL (alias for `spec`) |
152
+ | `output` | `string` | Yes | Output file path |
153
+ | `adaptor` | `'fetch' \| 'axios'` | No | HTTP client (default: `fetch`) |
154
+ | `baseURL` | `string` | No | API base URL |
155
+ | `importClientSource` | `string` | No | Custom client import source |
156
+ | `verbose` | `boolean` | No | Enable verbose logging |
157
+ | `typeCheck` | `boolean` | No | Run type check after generation |
132
158
 
133
- ### Using Custom Axios Instance
159
+ *\*Either `spec` or `docURL` is required
134
160
 
135
- If you need to use a custom-configured axios instance (e.g., with interceptors, custom base URL, or authentication), you can provide your own axios instance via `importClientSource`:
161
+ ### Custom Axios Instance
136
162
 
137
163
  ```typescript
138
164
  // src/lib/api-client.ts
@@ -151,7 +177,6 @@ apiClient.interceptors.request.use((config) => {
151
177
 
152
178
  ```typescript
153
179
  // vite.config.ts
154
- import { defineConfig } from 'vite';
155
180
  import { apiCodeGenPlugin } from '@moccona/apicodegen/vite';
156
181
  import { apiClient } from './src/lib/api-client';
157
182
 
@@ -170,27 +195,28 @@ export default defineConfig({
170
195
  });
171
196
  ```
172
197
 
173
- The generated code will use your custom instance instead of the default `axios`:
198
+ Generated code will use your custom instance:
174
199
 
175
200
  ```typescript
176
- // Generated code
177
201
  import { apiClient as axios } from '@/lib/api-client';
178
202
 
179
- // Uses apiClient under the hood
180
203
  export async function getPetById({ petId }: { petId: number }) {
181
204
  return apiClient(`/pets/${petId}`, { method: 'GET' });
182
205
  }
183
206
  ```
184
207
 
185
- ## πŸ“ Generated Code Example
208
+ ---
209
+
210
+ ## πŸ“ Generated Code
186
211
 
187
- Given the following OpenAPI definition:
212
+ Given this OpenAPI spec:
188
213
 
189
214
  ```yaml
190
215
  paths:
191
216
  /pets/{petId}:
192
217
  get:
193
218
  operationId: getPetById
219
+ summary: Get a pet by ID
194
220
  parameters:
195
221
  - name: petId
196
222
  in: path
@@ -221,15 +247,6 @@ components:
221
247
  The generated TypeScript code:
222
248
 
223
249
  ```typescript
224
- /**
225
- * Pet object
226
- */
227
- export type Pet = {
228
- id?: number;
229
- name?: string;
230
- status?: 'available' | 'pending' | 'sold';
231
- };
232
-
233
250
  /**
234
251
  * Get a pet by ID
235
252
  */
@@ -238,71 +255,121 @@ export async function getPetById({ petId }: { petId: number }) {
238
255
  method: 'GET',
239
256
  }).then(async (response) => (await response.json()) as Pet);
240
257
  }
258
+
259
+ /**
260
+ * Pet object
261
+ */
262
+ export type Pet = {
263
+ id?: number;
264
+ name?: string;
265
+ status?: 'available' | 'pending' | 'sold';
266
+ };
241
267
  ```
242
268
 
243
- ## 🎯 Supported OpenAPI Features
269
+ ---
270
+
271
+ ## 🎯 Supported Features
244
272
 
245
273
  ### Schema Types
246
- - βœ… Basic types: `string`, `number`, `integer`, `boolean`
247
- - βœ… Complex objects: `object` and property definitions
248
- - βœ… Array types: `array` and nested arrays
249
- - βœ… Enum types: Automatically generate TypeScript enums
250
- - βœ… Union types: `oneOf`, `anyOf`
251
- - βœ… Intersection types: `allOf`
252
- - βœ… Reference types: `$ref` with circular reference handling
253
- - βœ… File types: `binary`, `blob`, `file` formats
254
274
 
255
- ### Parameter Locations
256
- - βœ… Path parameters: `/users/{id}`
257
- - βœ… Query parameters: `?page=1&limit=10`
258
- - βœ… Header parameters: Custom request headers
259
- - βœ… Cookie parameters: Request cookies
260
- - βœ… Body parameters: JSON and FormData
275
+ | Type | Status | Notes |
276
+ |------|--------|-------|
277
+ | `string` | βœ… | Full support with formats |
278
+ | `number` / `integer` | βœ… | int8, uint8, int16, etc. |
279
+ | `boolean` | βœ… | Full support |
280
+ | `object` | βœ… | Complex nested objects |
281
+ | `array` | βœ… | Nested and multi-dimensional |
282
+ | `enum` | βœ… | Auto-generated TypeScript enums |
283
+ | `oneOf` / `anyOf` | βœ… | Union types with deduplication |
284
+ | `allOf` | βœ… | Intersection types |
285
+ | `$ref` | βœ… | Circular reference handling |
286
+ | `binary` / `blob` / `file` | βœ… | File upload types |
261
287
 
262
- ### Response Formats
263
- - βœ… `application/json` β€” parsed and typed
264
- - βœ… SSE `text/event-stream` β€” raw response returned for application-layer handling
265
- - βœ… Other text/binary types β€” returned unparsed
288
+ ### Parameter Locations
266
289
 
267
- > For SSE endpoints:
268
- > - **fetch** adapter: returns `Promise<Response>`. Use `EventSource`, `response.body.getReader()`, or another tool of your choice to parse events.
269
- > - **axios** adapter: configures axios with `adapter: 'fetch'` + `responseType: 'stream'`, so `response.data` is a `ReadableStream` you can iterate directly.
290
+ | Location | Status | Example |
291
+ |----------|--------|---------|
292
+ | `path` | βœ… | `/users/{id}` |
293
+ | `query` | βœ… | `?page=1&limit=10` |
294
+ | `header` | βœ… | `X-API-Key: xxx` |
295
+ | `cookie` | βœ… | `session=xxx` |
296
+ | `body` | βœ… | JSON and FormData |
270
297
 
271
298
  ### Request Body Formats
272
- - βœ… `application/json` - JSON data
273
- - βœ… `multipart/form-data` - File uploads
274
- - βœ… `application/x-www-form-urlencoded` - Form data
275
- - βœ… `text/plain`, `image/*` - Binary data
299
+
300
+ | Content-Type | Status |
301
+ |--------------|--------|
302
+ | `application/json` | βœ… |
303
+ | `multipart/form-data` | βœ… |
304
+ | `application/x-www-form-urlencoded` | βœ… |
305
+ | `text/plain` | βœ… |
306
+ | `image/*` | βœ… |
307
+
308
+ ### OpenAPI Versions
309
+
310
+ | Version | Status |
311
+ |---------|--------|
312
+ | OpenAPI 2.0 (Swagger) | βœ… |
313
+ | OpenAPI 3.0 | βœ… |
314
+ | OpenAPI 3.1 | βœ… |
315
+
316
+ ---
317
+
318
+ ## πŸ“– Examples
319
+
320
+ ### Complete API with Authentication
321
+
322
+ ```bash
323
+ apicodegen https://api.example.com/openapi.json \
324
+ -o ./src/api.ts \
325
+ -a fetch \
326
+ -b https://api.example.com \
327
+ -v
328
+ ```
329
+
330
+ ### Using Custom Axios with Interceptors
331
+
332
+ See [Custom Axios Instance](#custom-axios-instance) section above.
333
+
334
+ ### Watch Mode for Development
335
+
336
+ ```bash
337
+ apicodegen ./openapi.json \
338
+ --watch \
339
+ --output ./src/generated/api.ts \
340
+ --verbose
341
+ ```
342
+
343
+ ---
276
344
 
277
345
  ## πŸ”§ Troubleshooting
278
346
 
279
347
  ### Common Issues
280
348
 
281
349
  **"Command not found" after installation**
282
- If using the CLI globally but getting command not found, try:
283
350
  ```bash
284
351
  # Reinstall globally
285
352
  npm install -g @moccona/apicodegen
286
353
 
287
354
  # Or use npx
288
- npx @moccona/apicodegen <OpenAPIζ–‡ζ‘£URL> -o ./src/api.ts
355
+ npx @moccona/apicodegen <url> -o ./src/api.ts
289
356
  ```
290
357
 
291
358
  **Network errors when fetching OpenAPI documents**
292
359
  - Verify the URL is publicly accessible
293
360
  - Try downloading the document locally first
294
361
  - Check firewall/proxy settings
295
- - Use `-v` flag for verbose logging to debug
362
+ - Use `-v` flag for verbose logging
296
363
 
297
364
  **TypeScript errors in generated code**
298
365
  - Ensure `typescript` is installed: `npm install -D typescript`
299
366
  - Run `tsc --noEmit` to see specific errors
300
- - Check that your `tsconfig.json` is properly configured
367
+ - Verify your `tsconfig.json` is properly configured
301
368
 
302
369
  **Vite plugin not generating files**
303
370
  - Ensure Node.js 24+ is installed
304
371
  - Check that all required options (`name`, `output`) are provided
305
- - Set `verbose: true` in plugin options to see generation logs
372
+ - Set `verbose: true` in plugin options
306
373
 
307
374
  **Watch mode not triggering regeneration**
308
375
  - Watch mode monitors the spec file, not your output file
@@ -311,8 +378,11 @@ npx @moccona/apicodegen <OpenAPIζ–‡ζ‘£URL> -o ./src/api.ts
311
378
 
312
379
  ### Getting Help
313
380
 
314
- - Report issues at [GitHub Issues](https://github.com/freemode1614/api-codegen/issues)
315
- - Check [CHANGELOG](CHANGELOG.md) for recent updates
381
+ - πŸ“„ [CHANGELOG](CHANGELOG.md) - Recent updates
382
+ - πŸ› [GitHub Issues](https://github.com/freemode1614/api-codegen/issues) - Report bugs
383
+ - πŸ“– [docs/roadmap.md](docs/roadmap.md) - Future plans
384
+
385
+ ---
316
386
 
317
387
  ## πŸ”§ Development
318
388
 
@@ -320,13 +390,13 @@ npx @moccona/apicodegen <OpenAPIζ–‡ζ‘£URL> -o ./src/api.ts
320
390
  # Install dependencies
321
391
  pnpm install
322
392
 
323
- # Development mode
393
+ # Development mode (watch)
324
394
  pnpm dev
325
395
 
326
- # Build
396
+ # Build for production
327
397
  pnpm build
328
398
 
329
- # Run tests
399
+ # Run all tests
330
400
  pnpm test
331
401
 
332
402
  # Type check
@@ -335,10 +405,42 @@ pnpm typecheck
335
405
  # Lint
336
406
  pnpm lint
337
407
 
338
- # Format
408
+ # Format code
339
409
  pnpm format
340
410
  ```
341
411
 
412
+ ### Project Structure
413
+
414
+ ```
415
+ src/
416
+ β”œβ”€β”€ cli.ts # CLI entry point
417
+ β”œβ”€β”€ cli/ # CLI modules
418
+ β”œβ”€β”€ core/ # Core code generation
419
+ β”‚ β”œβ”€β”€ generator/ # TypeScript AST generation
420
+ β”‚ β”œβ”€β”€ client/ # fetch/axios adaptors
421
+ β”‚ β”œβ”€β”€ base/ # Base utilities
422
+ β”‚ └── constants/ # Constants
423
+ β”œβ”€β”€ openapi/ # OpenAPI spec parsing
424
+ β”‚ β”œβ”€β”€ V2.ts # OpenAPI 2.0
425
+ β”‚ β”œβ”€β”€ V3.ts # OpenAPI 3.0
426
+ β”‚ └── V3_1.ts # OpenAPI 3.1
427
+ β”œβ”€β”€ vite-plugin/ # Vite plugin
428
+ └── types/ # TypeScript types
429
+ ```
430
+
431
+ ---
432
+
433
+ ## πŸ—ΊοΈ Roadmap
434
+
435
+ See [docs/roadmap.md](docs/roadmap.md) for detailed future plans:
436
+
437
+ - **v0.1.0** - TypeScript strict mode, config files, enhanced errors
438
+ - **v0.2.0** - Plugin system, template engine, incremental generation
439
+ - **v0.3.0** - VSCode LSP, GitHub Action, mock server
440
+ - **v0.4.0+** - Web UI, GraphQL support
441
+
442
+ ---
443
+
342
444
  ## πŸ“„ License
343
445
 
344
446
  [MIT](LICENSE) Β© freemode
@@ -347,6 +449,17 @@ pnpm format
347
449
 
348
450
  Issues and Pull Requests are welcome!
349
451
 
452
+ - πŸ“˜ [Contributing Guide](./CONTRIBUTING.md) β€” dev setup, code style, PR process
453
+ - πŸ“œ [Code of Conduct](./CODE_OF_CONDUCT.md) β€” community norms & engineering standards
454
+ - πŸ”’ [Security Policy](./SECURITY.md) β€” how to report vulnerabilities privately
455
+ - πŸ’¬ [GitHub Discussions](https://github.com/freemode1614/api-codegen/discussions) β€” questions & ideas
456
+
457
+ When opening an issue, you'll be guided through **Bug Report**, **Feature Request**,
458
+ **Documentation**, or **Question** templates so we can help you faster.
459
+
460
+ By participating in this project you agree to abide by the [Code of Conduct](./CODE_OF_CONDUCT.md).
461
+
350
462
  ---
351
463
 
352
- Questions or suggestions? Visit [GitHub Issues](https://github.com/freemode1614/api-codegen/issues).
464
+ *Questions or suggestions? [Start a discussion](https://github.com/freemode1614/api-codegen/discussions)
465
+ or [open an issue](https://github.com/freemode1614/api-codegen/issues).*