@intrig/plugin-next 0.0.15-30 → 0.0.15-31
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/dist/index.cjs +95 -58
- package/dist/index.js +95 -58
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -3326,38 +3326,10 @@ function serverIndexTemplate(requestProperties, serverExports = [], internalGene
|
|
|
3326
3326
|
`;
|
|
3327
3327
|
}
|
|
3328
3328
|
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
const simpleType = (await pluginSdk.jsonLiteral('')`${JSON.stringify(schema)}`).content;
|
|
3334
|
-
const transport = schema.type === 'string' && schema.format === 'binary' ? 'binary' : 'json';
|
|
3335
|
-
var _JSON_stringify;
|
|
3336
|
-
return ts`
|
|
3337
|
-
import { z } from 'zod'
|
|
3338
|
-
|
|
3339
|
-
${[
|
|
3340
|
-
...imports
|
|
3341
|
-
].join('\n')}
|
|
3342
|
-
|
|
3343
|
-
//--- Zod Schemas ---//
|
|
3344
|
-
|
|
3345
|
-
export const ${typeName}Schema = ${zodSchema}
|
|
3346
|
-
|
|
3347
|
-
//--- Typescript Type ---//
|
|
3348
|
-
|
|
3349
|
-
export type ${typeName} = ${tsType}
|
|
3350
|
-
|
|
3351
|
-
//--- JSON Schema ---//
|
|
3352
|
-
|
|
3353
|
-
export const ${typeName}_jsonschema = ${(_JSON_stringify = JSON.stringify(schema)) != null ? _JSON_stringify : "{}"}
|
|
3354
|
-
|
|
3355
|
-
//--- Simple Type ---//
|
|
3356
|
-
/*[${simpleType}]*/
|
|
3357
|
-
|
|
3358
|
-
// Transport hint for clients ("binary" => use arraybuffer/blob)
|
|
3359
|
-
export const ${typeName}_transport = '${transport}' as const;
|
|
3360
|
-
`;
|
|
3329
|
+
// Helper to escape string values for use in generated code
|
|
3330
|
+
function escapeStringValue(value) {
|
|
3331
|
+
return value.replace(/\\/g, '\\\\') // Escape backslashes first
|
|
3332
|
+
.replace(/'/g, "\\'"); // Escape single quotes
|
|
3361
3333
|
}
|
|
3362
3334
|
function isRef(schema) {
|
|
3363
3335
|
return '$ref' in (schema != null ? schema : {});
|
|
@@ -3412,8 +3384,8 @@ function handleStringSchema(schema) {
|
|
|
3412
3384
|
const imports = new Set();
|
|
3413
3385
|
let binaryish = false;
|
|
3414
3386
|
if (schema.enum) {
|
|
3415
|
-
const enumValues = schema.enum.map((value)=>`'${value}'`).join(' | ');
|
|
3416
|
-
const zodEnum = `z.enum([${schema.enum.map((value)=>`'${value}'`).join(', ')}])`;
|
|
3387
|
+
const enumValues = schema.enum.map((value)=>`'${escapeStringValue(String(value))}'`).join(' | ');
|
|
3388
|
+
const zodEnum = `z.enum([${schema.enum.map((value)=>`'${escapeStringValue(String(value))}'`).join(', ')}])`;
|
|
3417
3389
|
return {
|
|
3418
3390
|
tsType: enumValues,
|
|
3419
3391
|
zodSchema: zodEnum,
|
|
@@ -3435,7 +3407,7 @@ function handleStringSchema(schema) {
|
|
|
3435
3407
|
} else if (schema.format === 'time') {
|
|
3436
3408
|
zodSchema = 'z.string()';
|
|
3437
3409
|
if (schema.pattern) {
|
|
3438
|
-
zodSchema += `.regex(new RegExp('${schema.pattern}'))`;
|
|
3410
|
+
zodSchema += `.regex(new RegExp('${escapeStringValue(schema.pattern)}'))`;
|
|
3439
3411
|
}
|
|
3440
3412
|
} else if (schema.format === 'date-time' && !schema.pattern) {
|
|
3441
3413
|
tsType = 'Date';
|
|
@@ -3450,12 +3422,30 @@ function handleStringSchema(schema) {
|
|
|
3450
3422
|
} else if (schema.format === 'binary') {
|
|
3451
3423
|
tsType = 'BinaryData';
|
|
3452
3424
|
zodSchema = 'BinaryDataSchema';
|
|
3453
|
-
|
|
3425
|
+
// Apply file size constraints using refinements
|
|
3426
|
+
if (schema.minLength !== undefined) {
|
|
3427
|
+
zodSchema += `.refine((blob) => blob.size >= ${schema.minLength}, { message: 'File size must be at least ${schema.minLength} bytes' })`;
|
|
3428
|
+
}
|
|
3429
|
+
if (schema.maxLength !== undefined) {
|
|
3430
|
+
zodSchema += `.refine((blob) => blob.size <= ${schema.maxLength}, { message: 'File size must not exceed ${schema.maxLength} bytes' })`;
|
|
3431
|
+
}
|
|
3432
|
+
imports.add(`import { BinaryData, BinaryDataSchema } from '@intrig/next/type-utils'`);
|
|
3454
3433
|
binaryish = true;
|
|
3455
3434
|
} else if (schema.format === 'byte') {
|
|
3456
3435
|
tsType = 'Uint8Array';
|
|
3457
3436
|
zodSchema = 'z.string().transform((val) => base64ToUint8Array(val))';
|
|
3458
|
-
|
|
3437
|
+
// Note: For byte (base64), length constraints apply to the decoded data
|
|
3438
|
+
// We can't easily validate before transform, so we validate after
|
|
3439
|
+
if (schema.minLength !== undefined || schema.maxLength !== undefined) {
|
|
3440
|
+
zodSchema = `(${zodSchema})`;
|
|
3441
|
+
if (schema.minLength !== undefined) {
|
|
3442
|
+
zodSchema += `.refine((arr) => arr.length >= ${schema.minLength}, { message: 'Data size must be at least ${schema.minLength} bytes' })`;
|
|
3443
|
+
}
|
|
3444
|
+
if (schema.maxLength !== undefined) {
|
|
3445
|
+
zodSchema += `.refine((arr) => arr.length <= ${schema.maxLength}, { message: 'Data size must not exceed ${schema.maxLength} bytes' })`;
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
imports.add(`import { base64ToUint8Array } from '@intrig/next/type-utils'`);
|
|
3459
3449
|
binaryish = true;
|
|
3460
3450
|
} else if (schema.format === 'email') {
|
|
3461
3451
|
zodSchema = 'z.string().email()';
|
|
@@ -3472,7 +3462,7 @@ function handleStringSchema(schema) {
|
|
|
3472
3462
|
} else {
|
|
3473
3463
|
if (schema.minLength !== undefined) zodSchema += `.min(${schema.minLength})`;
|
|
3474
3464
|
if (schema.maxLength !== undefined) zodSchema += `.max(${schema.maxLength})`;
|
|
3475
|
-
if (schema.pattern !== undefined) zodSchema += `.regex(new RegExp('${schema.pattern}'))`;
|
|
3465
|
+
if (schema.pattern !== undefined) zodSchema += `.regex(new RegExp('${escapeStringValue(schema.pattern)}'))`;
|
|
3476
3466
|
}
|
|
3477
3467
|
return {
|
|
3478
3468
|
tsType,
|
|
@@ -3483,8 +3473,11 @@ function handleStringSchema(schema) {
|
|
|
3483
3473
|
}
|
|
3484
3474
|
function handleNumberSchema(schema) {
|
|
3485
3475
|
let zodSchema = 'z.number()';
|
|
3486
|
-
|
|
3487
|
-
if (schema.
|
|
3476
|
+
// Handle exclusive bounds (OpenAPI 3.1 style - exclusiveMinimum/Maximum are numbers)
|
|
3477
|
+
if (schema.exclusiveMinimum !== undefined) zodSchema += `.gt(${schema.exclusiveMinimum})`;
|
|
3478
|
+
else if (schema.minimum !== undefined) zodSchema += `.min(${schema.minimum})`;
|
|
3479
|
+
if (schema.exclusiveMaximum !== undefined) zodSchema += `.lt(${schema.exclusiveMaximum})`;
|
|
3480
|
+
else if (schema.maximum !== undefined) zodSchema += `.max(${schema.maximum})`;
|
|
3488
3481
|
return {
|
|
3489
3482
|
tsType: 'number',
|
|
3490
3483
|
zodSchema,
|
|
@@ -3493,8 +3486,11 @@ function handleNumberSchema(schema) {
|
|
|
3493
3486
|
}
|
|
3494
3487
|
function handleIntegerSchema(schema) {
|
|
3495
3488
|
let zodSchema = 'z.number().int()';
|
|
3496
|
-
|
|
3497
|
-
if (schema.
|
|
3489
|
+
// Handle exclusive bounds (OpenAPI 3.1 style - exclusiveMinimum/Maximum are numbers)
|
|
3490
|
+
if (schema.exclusiveMinimum !== undefined) zodSchema += `.gt(${schema.exclusiveMinimum})`;
|
|
3491
|
+
else if (schema.minimum !== undefined) zodSchema += `.min(${schema.minimum})`;
|
|
3492
|
+
if (schema.exclusiveMaximum !== undefined) zodSchema += `.lt(${schema.exclusiveMaximum})`;
|
|
3493
|
+
else if (schema.maximum !== undefined) zodSchema += `.max(${schema.maximum})`;
|
|
3498
3494
|
return {
|
|
3499
3495
|
tsType: 'number',
|
|
3500
3496
|
zodSchema,
|
|
@@ -3514,9 +3510,11 @@ function handleArraySchema(schema, imports) {
|
|
|
3514
3510
|
throw new Error('Array schema must have an items property');
|
|
3515
3511
|
}
|
|
3516
3512
|
const { tsType, zodSchema: itemZodSchema, imports: itemImports, binaryish } = openApiSchemaToZod(schema.items, imports);
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
if (schema.
|
|
3513
|
+
// Build the array schema with constraints
|
|
3514
|
+
let arraySchema = `z.array(${itemZodSchema})`;
|
|
3515
|
+
if (schema.minItems !== undefined) arraySchema += `.min(${schema.minItems})`;
|
|
3516
|
+
if (schema.maxItems !== undefined) arraySchema += `.max(${schema.maxItems})`;
|
|
3517
|
+
const zodSchema = binaryish ? arraySchema : `(z.preprocess((raw) => (Array.isArray(raw) ? raw : [raw]), ${arraySchema}) as z.ZodType<(${tsType})[], z.ZodTypeDef, (${tsType})[]>)`;
|
|
3520
3518
|
return {
|
|
3521
3519
|
tsType: `(${tsType})[]`,
|
|
3522
3520
|
zodSchema,
|
|
@@ -3587,21 +3585,26 @@ function handleComplexSchema(schema, imports) {
|
|
|
3587
3585
|
const options = schema.allOf.map((subSchema)=>openApiSchemaToZod(subSchema));
|
|
3588
3586
|
const zodSchemas = options.map((option)=>option.zodSchema);
|
|
3589
3587
|
const tsTypes = options.map((option)=>option.tsType);
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
3597
|
-
|
|
3588
|
+
const allImports = new Set([
|
|
3589
|
+
...imports,
|
|
3590
|
+
...options.flatMap((option)=>Array.from(option.imports))
|
|
3591
|
+
]);
|
|
3592
|
+
if (zodSchemas.length === 1) {
|
|
3593
|
+
return {
|
|
3594
|
+
tsType: tsTypes[0],
|
|
3595
|
+
zodSchema: zodSchemas[0],
|
|
3596
|
+
imports: allImports
|
|
3597
|
+
};
|
|
3598
|
+
}
|
|
3599
|
+
// Use .and() chaining instead of z.intersection() which only takes 2 arguments
|
|
3600
|
+
const combinedZodSchema = zodSchemas.reduce((acc, schema, index)=>{
|
|
3601
|
+
if (index === 0) return schema;
|
|
3602
|
+
return `${acc}.and(${schema})`;
|
|
3603
|
+
});
|
|
3598
3604
|
return {
|
|
3599
3605
|
tsType: tsTypes.join(' & '),
|
|
3600
|
-
zodSchema:
|
|
3601
|
-
imports:
|
|
3602
|
-
...imports,
|
|
3603
|
-
...options.flatMap((option)=>Array.from(option.imports))
|
|
3604
|
-
])
|
|
3606
|
+
zodSchema: combinedZodSchema,
|
|
3607
|
+
imports: allImports
|
|
3605
3608
|
};
|
|
3606
3609
|
}
|
|
3607
3610
|
return {
|
|
@@ -3611,6 +3614,40 @@ function handleComplexSchema(schema, imports) {
|
|
|
3611
3614
|
};
|
|
3612
3615
|
}
|
|
3613
3616
|
|
|
3617
|
+
async function typeTemplate(descriptor) {
|
|
3618
|
+
const { data: { schema, name: typeName }, source } = descriptor;
|
|
3619
|
+
const { imports, zodSchema, tsType } = openApiSchemaToZod(schema);
|
|
3620
|
+
const ts = pluginSdk.typescript(path__namespace.resolve('src', source, 'components', 'schemas', `${typeName}.ts`));
|
|
3621
|
+
const simpleType = (await pluginSdk.jsonLiteral('')`${JSON.stringify(schema)}`).content;
|
|
3622
|
+
const transport = schema.type === 'string' && schema.format === 'binary' ? 'binary' : 'json';
|
|
3623
|
+
var _JSON_stringify;
|
|
3624
|
+
return ts`
|
|
3625
|
+
import { z } from 'zod'
|
|
3626
|
+
|
|
3627
|
+
${[
|
|
3628
|
+
...imports
|
|
3629
|
+
].join('\n')}
|
|
3630
|
+
|
|
3631
|
+
//--- Zod Schemas ---//
|
|
3632
|
+
|
|
3633
|
+
export const ${typeName}Schema = ${zodSchema}
|
|
3634
|
+
|
|
3635
|
+
//--- Typescript Type ---//
|
|
3636
|
+
|
|
3637
|
+
export type ${typeName} = ${tsType}
|
|
3638
|
+
|
|
3639
|
+
//--- JSON Schema ---//
|
|
3640
|
+
|
|
3641
|
+
export const ${typeName}_jsonschema = ${(_JSON_stringify = JSON.stringify(schema)) != null ? _JSON_stringify : "{}"}
|
|
3642
|
+
|
|
3643
|
+
//--- Simple Type ---//
|
|
3644
|
+
/*[${simpleType}]*/
|
|
3645
|
+
|
|
3646
|
+
// Transport hint for clients ("binary" => use arraybuffer/blob)
|
|
3647
|
+
export const ${typeName}_transport = '${transport}' as const;
|
|
3648
|
+
`;
|
|
3649
|
+
}
|
|
3650
|
+
|
|
3614
3651
|
function serverFunctionDocs(descriptor) {
|
|
3615
3652
|
const md = pluginSdk.mdLiteral('server-function.md');
|
|
3616
3653
|
var _descriptor_data_variables;
|
package/dist/index.js
CHANGED
|
@@ -3300,38 +3300,10 @@ function serverIndexTemplate(requestProperties, serverExports = [], internalGene
|
|
|
3300
3300
|
`;
|
|
3301
3301
|
}
|
|
3302
3302
|
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
const simpleType = (await jsonLiteral('')`${JSON.stringify(schema)}`).content;
|
|
3308
|
-
const transport = schema.type === 'string' && schema.format === 'binary' ? 'binary' : 'json';
|
|
3309
|
-
var _JSON_stringify;
|
|
3310
|
-
return ts`
|
|
3311
|
-
import { z } from 'zod'
|
|
3312
|
-
|
|
3313
|
-
${[
|
|
3314
|
-
...imports
|
|
3315
|
-
].join('\n')}
|
|
3316
|
-
|
|
3317
|
-
//--- Zod Schemas ---//
|
|
3318
|
-
|
|
3319
|
-
export const ${typeName}Schema = ${zodSchema}
|
|
3320
|
-
|
|
3321
|
-
//--- Typescript Type ---//
|
|
3322
|
-
|
|
3323
|
-
export type ${typeName} = ${tsType}
|
|
3324
|
-
|
|
3325
|
-
//--- JSON Schema ---//
|
|
3326
|
-
|
|
3327
|
-
export const ${typeName}_jsonschema = ${(_JSON_stringify = JSON.stringify(schema)) != null ? _JSON_stringify : "{}"}
|
|
3328
|
-
|
|
3329
|
-
//--- Simple Type ---//
|
|
3330
|
-
/*[${simpleType}]*/
|
|
3331
|
-
|
|
3332
|
-
// Transport hint for clients ("binary" => use arraybuffer/blob)
|
|
3333
|
-
export const ${typeName}_transport = '${transport}' as const;
|
|
3334
|
-
`;
|
|
3303
|
+
// Helper to escape string values for use in generated code
|
|
3304
|
+
function escapeStringValue(value) {
|
|
3305
|
+
return value.replace(/\\/g, '\\\\') // Escape backslashes first
|
|
3306
|
+
.replace(/'/g, "\\'"); // Escape single quotes
|
|
3335
3307
|
}
|
|
3336
3308
|
function isRef(schema) {
|
|
3337
3309
|
return '$ref' in (schema != null ? schema : {});
|
|
@@ -3386,8 +3358,8 @@ function handleStringSchema(schema) {
|
|
|
3386
3358
|
const imports = new Set();
|
|
3387
3359
|
let binaryish = false;
|
|
3388
3360
|
if (schema.enum) {
|
|
3389
|
-
const enumValues = schema.enum.map((value)=>`'${value}'`).join(' | ');
|
|
3390
|
-
const zodEnum = `z.enum([${schema.enum.map((value)=>`'${value}'`).join(', ')}])`;
|
|
3361
|
+
const enumValues = schema.enum.map((value)=>`'${escapeStringValue(String(value))}'`).join(' | ');
|
|
3362
|
+
const zodEnum = `z.enum([${schema.enum.map((value)=>`'${escapeStringValue(String(value))}'`).join(', ')}])`;
|
|
3391
3363
|
return {
|
|
3392
3364
|
tsType: enumValues,
|
|
3393
3365
|
zodSchema: zodEnum,
|
|
@@ -3409,7 +3381,7 @@ function handleStringSchema(schema) {
|
|
|
3409
3381
|
} else if (schema.format === 'time') {
|
|
3410
3382
|
zodSchema = 'z.string()';
|
|
3411
3383
|
if (schema.pattern) {
|
|
3412
|
-
zodSchema += `.regex(new RegExp('${schema.pattern}'))`;
|
|
3384
|
+
zodSchema += `.regex(new RegExp('${escapeStringValue(schema.pattern)}'))`;
|
|
3413
3385
|
}
|
|
3414
3386
|
} else if (schema.format === 'date-time' && !schema.pattern) {
|
|
3415
3387
|
tsType = 'Date';
|
|
@@ -3424,12 +3396,30 @@ function handleStringSchema(schema) {
|
|
|
3424
3396
|
} else if (schema.format === 'binary') {
|
|
3425
3397
|
tsType = 'BinaryData';
|
|
3426
3398
|
zodSchema = 'BinaryDataSchema';
|
|
3427
|
-
|
|
3399
|
+
// Apply file size constraints using refinements
|
|
3400
|
+
if (schema.minLength !== undefined) {
|
|
3401
|
+
zodSchema += `.refine((blob) => blob.size >= ${schema.minLength}, { message: 'File size must be at least ${schema.minLength} bytes' })`;
|
|
3402
|
+
}
|
|
3403
|
+
if (schema.maxLength !== undefined) {
|
|
3404
|
+
zodSchema += `.refine((blob) => blob.size <= ${schema.maxLength}, { message: 'File size must not exceed ${schema.maxLength} bytes' })`;
|
|
3405
|
+
}
|
|
3406
|
+
imports.add(`import { BinaryData, BinaryDataSchema } from '@intrig/next/type-utils'`);
|
|
3428
3407
|
binaryish = true;
|
|
3429
3408
|
} else if (schema.format === 'byte') {
|
|
3430
3409
|
tsType = 'Uint8Array';
|
|
3431
3410
|
zodSchema = 'z.string().transform((val) => base64ToUint8Array(val))';
|
|
3432
|
-
|
|
3411
|
+
// Note: For byte (base64), length constraints apply to the decoded data
|
|
3412
|
+
// We can't easily validate before transform, so we validate after
|
|
3413
|
+
if (schema.minLength !== undefined || schema.maxLength !== undefined) {
|
|
3414
|
+
zodSchema = `(${zodSchema})`;
|
|
3415
|
+
if (schema.minLength !== undefined) {
|
|
3416
|
+
zodSchema += `.refine((arr) => arr.length >= ${schema.minLength}, { message: 'Data size must be at least ${schema.minLength} bytes' })`;
|
|
3417
|
+
}
|
|
3418
|
+
if (schema.maxLength !== undefined) {
|
|
3419
|
+
zodSchema += `.refine((arr) => arr.length <= ${schema.maxLength}, { message: 'Data size must not exceed ${schema.maxLength} bytes' })`;
|
|
3420
|
+
}
|
|
3421
|
+
}
|
|
3422
|
+
imports.add(`import { base64ToUint8Array } from '@intrig/next/type-utils'`);
|
|
3433
3423
|
binaryish = true;
|
|
3434
3424
|
} else if (schema.format === 'email') {
|
|
3435
3425
|
zodSchema = 'z.string().email()';
|
|
@@ -3446,7 +3436,7 @@ function handleStringSchema(schema) {
|
|
|
3446
3436
|
} else {
|
|
3447
3437
|
if (schema.minLength !== undefined) zodSchema += `.min(${schema.minLength})`;
|
|
3448
3438
|
if (schema.maxLength !== undefined) zodSchema += `.max(${schema.maxLength})`;
|
|
3449
|
-
if (schema.pattern !== undefined) zodSchema += `.regex(new RegExp('${schema.pattern}'))`;
|
|
3439
|
+
if (schema.pattern !== undefined) zodSchema += `.regex(new RegExp('${escapeStringValue(schema.pattern)}'))`;
|
|
3450
3440
|
}
|
|
3451
3441
|
return {
|
|
3452
3442
|
tsType,
|
|
@@ -3457,8 +3447,11 @@ function handleStringSchema(schema) {
|
|
|
3457
3447
|
}
|
|
3458
3448
|
function handleNumberSchema(schema) {
|
|
3459
3449
|
let zodSchema = 'z.number()';
|
|
3460
|
-
|
|
3461
|
-
if (schema.
|
|
3450
|
+
// Handle exclusive bounds (OpenAPI 3.1 style - exclusiveMinimum/Maximum are numbers)
|
|
3451
|
+
if (schema.exclusiveMinimum !== undefined) zodSchema += `.gt(${schema.exclusiveMinimum})`;
|
|
3452
|
+
else if (schema.minimum !== undefined) zodSchema += `.min(${schema.minimum})`;
|
|
3453
|
+
if (schema.exclusiveMaximum !== undefined) zodSchema += `.lt(${schema.exclusiveMaximum})`;
|
|
3454
|
+
else if (schema.maximum !== undefined) zodSchema += `.max(${schema.maximum})`;
|
|
3462
3455
|
return {
|
|
3463
3456
|
tsType: 'number',
|
|
3464
3457
|
zodSchema,
|
|
@@ -3467,8 +3460,11 @@ function handleNumberSchema(schema) {
|
|
|
3467
3460
|
}
|
|
3468
3461
|
function handleIntegerSchema(schema) {
|
|
3469
3462
|
let zodSchema = 'z.number().int()';
|
|
3470
|
-
|
|
3471
|
-
if (schema.
|
|
3463
|
+
// Handle exclusive bounds (OpenAPI 3.1 style - exclusiveMinimum/Maximum are numbers)
|
|
3464
|
+
if (schema.exclusiveMinimum !== undefined) zodSchema += `.gt(${schema.exclusiveMinimum})`;
|
|
3465
|
+
else if (schema.minimum !== undefined) zodSchema += `.min(${schema.minimum})`;
|
|
3466
|
+
if (schema.exclusiveMaximum !== undefined) zodSchema += `.lt(${schema.exclusiveMaximum})`;
|
|
3467
|
+
else if (schema.maximum !== undefined) zodSchema += `.max(${schema.maximum})`;
|
|
3472
3468
|
return {
|
|
3473
3469
|
tsType: 'number',
|
|
3474
3470
|
zodSchema,
|
|
@@ -3488,9 +3484,11 @@ function handleArraySchema(schema, imports) {
|
|
|
3488
3484
|
throw new Error('Array schema must have an items property');
|
|
3489
3485
|
}
|
|
3490
3486
|
const { tsType, zodSchema: itemZodSchema, imports: itemImports, binaryish } = openApiSchemaToZod(schema.items, imports);
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
if (schema.
|
|
3487
|
+
// Build the array schema with constraints
|
|
3488
|
+
let arraySchema = `z.array(${itemZodSchema})`;
|
|
3489
|
+
if (schema.minItems !== undefined) arraySchema += `.min(${schema.minItems})`;
|
|
3490
|
+
if (schema.maxItems !== undefined) arraySchema += `.max(${schema.maxItems})`;
|
|
3491
|
+
const zodSchema = binaryish ? arraySchema : `(z.preprocess((raw) => (Array.isArray(raw) ? raw : [raw]), ${arraySchema}) as z.ZodType<(${tsType})[], z.ZodTypeDef, (${tsType})[]>)`;
|
|
3494
3492
|
return {
|
|
3495
3493
|
tsType: `(${tsType})[]`,
|
|
3496
3494
|
zodSchema,
|
|
@@ -3561,21 +3559,26 @@ function handleComplexSchema(schema, imports) {
|
|
|
3561
3559
|
const options = schema.allOf.map((subSchema)=>openApiSchemaToZod(subSchema));
|
|
3562
3560
|
const zodSchemas = options.map((option)=>option.zodSchema);
|
|
3563
3561
|
const tsTypes = options.map((option)=>option.tsType);
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3562
|
+
const allImports = new Set([
|
|
3563
|
+
...imports,
|
|
3564
|
+
...options.flatMap((option)=>Array.from(option.imports))
|
|
3565
|
+
]);
|
|
3566
|
+
if (zodSchemas.length === 1) {
|
|
3567
|
+
return {
|
|
3568
|
+
tsType: tsTypes[0],
|
|
3569
|
+
zodSchema: zodSchemas[0],
|
|
3570
|
+
imports: allImports
|
|
3571
|
+
};
|
|
3572
|
+
}
|
|
3573
|
+
// Use .and() chaining instead of z.intersection() which only takes 2 arguments
|
|
3574
|
+
const combinedZodSchema = zodSchemas.reduce((acc, schema, index)=>{
|
|
3575
|
+
if (index === 0) return schema;
|
|
3576
|
+
return `${acc}.and(${schema})`;
|
|
3577
|
+
});
|
|
3572
3578
|
return {
|
|
3573
3579
|
tsType: tsTypes.join(' & '),
|
|
3574
|
-
zodSchema:
|
|
3575
|
-
imports:
|
|
3576
|
-
...imports,
|
|
3577
|
-
...options.flatMap((option)=>Array.from(option.imports))
|
|
3578
|
-
])
|
|
3580
|
+
zodSchema: combinedZodSchema,
|
|
3581
|
+
imports: allImports
|
|
3579
3582
|
};
|
|
3580
3583
|
}
|
|
3581
3584
|
return {
|
|
@@ -3585,6 +3588,40 @@ function handleComplexSchema(schema, imports) {
|
|
|
3585
3588
|
};
|
|
3586
3589
|
}
|
|
3587
3590
|
|
|
3591
|
+
async function typeTemplate(descriptor) {
|
|
3592
|
+
const { data: { schema, name: typeName }, source } = descriptor;
|
|
3593
|
+
const { imports, zodSchema, tsType } = openApiSchemaToZod(schema);
|
|
3594
|
+
const ts = typescript(path.resolve('src', source, 'components', 'schemas', `${typeName}.ts`));
|
|
3595
|
+
const simpleType = (await jsonLiteral('')`${JSON.stringify(schema)}`).content;
|
|
3596
|
+
const transport = schema.type === 'string' && schema.format === 'binary' ? 'binary' : 'json';
|
|
3597
|
+
var _JSON_stringify;
|
|
3598
|
+
return ts`
|
|
3599
|
+
import { z } from 'zod'
|
|
3600
|
+
|
|
3601
|
+
${[
|
|
3602
|
+
...imports
|
|
3603
|
+
].join('\n')}
|
|
3604
|
+
|
|
3605
|
+
//--- Zod Schemas ---//
|
|
3606
|
+
|
|
3607
|
+
export const ${typeName}Schema = ${zodSchema}
|
|
3608
|
+
|
|
3609
|
+
//--- Typescript Type ---//
|
|
3610
|
+
|
|
3611
|
+
export type ${typeName} = ${tsType}
|
|
3612
|
+
|
|
3613
|
+
//--- JSON Schema ---//
|
|
3614
|
+
|
|
3615
|
+
export const ${typeName}_jsonschema = ${(_JSON_stringify = JSON.stringify(schema)) != null ? _JSON_stringify : "{}"}
|
|
3616
|
+
|
|
3617
|
+
//--- Simple Type ---//
|
|
3618
|
+
/*[${simpleType}]*/
|
|
3619
|
+
|
|
3620
|
+
// Transport hint for clients ("binary" => use arraybuffer/blob)
|
|
3621
|
+
export const ${typeName}_transport = '${transport}' as const;
|
|
3622
|
+
`;
|
|
3623
|
+
}
|
|
3624
|
+
|
|
3588
3625
|
function serverFunctionDocs(descriptor) {
|
|
3589
3626
|
const md = mdLiteral('server-function.md');
|
|
3590
3627
|
var _descriptor_data_variables;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intrig/plugin-next",
|
|
3
|
-
"version": "0.0.15-
|
|
3
|
+
"version": "0.0.15-31",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@intrig/plugin-sdk": "^0.0.15-
|
|
18
|
+
"@intrig/plugin-sdk": "^0.0.15-31",
|
|
19
19
|
"@swc/helpers": "~0.5.11",
|
|
20
20
|
"fs-extra": "^11.2.0"
|
|
21
21
|
},
|