nuxt-openapi-hyperfetch 0.2.7-alpha.1 → 0.3.0-beta
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/.editorconfig +26 -26
- package/.prettierignore +17 -17
- package/CONTRIBUTING.md +291 -291
- package/INSTRUCTIONS.md +327 -327
- package/LICENSE +202 -202
- package/README.md +309 -231
- package/dist/cli/config.d.ts +9 -2
- package/dist/cli/config.js +1 -1
- package/dist/cli/logo.js +5 -5
- package/dist/cli/messages.d.ts +1 -0
- package/dist/cli/messages.js +2 -0
- package/dist/cli/prompts.d.ts +5 -0
- package/dist/cli/prompts.js +12 -0
- package/dist/cli/types.d.ts +1 -1
- package/dist/generators/components/connector-generator/templates.js +68 -19
- package/dist/generators/shared/runtime/useFormConnector.js +8 -1
- package/dist/generators/shared/runtime/useListConnector.js +13 -6
- package/dist/generators/use-async-data/generator.js +4 -0
- package/dist/generators/use-async-data/runtime/useApiAsyncData.js +4 -4
- package/dist/generators/use-async-data/runtime/useApiAsyncDataRaw.js +4 -4
- package/dist/generators/use-async-data/templates.js +17 -17
- package/dist/generators/use-fetch/generator.js +4 -0
- package/dist/generators/use-fetch/templates.js +14 -14
- package/dist/index.js +40 -27
- package/dist/module/index.js +19 -0
- package/dist/module/types.d.ts +7 -0
- package/docs/API-REFERENCE.md +886 -886
- package/docs/generated-components.md +615 -615
- package/docs/headless-composables-ui.md +569 -569
- package/eslint.config.js +85 -85
- package/package.json +1 -1
- package/src/cli/config.ts +147 -140
- package/src/cli/logger.ts +124 -124
- package/src/cli/logo.ts +25 -25
- package/src/cli/messages.ts +4 -0
- package/src/cli/prompts.ts +14 -1
- package/src/cli/types.ts +50 -50
- package/src/generators/components/connector-generator/generator.ts +138 -138
- package/src/generators/components/connector-generator/templates.ts +307 -254
- package/src/generators/components/connector-generator/types.ts +34 -34
- package/src/generators/components/schema-analyzer/index.ts +44 -44
- package/src/generators/components/schema-analyzer/intent-detector.ts +187 -187
- package/src/generators/components/schema-analyzer/openapi-reader.ts +96 -96
- package/src/generators/components/schema-analyzer/resource-grouper.ts +166 -166
- package/src/generators/components/schema-analyzer/schema-field-mapper.ts +268 -268
- package/src/generators/components/schema-analyzer/types.ts +177 -177
- package/src/generators/nuxt-server/generator.ts +272 -272
- package/src/generators/shared/runtime/apiHelpers.ts +535 -535
- package/src/generators/shared/runtime/pagination.ts +323 -323
- package/src/generators/shared/runtime/useDeleteConnector.ts +109 -109
- package/src/generators/shared/runtime/useDetailConnector.ts +64 -64
- package/src/generators/shared/runtime/useFormConnector.ts +147 -139
- package/src/generators/shared/runtime/useListConnector.ts +158 -148
- package/src/generators/shared/runtime/zod-error-merger.ts +119 -119
- package/src/generators/shared/templates/api-callbacks-plugin.ts +399 -399
- package/src/generators/shared/templates/api-pagination-plugin.ts +158 -158
- package/src/generators/use-async-data/generator.ts +213 -205
- package/src/generators/use-async-data/runtime/useApiAsyncData.ts +329 -329
- package/src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts +324 -324
- package/src/generators/use-async-data/templates.ts +257 -257
- package/src/generators/use-fetch/generator.ts +178 -170
- package/src/generators/use-fetch/runtime/useApiRequest.ts +354 -354
- package/src/generators/use-fetch/templates.ts +214 -214
- package/src/index.ts +306 -303
- package/src/module/index.ts +158 -133
- package/src/module/types.ts +39 -31
- package/dist/generators/tanstack-query/generator.d.ts +0 -5
- package/dist/generators/tanstack-query/generator.js +0 -11
|
@@ -1,268 +1,268 @@
|
|
|
1
|
-
import { pascalCase } from 'change-case';
|
|
2
|
-
import type {
|
|
3
|
-
ColumnDef,
|
|
4
|
-
ColumnType,
|
|
5
|
-
FieldType,
|
|
6
|
-
FormFieldDef,
|
|
7
|
-
OpenApiPropertySchema,
|
|
8
|
-
OpenApiSchema,
|
|
9
|
-
} from './types.js';
|
|
10
|
-
|
|
11
|
-
// ─── Column mapping ───────────────────────────────────────────────────────────
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Derive table ColumnDef[] from a response schema (list or detail).
|
|
15
|
-
* When the response schema is an array, we read the items schema.
|
|
16
|
-
*/
|
|
17
|
-
export function mapColumnsFromSchema(schema: OpenApiSchema): ColumnDef[] {
|
|
18
|
-
// If the response is an array, inspect the items object schema
|
|
19
|
-
const objectSchema = schema.type === 'array' && schema.items ? schema.items : schema;
|
|
20
|
-
|
|
21
|
-
if (!objectSchema.properties) {
|
|
22
|
-
return [];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return Object.entries(objectSchema.properties).map(([key, prop]) => ({
|
|
26
|
-
key,
|
|
27
|
-
label: pascalCase(key)
|
|
28
|
-
.replace(/([A-Z])/g, ' $1')
|
|
29
|
-
.trim(),
|
|
30
|
-
type: columnTypeFromProp(prop),
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function columnTypeFromProp(prop: OpenApiPropertySchema): ColumnType {
|
|
35
|
-
if (prop.enum) {
|
|
36
|
-
return 'badge';
|
|
37
|
-
}
|
|
38
|
-
if (prop.type === 'boolean') {
|
|
39
|
-
return 'boolean';
|
|
40
|
-
}
|
|
41
|
-
if (prop.type === 'integer' || prop.type === 'number') {
|
|
42
|
-
return 'number';
|
|
43
|
-
}
|
|
44
|
-
if (prop.format === 'date' || prop.format === 'date-time') {
|
|
45
|
-
return 'date';
|
|
46
|
-
}
|
|
47
|
-
return 'text';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ─── Form field mapping ───────────────────────────────────────────────────────
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Derive FormFieldDef[] from a request body schema.
|
|
54
|
-
* readOnly fields are included as hidden: true (developer can override).
|
|
55
|
-
*/
|
|
56
|
-
export function mapFieldsFromSchema(schema: OpenApiSchema): FormFieldDef[] {
|
|
57
|
-
if (!schema.properties) {
|
|
58
|
-
return [];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const required = new Set(schema.required ?? []);
|
|
62
|
-
|
|
63
|
-
return Object.entries(schema.properties).map(([key, prop]) => {
|
|
64
|
-
const isRequired = required.has(key);
|
|
65
|
-
const fieldType = fieldTypeFromProp(prop);
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
key,
|
|
69
|
-
label: labelFromKey(key),
|
|
70
|
-
type: fieldType,
|
|
71
|
-
required: isRequired,
|
|
72
|
-
hidden: prop.readOnly === true,
|
|
73
|
-
options: optionsFromProp(prop),
|
|
74
|
-
placeholder: undefined,
|
|
75
|
-
zodExpression: zodExpressionFromProp(prop, isRequired),
|
|
76
|
-
};
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function labelFromKey(key: string): string {
|
|
81
|
-
// Split camelCase/PascalCase into words and capitalise the first letter.
|
|
82
|
-
// 'photoUrls' → 'Photo Urls', 'firstName' → 'First Name'
|
|
83
|
-
return key
|
|
84
|
-
.replace(/([A-Z])/g, ' $1')
|
|
85
|
-
.replace(/^./, (c) => c.toUpperCase())
|
|
86
|
-
.trim();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function fieldTypeFromProp(prop: OpenApiPropertySchema): FieldType {
|
|
90
|
-
if (prop.readOnly) {
|
|
91
|
-
return 'input'; // will be hidden anyway
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (prop.enum) {
|
|
95
|
-
return 'select';
|
|
96
|
-
}
|
|
97
|
-
if (prop.type === 'boolean') {
|
|
98
|
-
return 'checkbox';
|
|
99
|
-
}
|
|
100
|
-
if (prop.type === 'integer' || prop.type === 'number') {
|
|
101
|
-
return 'number';
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (prop.type === 'string') {
|
|
105
|
-
if (prop.format === 'date' || prop.format === 'date-time') {
|
|
106
|
-
return 'datepicker';
|
|
107
|
-
}
|
|
108
|
-
if (typeof prop.maxLength === 'number' && prop.maxLength > 200) {
|
|
109
|
-
return 'textarea';
|
|
110
|
-
}
|
|
111
|
-
return 'input';
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// array, object → input as fallback
|
|
115
|
-
return 'input';
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function optionsFromProp(prop: OpenApiPropertySchema): FormFieldDef['options'] {
|
|
119
|
-
if (!prop.enum) {
|
|
120
|
-
return undefined;
|
|
121
|
-
}
|
|
122
|
-
return prop.enum.map((v) => ({ label: String(v), value: v }));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// ─── Zod expression generation ────────────────────────────────────────────────
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Generate a Zod expression string for a single OpenAPI property.
|
|
129
|
-
*
|
|
130
|
-
* Returns a SOURCE CODE STRING (e.g. 'z.string().min(3)') that will be
|
|
131
|
-
* embedded inside the generated connector file, not evaluated here.
|
|
132
|
-
*
|
|
133
|
-
* Validation constraints (minLength, maximum, enum…) are read directly
|
|
134
|
-
* from the OpenAPI property schema and chained onto the Zod expression.
|
|
135
|
-
*/
|
|
136
|
-
export function zodExpressionFromProp(prop: OpenApiPropertySchema, isRequired: boolean): string {
|
|
137
|
-
let expr = baseZodExpr(prop);
|
|
138
|
-
|
|
139
|
-
if (!isRequired) {
|
|
140
|
-
expr += '.optional()';
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return expr;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function baseZodExpr(prop: OpenApiPropertySchema): string {
|
|
147
|
-
// Enum
|
|
148
|
-
if (prop.enum && prop.enum.length > 0) {
|
|
149
|
-
const values = prop.enum.map((v) => JSON.stringify(v)).join(', ');
|
|
150
|
-
return `z.enum([${values}])`;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
switch (prop.type) {
|
|
154
|
-
case 'string':
|
|
155
|
-
return stringZodExpr(prop);
|
|
156
|
-
|
|
157
|
-
case 'integer':
|
|
158
|
-
return integerZodExpr(prop);
|
|
159
|
-
|
|
160
|
-
case 'number':
|
|
161
|
-
return numberZodExpr(prop);
|
|
162
|
-
|
|
163
|
-
case 'boolean':
|
|
164
|
-
return 'z.boolean()';
|
|
165
|
-
|
|
166
|
-
case 'array':
|
|
167
|
-
return arrayZodExpr(prop);
|
|
168
|
-
|
|
169
|
-
case 'object':
|
|
170
|
-
return 'z.record(z.unknown())';
|
|
171
|
-
|
|
172
|
-
default:
|
|
173
|
-
// $ref already resolved, unknown type → permissive
|
|
174
|
-
return 'z.unknown()';
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function stringZodExpr(prop: OpenApiPropertySchema): string {
|
|
179
|
-
const expr = 'z.string()';
|
|
180
|
-
|
|
181
|
-
if (prop.format === 'email') {
|
|
182
|
-
return `${expr}.email()`;
|
|
183
|
-
}
|
|
184
|
-
if (prop.format === 'uri' || prop.format === 'url') {
|
|
185
|
-
return `${expr}.url()`;
|
|
186
|
-
}
|
|
187
|
-
if (prop.format === 'uuid') {
|
|
188
|
-
return `${expr}.uuid()`;
|
|
189
|
-
}
|
|
190
|
-
if (prop.format === 'date' || prop.format === 'date-time') {
|
|
191
|
-
return `${expr}.datetime()`;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
let chained = expr;
|
|
195
|
-
if (typeof prop.minLength === 'number') {
|
|
196
|
-
chained += `.min(${prop.minLength})`;
|
|
197
|
-
}
|
|
198
|
-
if (typeof prop.maxLength === 'number') {
|
|
199
|
-
chained += `.max(${prop.maxLength})`;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return chained;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function integerZodExpr(prop: OpenApiPropertySchema): string {
|
|
206
|
-
let expr = 'z.number().int()';
|
|
207
|
-
if (typeof prop.minimum === 'number') {
|
|
208
|
-
expr += `.min(${prop.minimum})`;
|
|
209
|
-
}
|
|
210
|
-
if (typeof prop.maximum === 'number') {
|
|
211
|
-
expr += `.max(${prop.maximum})`;
|
|
212
|
-
}
|
|
213
|
-
return expr;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function numberZodExpr(prop: OpenApiPropertySchema): string {
|
|
217
|
-
let expr = 'z.number()';
|
|
218
|
-
if (typeof prop.minimum === 'number') {
|
|
219
|
-
expr += `.min(${prop.minimum})`;
|
|
220
|
-
}
|
|
221
|
-
if (typeof prop.maximum === 'number') {
|
|
222
|
-
expr += `.max(${prop.maximum})`;
|
|
223
|
-
}
|
|
224
|
-
return expr;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function arrayZodExpr(prop: OpenApiPropertySchema): string {
|
|
228
|
-
const itemExpr = prop.items ? baseZodExpr(prop.items) : 'z.unknown()';
|
|
229
|
-
let expr = `z.array(${itemExpr})`;
|
|
230
|
-
if (typeof prop.minItems === 'number') {
|
|
231
|
-
expr += `.min(${prop.minItems})`;
|
|
232
|
-
}
|
|
233
|
-
if (typeof prop.maxItems === 'number') {
|
|
234
|
-
expr += `.max(${prop.maxItems})`;
|
|
235
|
-
}
|
|
236
|
-
return expr;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// ─── Full Zod object schema string ───────────────────────────────────────────
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Build a complete z.object({...}) string from a request body schema.
|
|
243
|
-
*
|
|
244
|
-
* ⚠️ This returns a SOURCE CODE STRING, not a real Zod object.
|
|
245
|
-
* It is embedded verbatim inside the generated connector file so that
|
|
246
|
-
* the user's project (which has zod installed) can evaluate it at runtime.
|
|
247
|
-
*
|
|
248
|
-
* Example output:
|
|
249
|
-
* z.object({
|
|
250
|
-
* name: z.string().min(1),
|
|
251
|
-
* status: z.enum(['available','pending','sold']).optional(),
|
|
252
|
-
* })
|
|
253
|
-
*/
|
|
254
|
-
export function buildZodSchema(schema: OpenApiSchema): string {
|
|
255
|
-
if (!schema.properties) {
|
|
256
|
-
return 'z.object({})';
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const required = new Set(schema.required ?? []);
|
|
260
|
-
|
|
261
|
-
const lines = Object.entries(schema.properties).map(([key, prop]) => {
|
|
262
|
-
const expr = zodExpressionFromProp(prop, required.has(key));
|
|
263
|
-
const readOnly = prop.readOnly ? ' // readOnly — excluded from form' : '';
|
|
264
|
-
return ` ${key}: ${expr},${readOnly}`;
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
return `z.object({\n${lines.join('\n')}\n})`;
|
|
268
|
-
}
|
|
1
|
+
import { pascalCase } from 'change-case';
|
|
2
|
+
import type {
|
|
3
|
+
ColumnDef,
|
|
4
|
+
ColumnType,
|
|
5
|
+
FieldType,
|
|
6
|
+
FormFieldDef,
|
|
7
|
+
OpenApiPropertySchema,
|
|
8
|
+
OpenApiSchema,
|
|
9
|
+
} from './types.js';
|
|
10
|
+
|
|
11
|
+
// ─── Column mapping ───────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Derive table ColumnDef[] from a response schema (list or detail).
|
|
15
|
+
* When the response schema is an array, we read the items schema.
|
|
16
|
+
*/
|
|
17
|
+
export function mapColumnsFromSchema(schema: OpenApiSchema): ColumnDef[] {
|
|
18
|
+
// If the response is an array, inspect the items object schema
|
|
19
|
+
const objectSchema = schema.type === 'array' && schema.items ? schema.items : schema;
|
|
20
|
+
|
|
21
|
+
if (!objectSchema.properties) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return Object.entries(objectSchema.properties).map(([key, prop]) => ({
|
|
26
|
+
key,
|
|
27
|
+
label: pascalCase(key)
|
|
28
|
+
.replace(/([A-Z])/g, ' $1')
|
|
29
|
+
.trim(),
|
|
30
|
+
type: columnTypeFromProp(prop),
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function columnTypeFromProp(prop: OpenApiPropertySchema): ColumnType {
|
|
35
|
+
if (prop.enum) {
|
|
36
|
+
return 'badge';
|
|
37
|
+
}
|
|
38
|
+
if (prop.type === 'boolean') {
|
|
39
|
+
return 'boolean';
|
|
40
|
+
}
|
|
41
|
+
if (prop.type === 'integer' || prop.type === 'number') {
|
|
42
|
+
return 'number';
|
|
43
|
+
}
|
|
44
|
+
if (prop.format === 'date' || prop.format === 'date-time') {
|
|
45
|
+
return 'date';
|
|
46
|
+
}
|
|
47
|
+
return 'text';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ─── Form field mapping ───────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Derive FormFieldDef[] from a request body schema.
|
|
54
|
+
* readOnly fields are included as hidden: true (developer can override).
|
|
55
|
+
*/
|
|
56
|
+
export function mapFieldsFromSchema(schema: OpenApiSchema): FormFieldDef[] {
|
|
57
|
+
if (!schema.properties) {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const required = new Set(schema.required ?? []);
|
|
62
|
+
|
|
63
|
+
return Object.entries(schema.properties).map(([key, prop]) => {
|
|
64
|
+
const isRequired = required.has(key);
|
|
65
|
+
const fieldType = fieldTypeFromProp(prop);
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
key,
|
|
69
|
+
label: labelFromKey(key),
|
|
70
|
+
type: fieldType,
|
|
71
|
+
required: isRequired,
|
|
72
|
+
hidden: prop.readOnly === true,
|
|
73
|
+
options: optionsFromProp(prop),
|
|
74
|
+
placeholder: undefined,
|
|
75
|
+
zodExpression: zodExpressionFromProp(prop, isRequired),
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function labelFromKey(key: string): string {
|
|
81
|
+
// Split camelCase/PascalCase into words and capitalise the first letter.
|
|
82
|
+
// 'photoUrls' → 'Photo Urls', 'firstName' → 'First Name'
|
|
83
|
+
return key
|
|
84
|
+
.replace(/([A-Z])/g, ' $1')
|
|
85
|
+
.replace(/^./, (c) => c.toUpperCase())
|
|
86
|
+
.trim();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function fieldTypeFromProp(prop: OpenApiPropertySchema): FieldType {
|
|
90
|
+
if (prop.readOnly) {
|
|
91
|
+
return 'input'; // will be hidden anyway
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (prop.enum) {
|
|
95
|
+
return 'select';
|
|
96
|
+
}
|
|
97
|
+
if (prop.type === 'boolean') {
|
|
98
|
+
return 'checkbox';
|
|
99
|
+
}
|
|
100
|
+
if (prop.type === 'integer' || prop.type === 'number') {
|
|
101
|
+
return 'number';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (prop.type === 'string') {
|
|
105
|
+
if (prop.format === 'date' || prop.format === 'date-time') {
|
|
106
|
+
return 'datepicker';
|
|
107
|
+
}
|
|
108
|
+
if (typeof prop.maxLength === 'number' && prop.maxLength > 200) {
|
|
109
|
+
return 'textarea';
|
|
110
|
+
}
|
|
111
|
+
return 'input';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// array, object → input as fallback
|
|
115
|
+
return 'input';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function optionsFromProp(prop: OpenApiPropertySchema): FormFieldDef['options'] {
|
|
119
|
+
if (!prop.enum) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return prop.enum.map((v) => ({ label: String(v), value: v }));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ─── Zod expression generation ────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Generate a Zod expression string for a single OpenAPI property.
|
|
129
|
+
*
|
|
130
|
+
* Returns a SOURCE CODE STRING (e.g. 'z.string().min(3)') that will be
|
|
131
|
+
* embedded inside the generated connector file, not evaluated here.
|
|
132
|
+
*
|
|
133
|
+
* Validation constraints (minLength, maximum, enum…) are read directly
|
|
134
|
+
* from the OpenAPI property schema and chained onto the Zod expression.
|
|
135
|
+
*/
|
|
136
|
+
export function zodExpressionFromProp(prop: OpenApiPropertySchema, isRequired: boolean): string {
|
|
137
|
+
let expr = baseZodExpr(prop);
|
|
138
|
+
|
|
139
|
+
if (!isRequired) {
|
|
140
|
+
expr += '.optional()';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return expr;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function baseZodExpr(prop: OpenApiPropertySchema): string {
|
|
147
|
+
// Enum
|
|
148
|
+
if (prop.enum && prop.enum.length > 0) {
|
|
149
|
+
const values = prop.enum.map((v) => JSON.stringify(v)).join(', ');
|
|
150
|
+
return `z.enum([${values}])`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
switch (prop.type) {
|
|
154
|
+
case 'string':
|
|
155
|
+
return stringZodExpr(prop);
|
|
156
|
+
|
|
157
|
+
case 'integer':
|
|
158
|
+
return integerZodExpr(prop);
|
|
159
|
+
|
|
160
|
+
case 'number':
|
|
161
|
+
return numberZodExpr(prop);
|
|
162
|
+
|
|
163
|
+
case 'boolean':
|
|
164
|
+
return 'z.boolean()';
|
|
165
|
+
|
|
166
|
+
case 'array':
|
|
167
|
+
return arrayZodExpr(prop);
|
|
168
|
+
|
|
169
|
+
case 'object':
|
|
170
|
+
return 'z.record(z.unknown())';
|
|
171
|
+
|
|
172
|
+
default:
|
|
173
|
+
// $ref already resolved, unknown type → permissive
|
|
174
|
+
return 'z.unknown()';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function stringZodExpr(prop: OpenApiPropertySchema): string {
|
|
179
|
+
const expr = 'z.string()';
|
|
180
|
+
|
|
181
|
+
if (prop.format === 'email') {
|
|
182
|
+
return `${expr}.email()`;
|
|
183
|
+
}
|
|
184
|
+
if (prop.format === 'uri' || prop.format === 'url') {
|
|
185
|
+
return `${expr}.url()`;
|
|
186
|
+
}
|
|
187
|
+
if (prop.format === 'uuid') {
|
|
188
|
+
return `${expr}.uuid()`;
|
|
189
|
+
}
|
|
190
|
+
if (prop.format === 'date' || prop.format === 'date-time') {
|
|
191
|
+
return `${expr}.datetime()`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let chained = expr;
|
|
195
|
+
if (typeof prop.minLength === 'number') {
|
|
196
|
+
chained += `.min(${prop.minLength})`;
|
|
197
|
+
}
|
|
198
|
+
if (typeof prop.maxLength === 'number') {
|
|
199
|
+
chained += `.max(${prop.maxLength})`;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return chained;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function integerZodExpr(prop: OpenApiPropertySchema): string {
|
|
206
|
+
let expr = 'z.number().int()';
|
|
207
|
+
if (typeof prop.minimum === 'number') {
|
|
208
|
+
expr += `.min(${prop.minimum})`;
|
|
209
|
+
}
|
|
210
|
+
if (typeof prop.maximum === 'number') {
|
|
211
|
+
expr += `.max(${prop.maximum})`;
|
|
212
|
+
}
|
|
213
|
+
return expr;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function numberZodExpr(prop: OpenApiPropertySchema): string {
|
|
217
|
+
let expr = 'z.number()';
|
|
218
|
+
if (typeof prop.minimum === 'number') {
|
|
219
|
+
expr += `.min(${prop.minimum})`;
|
|
220
|
+
}
|
|
221
|
+
if (typeof prop.maximum === 'number') {
|
|
222
|
+
expr += `.max(${prop.maximum})`;
|
|
223
|
+
}
|
|
224
|
+
return expr;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function arrayZodExpr(prop: OpenApiPropertySchema): string {
|
|
228
|
+
const itemExpr = prop.items ? baseZodExpr(prop.items) : 'z.unknown()';
|
|
229
|
+
let expr = `z.array(${itemExpr})`;
|
|
230
|
+
if (typeof prop.minItems === 'number') {
|
|
231
|
+
expr += `.min(${prop.minItems})`;
|
|
232
|
+
}
|
|
233
|
+
if (typeof prop.maxItems === 'number') {
|
|
234
|
+
expr += `.max(${prop.maxItems})`;
|
|
235
|
+
}
|
|
236
|
+
return expr;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ─── Full Zod object schema string ───────────────────────────────────────────
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Build a complete z.object({...}) string from a request body schema.
|
|
243
|
+
*
|
|
244
|
+
* ⚠️ This returns a SOURCE CODE STRING, not a real Zod object.
|
|
245
|
+
* It is embedded verbatim inside the generated connector file so that
|
|
246
|
+
* the user's project (which has zod installed) can evaluate it at runtime.
|
|
247
|
+
*
|
|
248
|
+
* Example output:
|
|
249
|
+
* z.object({
|
|
250
|
+
* name: z.string().min(1),
|
|
251
|
+
* status: z.enum(['available','pending','sold']).optional(),
|
|
252
|
+
* })
|
|
253
|
+
*/
|
|
254
|
+
export function buildZodSchema(schema: OpenApiSchema): string {
|
|
255
|
+
if (!schema.properties) {
|
|
256
|
+
return 'z.object({})';
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const required = new Set(schema.required ?? []);
|
|
260
|
+
|
|
261
|
+
const lines = Object.entries(schema.properties).map(([key, prop]) => {
|
|
262
|
+
const expr = zodExpressionFromProp(prop, required.has(key));
|
|
263
|
+
const readOnly = prop.readOnly ? ' // readOnly — excluded from form' : '';
|
|
264
|
+
return ` ${key}: ${expr},${readOnly}`;
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
return `z.object({\n${lines.join('\n')}\n})`;
|
|
268
|
+
}
|