@cerios/openapi-to-zod 0.2.0 → 0.3.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 +112 -114
- package/dist/cli.js +5131 -156
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5108 -153
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +20 -8
- package/dist/index.d.ts +20 -8
- package/dist/index.js +13 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -35,84 +35,77 @@ npm install @cerios/openapi-to-zod
|
|
|
35
35
|
|
|
36
36
|
## CLI Usage
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
> **Breaking Change in v2.0:** All CLI options have been removed. Use configuration files instead.
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
# Basic usage
|
|
42
|
-
openapi-to-zod -i openapi.yaml -o schemas.ts
|
|
43
|
-
|
|
44
|
-
# Strict mode (no additional properties)
|
|
45
|
-
openapi-to-zod -i openapi.yaml -o schemas.ts --mode strict
|
|
40
|
+
### Quick Start
|
|
46
41
|
|
|
47
|
-
|
|
48
|
-
openapi-to-zod -i openapi.yaml -o schemas.ts --mode loose
|
|
42
|
+
#### 1. Initialize Configuration
|
|
49
43
|
|
|
50
|
-
|
|
51
|
-
openapi-to-zod
|
|
44
|
+
```bash
|
|
45
|
+
npx @cerios/openapi-to-zod --init
|
|
46
|
+
```
|
|
52
47
|
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
This interactive command will:
|
|
49
|
+
- Prompt for your OpenAPI spec path
|
|
50
|
+
- Prompt for output file path
|
|
51
|
+
- Ask if you want to include commonly-used defaults
|
|
52
|
+
- Generate a config file (`openapi-to-zod.config.ts` or `.json`)
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
openapi-to-zod -i openapi.yaml -o request-schemas.ts -s request
|
|
54
|
+
#### 2. Generate Schemas
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
openapi-to-zod
|
|
56
|
+
```bash
|
|
57
|
+
npx @cerios/openapi-to-zod
|
|
58
|
+
```
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
openapi-to-zod -i openapi.yaml -o schemas.ts -e typescript
|
|
60
|
+
The tool will auto-discover your config file and generate schemas.
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
openapi-to-zod -i openapi.yaml -o schemas.ts -p api
|
|
67
|
-
# Result: apiUserSchema, apiProductSchema, etc.
|
|
62
|
+
### Configuration
|
|
68
63
|
|
|
69
|
-
|
|
70
|
-
openapi-to-zod -i openapi.yaml -o schemas.ts --suffix dto
|
|
71
|
-
# Result: userDtoSchema, productDtoSchema, etc.
|
|
64
|
+
#### TypeScript Config (Recommended)
|
|
72
65
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
**Minimal:**
|
|
67
|
+
```typescript
|
|
68
|
+
import { defineConfig } from '@cerios/openapi-to-zod';
|
|
76
69
|
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
export default defineConfig({
|
|
71
|
+
specs: [
|
|
72
|
+
{
|
|
73
|
+
input: 'openapi.yaml',
|
|
74
|
+
output: 'src/schemas.ts',
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
});
|
|
79
78
|
```
|
|
80
79
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
```bash
|
|
86
|
-
# Auto-discover config file (openapi-to-zod.config.ts or openapi-to-zod.config.json)
|
|
87
|
-
openapi-to-zod --config
|
|
88
|
-
|
|
89
|
-
# Explicit config path
|
|
90
|
-
openapi-to-zod --config path/to/config.json
|
|
91
|
-
|
|
92
|
-
# Override execution mode
|
|
93
|
-
openapi-to-zod --config --execution-mode sequential
|
|
80
|
+
**With Commonly-Used Defaults:**
|
|
81
|
+
```typescript
|
|
82
|
+
import { defineConfig } from '@cerios/openapi-to-zod';
|
|
94
83
|
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
export default defineConfig({
|
|
85
|
+
defaults: {
|
|
86
|
+
mode: 'strict', // Strictest validation
|
|
87
|
+
includeDescriptions: true, // Useful JSDoc comments
|
|
88
|
+
showStats: false, // Cleaner output
|
|
89
|
+
},
|
|
90
|
+
specs: [
|
|
91
|
+
{
|
|
92
|
+
input: 'openapi.yaml',
|
|
93
|
+
output: 'src/schemas.ts',
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
97
|
```
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
**TypeScript Config** (`openapi-to-zod.config.ts`) - Recommended for type safety:
|
|
99
|
+
**Multi-Spec with Custom Options:**
|
|
102
100
|
|
|
103
101
|
```typescript
|
|
104
102
|
import { defineConfig } from '@cerios/openapi-to-zod';
|
|
105
103
|
|
|
106
104
|
export default defineConfig({
|
|
107
|
-
// Global defaults applied to all specs
|
|
108
105
|
defaults: {
|
|
109
106
|
mode: 'strict',
|
|
110
107
|
includeDescriptions: true,
|
|
111
|
-
enumType: 'zod',
|
|
112
|
-
showStats: false,
|
|
113
108
|
},
|
|
114
|
-
|
|
115
|
-
// Array of specs to process
|
|
116
109
|
specs: [
|
|
117
110
|
{
|
|
118
111
|
name: 'api-v1',
|
|
@@ -126,21 +119,14 @@ export default defineConfig({
|
|
|
126
119
|
mode: 'normal', // Override default
|
|
127
120
|
prefix: 'v2',
|
|
128
121
|
},
|
|
129
|
-
{
|
|
130
|
-
name: 'admin-api',
|
|
131
|
-
input: 'specs/admin.yaml',
|
|
132
|
-
output: 'src/schemas/admin.ts',
|
|
133
|
-
prefix: 'admin',
|
|
134
|
-
suffix: 'dto',
|
|
135
|
-
},
|
|
136
122
|
],
|
|
137
|
-
|
|
138
|
-
// Execution mode: 'parallel' (default) or 'sequential'
|
|
139
|
-
executionMode: 'parallel',
|
|
123
|
+
executionMode: 'parallel', // Process specs in parallel (default)
|
|
140
124
|
});
|
|
141
125
|
```
|
|
142
126
|
|
|
143
|
-
|
|
127
|
+
#### JSON Config
|
|
128
|
+
|
|
129
|
+
**openapi-to-zod.config.json:**
|
|
144
130
|
|
|
145
131
|
```json
|
|
146
132
|
{
|
|
@@ -148,46 +134,74 @@ export default defineConfig({
|
|
|
148
134
|
"mode": "strict",
|
|
149
135
|
"includeDescriptions": true,
|
|
150
136
|
"enumType": "zod",
|
|
151
|
-
|
|
137
|
+
```json
|
|
138
|
+
{
|
|
139
|
+
"defaults": {
|
|
140
|
+
"mode": "strict",
|
|
141
|
+
"includeDescriptions": true
|
|
152
142
|
},
|
|
153
143
|
"specs": [
|
|
154
144
|
{
|
|
155
|
-
"
|
|
156
|
-
"
|
|
157
|
-
"output": "src/schemas/v1.ts"
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
"name": "api-v2",
|
|
161
|
-
"input": "specs/api-v2.yaml",
|
|
162
|
-
"output": "src/schemas/v2.ts",
|
|
163
|
-
"mode": "normal",
|
|
164
|
-
"prefix": "v2"
|
|
145
|
+
"input": "openapi.yaml",
|
|
146
|
+
"output": "src/schemas.ts"
|
|
165
147
|
}
|
|
166
|
-
]
|
|
167
|
-
"executionMode": "parallel"
|
|
148
|
+
]
|
|
168
149
|
}
|
|
169
150
|
```
|
|
170
151
|
|
|
171
|
-
|
|
152
|
+
### CLI Reference
|
|
172
153
|
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
154
|
+
```bash
|
|
155
|
+
openapi-to-zod [options]
|
|
156
|
+
|
|
157
|
+
Options:
|
|
158
|
+
-c, --config <path> Path to config file (optional if using auto-discovery)
|
|
159
|
+
--init Initialize a new config file
|
|
160
|
+
-V, --version Output version number
|
|
161
|
+
-h, --help Display help
|
|
162
|
+
|
|
163
|
+
Examples:
|
|
164
|
+
# Create config
|
|
165
|
+
$ openapi-to-zod --init
|
|
166
|
+
|
|
167
|
+
# Generate (auto-discover config)
|
|
168
|
+
$ openapi-to-zod
|
|
169
|
+
|
|
170
|
+
# Generate with custom config path
|
|
171
|
+
$ openapi-to-zod --config custom.config.ts
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Migration from v1.x
|
|
175
|
+
|
|
176
|
+
**Before (v1.x):**
|
|
177
|
+
```bash
|
|
178
|
+
openapi-to-zod -i openapi.yaml -o schemas.ts --mode strict --prefix Api
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**After (v2.0):**
|
|
182
|
+
```typescript
|
|
183
|
+
// openapi-to-zod.config.ts
|
|
184
|
+
import { defineConfig } from '@cerios/openapi-to-zod';
|
|
185
|
+
|
|
186
|
+
export default defineConfig({
|
|
187
|
+
specs: [
|
|
188
|
+
{
|
|
189
|
+
input: 'openapi.yaml',
|
|
190
|
+
output: 'schemas.ts',
|
|
191
|
+
mode: 'strict',
|
|
192
|
+
prefix: 'Api',
|
|
179
193
|
},
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
"input": "openapi.yaml",
|
|
183
|
-
"output": "src/schemas.ts"
|
|
184
|
-
}
|
|
185
|
-
]
|
|
186
|
-
}
|
|
187
|
-
}
|
|
194
|
+
],
|
|
195
|
+
});
|
|
188
196
|
```
|
|
189
197
|
|
|
190
|
-
|
|
198
|
+
Then run:
|
|
199
|
+
```bash
|
|
200
|
+
openapi-to-zod
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Configuration Options
|
|
204
|
+
|
|
191
205
|
|
|
192
206
|
| Option | Type | Description |
|
|
193
207
|
|--------|------|-------------|
|
|
@@ -195,7 +209,7 @@ export default defineConfig({
|
|
|
195
209
|
| `specs` | `array` | Array of spec configurations (required, minimum 1) |
|
|
196
210
|
| `executionMode` | `"parallel"` \| `"sequential"` | How to process specs (default: `"parallel"`) |
|
|
197
211
|
|
|
198
|
-
|
|
212
|
+
**Per-Spec Options:**
|
|
199
213
|
|
|
200
214
|
| Spec Option | Type | Description |
|
|
201
215
|
|-------------|------|-------------|
|
|
@@ -210,27 +224,11 @@ Each spec in the `specs` array supports all generator options:
|
|
|
210
224
|
| `prefix` | `string` | Prefix for schema names |
|
|
211
225
|
| `suffix` | `string` | Suffix for schema names |
|
|
212
226
|
| `showStats` | `boolean` | Include generation statistics |
|
|
227
|
+
| `nativeEnumType` | `"union"` \| `"enum"` | Native enum type when using TypeScript enums |
|
|
228
|
+
| `request` | `object` | Request-specific options (mode, typeMode, enumType, etc.) |
|
|
229
|
+
| `response` | `object` | Response-specific options (mode, enumType, etc.) |
|
|
213
230
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
Options are merged in the following order (highest precedence first):
|
|
217
|
-
|
|
218
|
-
1. **CLI arguments** - Override everything
|
|
219
|
-
2. **Per-spec config** - Override defaults
|
|
220
|
-
3. **Defaults** - From config file
|
|
221
|
-
4. **Built-in defaults** - Hardcoded fallbacks
|
|
222
|
-
|
|
223
|
-
Example:
|
|
224
|
-
```bash
|
|
225
|
-
# Config has defaults.mode = "strict"
|
|
226
|
-
# Spec has mode = "normal"
|
|
227
|
-
# CLI has --mode loose
|
|
228
|
-
|
|
229
|
-
# Result: "loose" (CLI wins)
|
|
230
|
-
openapi-to-zod --config myconfig.json --mode loose
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
#### Batch Execution
|
|
231
|
+
### Batch Execution
|
|
234
232
|
|
|
235
233
|
**Parallel Mode** (default):
|
|
236
234
|
- Processes all specs concurrently
|