Skip to content

Commit 1b89885

Browse files
committed
Use plugin as variable instead of a new config variable
1 parent ccbb1cd commit 1b89885

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

packages/openapi-ts/src/plugins/zod/plugin.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ const zIdentifier = compiler.identifier({ text: 'z' });
4040
const nameTransformer = (name: string) => `z-${name}`;
4141

4242
const arrayTypeToZodSchema = ({
43-
config,
4443
context,
44+
plugin,
4545
result,
4646
schema,
4747
}: {
48-
config: Omit<Config, 'name'>;
4948
context: IR.Context;
49+
plugin: Config;
5050
result: Result;
5151
schema: SchemaWithType<'array'>;
5252
}): ts.CallExpression => {
@@ -75,8 +75,8 @@ const arrayTypeToZodSchema = ({
7575
// at least one item is guaranteed
7676
const itemExpressions = schema.items!.map((item) =>
7777
schemaToZodSchema({
78-
config,
7978
context,
79+
plugin,
8080
result,
8181
schema: item,
8282
}),
@@ -334,13 +334,13 @@ const numberTypeToZodSchema = ({
334334
};
335335

336336
const objectTypeToZodSchema = ({
337-
config,
338337
context,
338+
plugin,
339339
result,
340340
schema,
341341
}: {
342-
config: Omit<Config, 'name'>;
343342
context: IR.Context;
343+
plugin: Config;
344344
result: Result;
345345
schema: SchemaWithType<'object'>;
346346
}) => {
@@ -358,9 +358,9 @@ const objectTypeToZodSchema = ({
358358
const isRequired = required.includes(name);
359359

360360
const propertyExpression = schemaToZodSchema({
361-
config,
362361
context,
363362
optional: !isRequired,
363+
plugin,
364364
result,
365365
schema: property,
366366
});
@@ -438,11 +438,11 @@ const objectTypeToZodSchema = ({
438438
};
439439

440440
const stringTypeToZodSchema = ({
441-
config,
441+
plugin,
442442
schema,
443443
}: {
444-
config: Omit<Config, 'name'>;
445444
context: IR.Context;
445+
plugin: Config;
446446
schema: SchemaWithType<'string'>;
447447
}) => {
448448
if (typeof schema.const === 'string') {
@@ -472,8 +472,8 @@ const stringTypeToZodSchema = ({
472472
name: compiler.identifier({ text: 'datetime' }),
473473
}),
474474
parameters: [
475-
config.dateTimeOptions
476-
? JSON.stringify(config.dateTimeOptions)
475+
plugin.dateTimeOptions
476+
? JSON.stringify(plugin.dateTimeOptions)
477477
: undefined,
478478
],
479479
});
@@ -655,21 +655,21 @@ const voidTypeToZodSchema = ({
655655
};
656656

657657
const schemaTypeToZodSchema = ({
658-
config,
659658
context,
659+
plugin,
660660
result,
661661
schema,
662662
}: {
663-
config: Omit<Config, 'name'>;
664663
context: IR.Context;
664+
plugin: Config;
665665
result: Result;
666666
schema: IR.SchemaObject;
667667
}): ts.Expression => {
668668
switch (schema.type as Required<IR.SchemaObject>['type']) {
669669
case 'array':
670670
return arrayTypeToZodSchema({
671-
config,
672671
context,
672+
plugin,
673673
result,
674674
schema: schema as SchemaWithType<'array'>,
675675
});
@@ -701,15 +701,15 @@ const schemaTypeToZodSchema = ({
701701
});
702702
case 'object':
703703
return objectTypeToZodSchema({
704-
config,
705704
context,
705+
plugin,
706706
result,
707707
schema: schema as SchemaWithType<'object'>,
708708
});
709709
case 'string':
710710
return stringTypeToZodSchema({
711-
config,
712711
context,
712+
plugin,
713713
schema: schema as SchemaWithType<'string'>,
714714
});
715715
case 'tuple':
@@ -736,14 +736,14 @@ const schemaTypeToZodSchema = ({
736736
};
737737

738738
const operationToZodSchema = ({
739-
config,
740739
context,
741740
operation,
741+
plugin,
742742
result,
743743
}: {
744-
config: Omit<Config, 'name'>;
745744
context: IR.Context;
746745
operation: IR.OperationObject;
746+
plugin: Config;
747747
result: Result;
748748
}) => {
749749
if (operation.responses) {
@@ -756,8 +756,8 @@ const operationToZodSchema = ({
756756
id: operation.id,
757757
type: 'response',
758758
}),
759-
config,
760759
context,
760+
plugin,
761761
result,
762762
schema: response,
763763
});
@@ -767,24 +767,24 @@ const operationToZodSchema = ({
767767

768768
const schemaToZodSchema = ({
769769
$ref,
770-
config,
771770
context,
772771
optional,
772+
plugin,
773773
result,
774774
schema,
775775
}: {
776776
/**
777777
* When $ref is supplied, a node will be emitted to the file.
778778
*/
779779
$ref?: string;
780-
config: Omit<Config, 'name'>;
781780
context: IR.Context;
782781
/**
783782
* Accept `optional` to handle optional object properties. We can't handle
784783
* this inside the object function because `.optional()` must come before
785784
* `.default()` which is handled in this function.
786785
*/
787786
optional?: boolean;
787+
plugin: Config;
788788
result: Result;
789789
schema: IR.SchemaObject;
790790
}): ts.Expression => {
@@ -821,8 +821,8 @@ const schemaToZodSchema = ({
821821
if (!identifierRef.name) {
822822
const ref = context.resolveIrRef<IR.SchemaObject>(schema.$ref);
823823
expression = schemaToZodSchema({
824-
config,
825824
context,
825+
plugin,
826826
result,
827827
schema: ref,
828828
});
@@ -860,8 +860,8 @@ const schemaToZodSchema = ({
860860
}
861861
} else if (schema.type) {
862862
expression = schemaTypeToZodSchema({
863-
config,
864863
context,
864+
plugin,
865865
result,
866866
schema,
867867
});
@@ -871,8 +871,8 @@ const schemaToZodSchema = ({
871871
if (schema.items) {
872872
const itemTypes = schema.items.map((item) =>
873873
schemaToZodSchema({
874-
config,
875874
context,
875+
plugin,
876876
result,
877877
schema: item,
878878
}),
@@ -921,17 +921,17 @@ const schemaToZodSchema = ({
921921
}
922922
} else {
923923
expression = schemaToZodSchema({
924-
config,
925924
context,
925+
plugin,
926926
result,
927927
schema,
928928
});
929929
}
930930
} else {
931931
// catch-all fallback for failed schemas
932932
expression = schemaTypeToZodSchema({
933-
config,
934933
context,
934+
plugin,
935935
result,
936936
schema: {
937937
type: 'unknown',
@@ -1010,20 +1010,16 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
10101010
name: 'z',
10111011
});
10121012

1013-
const config: Omit<Config, 'name'> = {
1014-
dateTimeOptions: plugin.dateTimeOptions,
1015-
};
1016-
10171013
context.subscribe('operation', ({ operation }) => {
10181014
const result: Result = {
10191015
circularReferenceTracker: new Set(),
10201016
hasCircularReference: false,
10211017
};
10221018

10231019
operationToZodSchema({
1024-
config,
10251020
context,
10261021
operation,
1022+
plugin,
10271023
result,
10281024
});
10291025
});
@@ -1036,8 +1032,8 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
10361032

10371033
schemaToZodSchema({
10381034
$ref,
1039-
config,
10401035
context,
1036+
plugin,
10411037
result,
10421038
schema,
10431039
});

packages/openapi-ts/src/plugins/zod/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export interface Config extends Plugin.Name<'zod'> {
2222
* Customise the Zod schema name. By default, `z{{name}}` is used,
2323
* where `name` is a definition name or an operation name.
2424
*/
25-
// nameBuilder?: (model: IR.OperationObject | IR.SchemaObject) => string;
26-
/**
25+
// nameBuilder?: (model: IR.OperationObject | IR.SchemaObject) => string;
26+
/**
2727
* Name of the generated file.
2828
*
2929
* @default 'zod'

0 commit comments

Comments
 (0)