@kubb/plugin-zod 5.0.0-alpha.26 → 5.0.0-alpha.27
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 +175 -191
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +165 -76
- package/dist/index.js +176 -192
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/Zod.tsx +10 -9
- package/src/generators/zodGenerator.tsx +28 -52
- package/src/generators/zodGeneratorLegacy.tsx +27 -51
- package/src/index.ts +2 -1
- package/src/plugin.ts +9 -5
- package/src/presets.ts +7 -2
- package/src/printers/printerZod.ts +31 -4
- package/src/printers/printerZodMini.ts +32 -5
- package/src/resolvers/resolverZod.ts +12 -22
- package/src/resolvers/resolverZodLegacy.ts +8 -8
- package/src/types.ts +56 -17
- package/src/components/ZodMini.tsx +0 -41
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { caseParams,
|
|
3
|
+
import { caseParams, createProperty, createSchema, extractRefName, narrowSchema, syncSchemaRef, transform, walk } from "@kubb/ast";
|
|
4
4
|
import { createPlugin, defineGenerator, definePresets, definePrinter, defineResolver, getBarrelFiles, getMode, getPreset, runGeneratorOperation, runGeneratorOperations, runGeneratorSchema } from "@kubb/core";
|
|
5
5
|
import { Const, File, Type } from "@kubb/react-fabric";
|
|
6
6
|
import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime";
|
|
@@ -201,6 +201,39 @@ function Operations({ name, operations }) {
|
|
|
201
201
|
] });
|
|
202
202
|
}
|
|
203
203
|
//#endregion
|
|
204
|
+
//#region src/components/Zod.tsx
|
|
205
|
+
function Zod({ name, node, printer, inferTypeName }) {
|
|
206
|
+
const output = printer.print(node);
|
|
207
|
+
if (!output) return;
|
|
208
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, {
|
|
209
|
+
name,
|
|
210
|
+
isExportable: true,
|
|
211
|
+
isIndexable: true,
|
|
212
|
+
children: /* @__PURE__ */ jsx(Const, {
|
|
213
|
+
export: true,
|
|
214
|
+
name,
|
|
215
|
+
children: output
|
|
216
|
+
})
|
|
217
|
+
}), inferTypeName && /* @__PURE__ */ jsx(File.Source, {
|
|
218
|
+
name: inferTypeName,
|
|
219
|
+
isExportable: true,
|
|
220
|
+
isIndexable: true,
|
|
221
|
+
isTypeOnly: true,
|
|
222
|
+
children: /* @__PURE__ */ jsx(Type, {
|
|
223
|
+
export: true,
|
|
224
|
+
name: inferTypeName,
|
|
225
|
+
children: `z.infer<typeof ${name}>`
|
|
226
|
+
})
|
|
227
|
+
})] });
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/constants.ts
|
|
231
|
+
/**
|
|
232
|
+
* Import paths that use a namespace import (`import * as z from '...'`).
|
|
233
|
+
* All other import paths use a named import (`import { z } from '...'`).
|
|
234
|
+
*/
|
|
235
|
+
const ZOD_NAMESPACE_IMPORTS = new Set(["zod", "zod/mini"]);
|
|
236
|
+
//#endregion
|
|
204
237
|
//#region src/utils.ts
|
|
205
238
|
/**
|
|
206
239
|
* Returns `true` when the given coercion option enables coercion for the specified type.
|
|
@@ -529,7 +562,8 @@ const printerZod = definePrinter((options) => {
|
|
|
529
562
|
if (transformed) base = `${base}.and(${transformed})`;
|
|
530
563
|
}
|
|
531
564
|
return base;
|
|
532
|
-
}
|
|
565
|
+
},
|
|
566
|
+
...options.nodes
|
|
533
567
|
},
|
|
534
568
|
print(node) {
|
|
535
569
|
const { keysToOmit } = this.options;
|
|
@@ -549,39 +583,6 @@ const printerZod = definePrinter((options) => {
|
|
|
549
583
|
};
|
|
550
584
|
});
|
|
551
585
|
//#endregion
|
|
552
|
-
//#region src/components/Zod.tsx
|
|
553
|
-
function Zod({ name, node, coercion, guidType, wrapOutput, inferTypeName, resolver, keysToOmit }) {
|
|
554
|
-
const output = printerZod({
|
|
555
|
-
coercion,
|
|
556
|
-
guidType,
|
|
557
|
-
wrapOutput,
|
|
558
|
-
resolver,
|
|
559
|
-
schemaName: name,
|
|
560
|
-
keysToOmit
|
|
561
|
-
}).print(node);
|
|
562
|
-
if (!output) return;
|
|
563
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, {
|
|
564
|
-
name,
|
|
565
|
-
isExportable: true,
|
|
566
|
-
isIndexable: true,
|
|
567
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
568
|
-
export: true,
|
|
569
|
-
name,
|
|
570
|
-
children: output
|
|
571
|
-
})
|
|
572
|
-
}), inferTypeName && /* @__PURE__ */ jsx(File.Source, {
|
|
573
|
-
name: inferTypeName,
|
|
574
|
-
isExportable: true,
|
|
575
|
-
isIndexable: true,
|
|
576
|
-
isTypeOnly: true,
|
|
577
|
-
children: /* @__PURE__ */ jsx(Type, {
|
|
578
|
-
export: true,
|
|
579
|
-
name: inferTypeName,
|
|
580
|
-
children: `z.infer<typeof ${name}>`
|
|
581
|
-
})
|
|
582
|
-
})] });
|
|
583
|
-
}
|
|
584
|
-
//#endregion
|
|
585
586
|
//#region src/printers/printerZodMini.ts
|
|
586
587
|
/**
|
|
587
588
|
* Zod v4 **Mini** printer built with `definePrinter`.
|
|
@@ -733,7 +734,8 @@ const printerZodMini = definePrinter((options) => {
|
|
|
733
734
|
if (transformed) base = `z.intersection(${base}, ${transformed})`;
|
|
734
735
|
}
|
|
735
736
|
return base;
|
|
736
|
-
}
|
|
737
|
+
},
|
|
738
|
+
...options.nodes
|
|
737
739
|
},
|
|
738
740
|
print(node) {
|
|
739
741
|
const { keysToOmit } = this.options;
|
|
@@ -752,58 +754,19 @@ const printerZodMini = definePrinter((options) => {
|
|
|
752
754
|
};
|
|
753
755
|
});
|
|
754
756
|
//#endregion
|
|
755
|
-
//#region src/components/ZodMini.tsx
|
|
756
|
-
function ZodMini({ name, node, guidType, wrapOutput, inferTypeName, resolver, keysToOmit }) {
|
|
757
|
-
const output = printerZodMini({
|
|
758
|
-
guidType,
|
|
759
|
-
wrapOutput,
|
|
760
|
-
resolver,
|
|
761
|
-
schemaName: name,
|
|
762
|
-
keysToOmit
|
|
763
|
-
}).print(node);
|
|
764
|
-
if (!output) return;
|
|
765
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, {
|
|
766
|
-
name,
|
|
767
|
-
isExportable: true,
|
|
768
|
-
isIndexable: true,
|
|
769
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
770
|
-
export: true,
|
|
771
|
-
name,
|
|
772
|
-
children: output
|
|
773
|
-
})
|
|
774
|
-
}), inferTypeName && /* @__PURE__ */ jsx(File.Source, {
|
|
775
|
-
name: inferTypeName,
|
|
776
|
-
isExportable: true,
|
|
777
|
-
isIndexable: true,
|
|
778
|
-
isTypeOnly: true,
|
|
779
|
-
children: /* @__PURE__ */ jsx(Type, {
|
|
780
|
-
export: true,
|
|
781
|
-
name: inferTypeName,
|
|
782
|
-
children: `z.infer<typeof ${name}>`
|
|
783
|
-
})
|
|
784
|
-
})] });
|
|
785
|
-
}
|
|
786
|
-
//#endregion
|
|
787
|
-
//#region src/constants.ts
|
|
788
|
-
/**
|
|
789
|
-
* Import paths that use a namespace import (`import * as z from '...'`).
|
|
790
|
-
* All other import paths use a named import (`import { z } from '...'`).
|
|
791
|
-
*/
|
|
792
|
-
const ZOD_NAMESPACE_IMPORTS = new Set(["zod", "zod/mini"]);
|
|
793
|
-
//#endregion
|
|
794
757
|
//#region src/generators/zodGenerator.tsx
|
|
795
758
|
const zodGenerator = defineGenerator({
|
|
796
759
|
name: "zod",
|
|
797
760
|
type: "react",
|
|
798
|
-
Schema({ node, adapter, options, config, resolver }) {
|
|
799
|
-
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group,
|
|
800
|
-
const transformedNode = transform(node,
|
|
761
|
+
Schema({ node, adapter, options, config, resolver, plugin }) {
|
|
762
|
+
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, printer } = options;
|
|
763
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
|
|
801
764
|
if (!transformedNode.name) return;
|
|
802
765
|
const root = path.resolve(config.root, config.output.path);
|
|
803
766
|
const mode = getMode(path.resolve(root, output.path));
|
|
804
767
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
805
768
|
const imports = adapter.getImports(transformedNode, (schemaName) => ({
|
|
806
|
-
name: resolver.
|
|
769
|
+
name: resolver.resolveSchemaName(schemaName),
|
|
807
770
|
path: resolver.resolveFile({
|
|
808
771
|
name: schemaName,
|
|
809
772
|
extname: ".ts"
|
|
@@ -813,9 +776,8 @@ const zodGenerator = defineGenerator({
|
|
|
813
776
|
group
|
|
814
777
|
}).path
|
|
815
778
|
}));
|
|
816
|
-
const inferTypeName = inferred ? resolver.resolveInferName(resolver.resolveName(transformedNode.name)) : void 0;
|
|
817
779
|
const meta = {
|
|
818
|
-
name: resolver.
|
|
780
|
+
name: resolver.resolveSchemaName(transformedNode.name),
|
|
819
781
|
file: resolver.resolveFile({
|
|
820
782
|
name: transformedNode.name,
|
|
821
783
|
extname: ".ts"
|
|
@@ -825,6 +787,21 @@ const zodGenerator = defineGenerator({
|
|
|
825
787
|
group
|
|
826
788
|
})
|
|
827
789
|
};
|
|
790
|
+
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(transformedNode.name) : void 0;
|
|
791
|
+
const schemaPrinter = mini ? printerZodMini({
|
|
792
|
+
guidType,
|
|
793
|
+
wrapOutput,
|
|
794
|
+
resolver,
|
|
795
|
+
schemaName: meta.name,
|
|
796
|
+
nodes: printer?.nodes
|
|
797
|
+
}) : printerZod({
|
|
798
|
+
coercion,
|
|
799
|
+
guidType,
|
|
800
|
+
wrapOutput,
|
|
801
|
+
resolver,
|
|
802
|
+
schemaName: meta.name,
|
|
803
|
+
nodes: printer?.nodes
|
|
804
|
+
});
|
|
828
805
|
return /* @__PURE__ */ jsxs(File, {
|
|
829
806
|
baseName: meta.file.baseName,
|
|
830
807
|
path: meta.file.path,
|
|
@@ -848,28 +825,18 @@ const zodGenerator = defineGenerator({
|
|
|
848
825
|
path: imp.path,
|
|
849
826
|
name: imp.name
|
|
850
827
|
}, [transformedNode.name, imp.path].join("-"))),
|
|
851
|
-
|
|
828
|
+
/* @__PURE__ */ jsx(Zod, {
|
|
852
829
|
name: meta.name,
|
|
853
830
|
node: transformedNode,
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
inferTypeName,
|
|
857
|
-
resolver
|
|
858
|
-
}) : /* @__PURE__ */ jsx(Zod, {
|
|
859
|
-
name: meta.name,
|
|
860
|
-
node: transformedNode,
|
|
861
|
-
coercion,
|
|
862
|
-
guidType,
|
|
863
|
-
wrapOutput,
|
|
864
|
-
inferTypeName,
|
|
865
|
-
resolver
|
|
831
|
+
printer: schemaPrinter,
|
|
832
|
+
inferTypeName
|
|
866
833
|
})
|
|
867
834
|
]
|
|
868
835
|
});
|
|
869
836
|
},
|
|
870
|
-
Operation({ node, adapter, options, config, resolver }) {
|
|
871
|
-
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing,
|
|
872
|
-
const transformedNode = transform(node,
|
|
837
|
+
Operation({ node, adapter, options, config, resolver, plugin }) {
|
|
838
|
+
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing, printer } = options;
|
|
839
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
|
|
873
840
|
const root = path.resolve(config.root, config.output.path);
|
|
874
841
|
const mode = getMode(path.resolve(root, output.path));
|
|
875
842
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -886,9 +853,9 @@ const zodGenerator = defineGenerator({
|
|
|
886
853
|
}) };
|
|
887
854
|
function renderSchemaEntry({ schema, name, keysToOmit }) {
|
|
888
855
|
if (!schema) return null;
|
|
889
|
-
const inferTypeName = inferred ? resolver.
|
|
856
|
+
const inferTypeName = inferred ? resolver.resolveTypeName(name) : void 0;
|
|
890
857
|
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
891
|
-
name: resolver.
|
|
858
|
+
name: resolver.resolveSchemaName(schemaName),
|
|
892
859
|
path: resolver.resolveFile({
|
|
893
860
|
name: schemaName,
|
|
894
861
|
extname: ".ts"
|
|
@@ -898,6 +865,22 @@ const zodGenerator = defineGenerator({
|
|
|
898
865
|
group
|
|
899
866
|
}).path
|
|
900
867
|
}));
|
|
868
|
+
const schemaPrinter = mini ? printerZodMini({
|
|
869
|
+
guidType,
|
|
870
|
+
wrapOutput,
|
|
871
|
+
resolver,
|
|
872
|
+
schemaName: name,
|
|
873
|
+
keysToOmit,
|
|
874
|
+
nodes: printer?.nodes
|
|
875
|
+
}) : printerZod({
|
|
876
|
+
coercion,
|
|
877
|
+
guidType,
|
|
878
|
+
wrapOutput,
|
|
879
|
+
resolver,
|
|
880
|
+
schemaName: name,
|
|
881
|
+
keysToOmit,
|
|
882
|
+
nodes: printer?.nodes
|
|
883
|
+
});
|
|
901
884
|
return /* @__PURE__ */ jsxs(Fragment, { children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ jsx(File.Import, {
|
|
902
885
|
root: meta.file.path,
|
|
903
886
|
path: imp.path,
|
|
@@ -906,23 +889,11 @@ const zodGenerator = defineGenerator({
|
|
|
906
889
|
name,
|
|
907
890
|
imp.path,
|
|
908
891
|
imp.name
|
|
909
|
-
].join("-"))),
|
|
910
|
-
name,
|
|
911
|
-
node: schema,
|
|
912
|
-
guidType,
|
|
913
|
-
wrapOutput,
|
|
914
|
-
inferTypeName,
|
|
915
|
-
resolver,
|
|
916
|
-
keysToOmit
|
|
917
|
-
}) : /* @__PURE__ */ jsx(Zod, {
|
|
892
|
+
].join("-"))), /* @__PURE__ */ jsx(Zod, {
|
|
918
893
|
name,
|
|
919
894
|
node: schema,
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
wrapOutput,
|
|
923
|
-
inferTypeName,
|
|
924
|
-
resolver,
|
|
925
|
-
keysToOmit
|
|
895
|
+
printer: schemaPrinter,
|
|
896
|
+
inferTypeName
|
|
926
897
|
})] });
|
|
927
898
|
}
|
|
928
899
|
const paramSchemas = params.map((param) => renderSchemaEntry({
|
|
@@ -966,8 +937,8 @@ const zodGenerator = defineGenerator({
|
|
|
966
937
|
]
|
|
967
938
|
});
|
|
968
939
|
},
|
|
969
|
-
Operations({ nodes, adapter, options, config, resolver }) {
|
|
970
|
-
const { output, importPath, group, operations, paramsCasing
|
|
940
|
+
Operations({ nodes, adapter, options, config, resolver, plugin }) {
|
|
941
|
+
const { output, importPath, group, operations, paramsCasing } = options;
|
|
971
942
|
if (!operations) return;
|
|
972
943
|
const root = path.resolve(config.root, config.output.path);
|
|
973
944
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -980,7 +951,7 @@ const zodGenerator = defineGenerator({
|
|
|
980
951
|
group
|
|
981
952
|
}) };
|
|
982
953
|
const transformedOperations = nodes.map((node) => {
|
|
983
|
-
const transformedNode = transform(node,
|
|
954
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
|
|
984
955
|
return {
|
|
985
956
|
node: transformedNode,
|
|
986
957
|
data: buildSchemaNames(transformedNode, {
|
|
@@ -1180,15 +1151,15 @@ function buildLegacySchemaNames(node, params, resolver) {
|
|
|
1180
1151
|
const zodGeneratorLegacy = defineGenerator({
|
|
1181
1152
|
name: "zod-legacy",
|
|
1182
1153
|
type: "react",
|
|
1183
|
-
Schema({ node, adapter, options, config, resolver }) {
|
|
1184
|
-
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group,
|
|
1185
|
-
const transformedNode = transform(node,
|
|
1154
|
+
Schema({ node, adapter, options, config, resolver, plugin }) {
|
|
1155
|
+
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, printer } = options;
|
|
1156
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
|
|
1186
1157
|
if (!transformedNode.name) return;
|
|
1187
1158
|
const root = path.resolve(config.root, config.output.path);
|
|
1188
1159
|
const mode = getMode(path.resolve(root, output.path));
|
|
1189
1160
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
1190
1161
|
const imports = adapter.getImports(transformedNode, (schemaName) => ({
|
|
1191
|
-
name: resolver.
|
|
1162
|
+
name: resolver.resolveSchemaName(schemaName),
|
|
1192
1163
|
path: resolver.resolveFile({
|
|
1193
1164
|
name: schemaName,
|
|
1194
1165
|
extname: ".ts"
|
|
@@ -1198,9 +1169,9 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1198
1169
|
group
|
|
1199
1170
|
}).path
|
|
1200
1171
|
}));
|
|
1201
|
-
const inferTypeName = inferred ? resolver.
|
|
1172
|
+
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(transformedNode.name) : void 0;
|
|
1202
1173
|
const meta = {
|
|
1203
|
-
name: resolver.
|
|
1174
|
+
name: resolver.resolveSchemaName(transformedNode.name),
|
|
1204
1175
|
file: resolver.resolveFile({
|
|
1205
1176
|
name: transformedNode.name,
|
|
1206
1177
|
extname: ".ts"
|
|
@@ -1210,6 +1181,20 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1210
1181
|
group
|
|
1211
1182
|
})
|
|
1212
1183
|
};
|
|
1184
|
+
const schemaPrinter = mini ? printerZodMini({
|
|
1185
|
+
guidType,
|
|
1186
|
+
wrapOutput,
|
|
1187
|
+
resolver,
|
|
1188
|
+
schemaName: meta.name,
|
|
1189
|
+
nodes: printer?.nodes
|
|
1190
|
+
}) : printerZod({
|
|
1191
|
+
coercion,
|
|
1192
|
+
guidType,
|
|
1193
|
+
wrapOutput,
|
|
1194
|
+
resolver,
|
|
1195
|
+
schemaName: meta.name,
|
|
1196
|
+
nodes: printer?.nodes
|
|
1197
|
+
});
|
|
1213
1198
|
return /* @__PURE__ */ jsxs(File, {
|
|
1214
1199
|
baseName: meta.file.baseName,
|
|
1215
1200
|
path: meta.file.path,
|
|
@@ -1233,28 +1218,18 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1233
1218
|
path: imp.path,
|
|
1234
1219
|
name: imp.name
|
|
1235
1220
|
}, [transformedNode.name, imp.path].join("-"))),
|
|
1236
|
-
|
|
1221
|
+
/* @__PURE__ */ jsx(Zod, {
|
|
1237
1222
|
name: meta.name,
|
|
1238
1223
|
node: transformedNode,
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
inferTypeName,
|
|
1242
|
-
resolver
|
|
1243
|
-
}) : /* @__PURE__ */ jsx(Zod, {
|
|
1244
|
-
name: meta.name,
|
|
1245
|
-
node: transformedNode,
|
|
1246
|
-
coercion,
|
|
1247
|
-
guidType,
|
|
1248
|
-
wrapOutput,
|
|
1249
|
-
inferTypeName,
|
|
1250
|
-
resolver
|
|
1224
|
+
printer: schemaPrinter,
|
|
1225
|
+
inferTypeName
|
|
1251
1226
|
})
|
|
1252
1227
|
]
|
|
1253
1228
|
});
|
|
1254
1229
|
},
|
|
1255
|
-
Operation({ node, adapter, options, config, resolver }) {
|
|
1256
|
-
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing,
|
|
1257
|
-
const transformedNode = transform(node,
|
|
1230
|
+
Operation({ node, adapter, options, config, resolver, plugin }) {
|
|
1231
|
+
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing, printer } = options;
|
|
1232
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
|
|
1258
1233
|
const root = path.resolve(config.root, config.output.path);
|
|
1259
1234
|
const mode = getMode(path.resolve(root, output.path));
|
|
1260
1235
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -1271,9 +1246,9 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1271
1246
|
}) };
|
|
1272
1247
|
function renderSchemaEntry({ schema, name, keysToOmit }) {
|
|
1273
1248
|
if (!schema) return null;
|
|
1274
|
-
const inferTypeName = inferred ? resolver.
|
|
1249
|
+
const inferTypeName = inferred ? resolver.resolveTypeName(name) : void 0;
|
|
1275
1250
|
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
1276
|
-
name: resolver.
|
|
1251
|
+
name: resolver.resolveSchemaName(schemaName),
|
|
1277
1252
|
path: resolver.resolveFile({
|
|
1278
1253
|
name: schemaName,
|
|
1279
1254
|
extname: ".ts"
|
|
@@ -1283,6 +1258,22 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1283
1258
|
group
|
|
1284
1259
|
}).path
|
|
1285
1260
|
}));
|
|
1261
|
+
const schemaPrinter = mini ? printerZodMini({
|
|
1262
|
+
guidType,
|
|
1263
|
+
wrapOutput,
|
|
1264
|
+
resolver,
|
|
1265
|
+
schemaName: name,
|
|
1266
|
+
keysToOmit,
|
|
1267
|
+
nodes: printer?.nodes
|
|
1268
|
+
}) : printerZod({
|
|
1269
|
+
coercion,
|
|
1270
|
+
guidType,
|
|
1271
|
+
wrapOutput,
|
|
1272
|
+
resolver,
|
|
1273
|
+
schemaName: name,
|
|
1274
|
+
keysToOmit,
|
|
1275
|
+
nodes: printer?.nodes
|
|
1276
|
+
});
|
|
1286
1277
|
return /* @__PURE__ */ jsxs(Fragment, { children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ jsx(File.Import, {
|
|
1287
1278
|
root: meta.file.path,
|
|
1288
1279
|
path: imp.path,
|
|
@@ -1291,23 +1282,11 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1291
1282
|
name,
|
|
1292
1283
|
imp.path,
|
|
1293
1284
|
imp.name
|
|
1294
|
-
].join("-"))),
|
|
1285
|
+
].join("-"))), /* @__PURE__ */ jsx(Zod, {
|
|
1295
1286
|
name,
|
|
1296
1287
|
node: schema,
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
inferTypeName,
|
|
1300
|
-
resolver,
|
|
1301
|
-
keysToOmit
|
|
1302
|
-
}) : /* @__PURE__ */ jsx(Zod, {
|
|
1303
|
-
name,
|
|
1304
|
-
node: schema,
|
|
1305
|
-
coercion,
|
|
1306
|
-
guidType,
|
|
1307
|
-
wrapOutput,
|
|
1308
|
-
inferTypeName,
|
|
1309
|
-
resolver,
|
|
1310
|
-
keysToOmit
|
|
1288
|
+
printer: schemaPrinter,
|
|
1289
|
+
inferTypeName
|
|
1311
1290
|
})] });
|
|
1312
1291
|
}
|
|
1313
1292
|
const pathParams = params.filter((p) => p.in === "path");
|
|
@@ -1389,8 +1368,8 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1389
1368
|
]
|
|
1390
1369
|
});
|
|
1391
1370
|
},
|
|
1392
|
-
Operations({ nodes, adapter, options, config, resolver }) {
|
|
1393
|
-
const { output, importPath, group, operations, paramsCasing
|
|
1371
|
+
Operations({ nodes, adapter, options, config, resolver, plugin }) {
|
|
1372
|
+
const { output, importPath, group, operations, paramsCasing } = options;
|
|
1394
1373
|
if (!operations) return;
|
|
1395
1374
|
const root = path.resolve(config.root, config.output.path);
|
|
1396
1375
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -1403,7 +1382,7 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1403
1382
|
group
|
|
1404
1383
|
}) };
|
|
1405
1384
|
const transformedOperations = nodes.map((node) => {
|
|
1406
|
-
const transformedNode = transform(node,
|
|
1385
|
+
const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
|
|
1407
1386
|
return {
|
|
1408
1387
|
node: transformedNode,
|
|
1409
1388
|
data: buildLegacySchemaNames(transformedNode, caseParams(transformedNode.parameters, paramsCasing), resolver)
|
|
@@ -1461,14 +1440,6 @@ const zodGeneratorLegacy = defineGenerator({
|
|
|
1461
1440
|
});
|
|
1462
1441
|
//#endregion
|
|
1463
1442
|
//#region src/resolvers/resolverZod.ts
|
|
1464
|
-
function toSchemaName(name, type) {
|
|
1465
|
-
const resolved = camelCase(name, {
|
|
1466
|
-
suffix: type ? "schema" : void 0,
|
|
1467
|
-
isFile: type === "file"
|
|
1468
|
-
});
|
|
1469
|
-
if (type === "type") return pascalCase(resolved);
|
|
1470
|
-
return resolved;
|
|
1471
|
-
}
|
|
1472
1443
|
/**
|
|
1473
1444
|
* Default resolver for `@kubb/plugin-zod`.
|
|
1474
1445
|
*
|
|
@@ -1486,31 +1457,37 @@ const resolverZod = defineResolver(() => {
|
|
|
1486
1457
|
name: "default",
|
|
1487
1458
|
pluginName: "plugin-zod",
|
|
1488
1459
|
default(name, type) {
|
|
1489
|
-
return
|
|
1460
|
+
return camelCase(name, {
|
|
1461
|
+
isFile: type === "file",
|
|
1462
|
+
suffix: type ? "schema" : void 0
|
|
1463
|
+
});
|
|
1464
|
+
},
|
|
1465
|
+
resolveSchemaName(name) {
|
|
1466
|
+
return camelCase(name, { suffix: "schema" });
|
|
1490
1467
|
},
|
|
1491
|
-
|
|
1492
|
-
return
|
|
1468
|
+
resolveSchemaTypeName(name) {
|
|
1469
|
+
return pascalCase(name, { suffix: "schema" });
|
|
1493
1470
|
},
|
|
1494
|
-
|
|
1471
|
+
resolveTypeName(name) {
|
|
1495
1472
|
return pascalCase(name);
|
|
1496
1473
|
},
|
|
1497
1474
|
resolvePathName(name, type) {
|
|
1498
1475
|
return this.default(name, type);
|
|
1499
1476
|
},
|
|
1500
1477
|
resolveParamName(node, param) {
|
|
1501
|
-
return this.
|
|
1478
|
+
return this.resolveSchemaName(`${node.operationId} ${param.in} ${param.name}`);
|
|
1502
1479
|
},
|
|
1503
1480
|
resolveResponseStatusName(node, statusCode) {
|
|
1504
|
-
return this.
|
|
1481
|
+
return this.resolveSchemaName(`${node.operationId} Status ${statusCode}`);
|
|
1505
1482
|
},
|
|
1506
1483
|
resolveDataName(node) {
|
|
1507
|
-
return this.
|
|
1484
|
+
return this.resolveSchemaName(`${node.operationId} Data`);
|
|
1508
1485
|
},
|
|
1509
1486
|
resolveResponsesName(node) {
|
|
1510
|
-
return this.
|
|
1487
|
+
return this.resolveSchemaName(`${node.operationId} Responses`);
|
|
1511
1488
|
},
|
|
1512
1489
|
resolveResponseName(node) {
|
|
1513
|
-
return this.
|
|
1490
|
+
return this.resolveSchemaName(`${node.operationId} Response`);
|
|
1514
1491
|
},
|
|
1515
1492
|
resolvePathParamsName(node, param) {
|
|
1516
1493
|
return this.resolveParamName(node, param);
|
|
@@ -1553,29 +1530,29 @@ const resolverZodLegacy = defineResolver(() => {
|
|
|
1553
1530
|
...resolverZod,
|
|
1554
1531
|
pluginName: "plugin-zod",
|
|
1555
1532
|
resolveResponseStatusName(node, statusCode) {
|
|
1556
|
-
if (statusCode === "default") return this.
|
|
1557
|
-
return this.
|
|
1533
|
+
if (statusCode === "default") return this.resolveSchemaName(`${node.operationId} Error`);
|
|
1534
|
+
return this.resolveSchemaName(`${node.operationId} ${statusCode}`);
|
|
1558
1535
|
},
|
|
1559
1536
|
resolveDataName(node) {
|
|
1560
1537
|
const suffix = node.method === "GET" ? "QueryRequest" : "MutationRequest";
|
|
1561
|
-
return this.
|
|
1538
|
+
return this.resolveSchemaName(`${node.operationId} ${suffix}`);
|
|
1562
1539
|
},
|
|
1563
1540
|
resolveResponsesName(node) {
|
|
1564
1541
|
const suffix = node.method === "GET" ? "Query" : "Mutation";
|
|
1565
|
-
return this.
|
|
1542
|
+
return this.resolveSchemaName(`${node.operationId} ${suffix}`);
|
|
1566
1543
|
},
|
|
1567
1544
|
resolveResponseName(node) {
|
|
1568
1545
|
const suffix = node.method === "GET" ? "QueryResponse" : "MutationResponse";
|
|
1569
|
-
return this.
|
|
1546
|
+
return this.resolveSchemaName(`${node.operationId} ${suffix}`);
|
|
1570
1547
|
},
|
|
1571
1548
|
resolvePathParamsName(node, _param) {
|
|
1572
|
-
return this.
|
|
1549
|
+
return this.resolveSchemaName(`${node.operationId} PathParams`);
|
|
1573
1550
|
},
|
|
1574
1551
|
resolveQueryParamsName(node, _param) {
|
|
1575
|
-
return this.
|
|
1552
|
+
return this.resolveSchemaName(`${node.operationId} QueryParams`);
|
|
1576
1553
|
},
|
|
1577
1554
|
resolveHeaderParamsName(node, _param) {
|
|
1578
|
-
return this.
|
|
1555
|
+
return this.resolveSchemaName(`${node.operationId} HeaderParams`);
|
|
1579
1556
|
}
|
|
1580
1557
|
};
|
|
1581
1558
|
});
|
|
@@ -1586,17 +1563,21 @@ const resolverZodLegacy = defineResolver(() => {
|
|
|
1586
1563
|
*
|
|
1587
1564
|
* - `default` — uses `resolverZod` and `zodGenerator` (current naming conventions).
|
|
1588
1565
|
* - `kubbV4` — uses `resolverZodLegacy` and `zodGeneratorLegacy` (Kubb v4 naming conventions).
|
|
1566
|
+
*
|
|
1567
|
+
* Note: `printerZodMini` is selected at runtime by the generator when `mini: true` is set.
|
|
1589
1568
|
*/
|
|
1590
1569
|
const presets = definePresets({
|
|
1591
1570
|
default: {
|
|
1592
1571
|
name: "default",
|
|
1593
|
-
|
|
1594
|
-
generators: [zodGenerator]
|
|
1572
|
+
resolver: resolverZod,
|
|
1573
|
+
generators: [zodGenerator],
|
|
1574
|
+
printer: printerZod
|
|
1595
1575
|
},
|
|
1596
1576
|
kubbV4: {
|
|
1597
1577
|
name: "kubbV4",
|
|
1598
|
-
|
|
1599
|
-
generators: [zodGeneratorLegacy]
|
|
1578
|
+
resolver: resolverZodLegacy,
|
|
1579
|
+
generators: [zodGeneratorLegacy],
|
|
1580
|
+
printer: printerZod
|
|
1600
1581
|
}
|
|
1601
1582
|
});
|
|
1602
1583
|
//#endregion
|
|
@@ -1625,12 +1606,12 @@ const pluginZod = createPlugin((options) => {
|
|
|
1625
1606
|
const { output = {
|
|
1626
1607
|
path: "zod",
|
|
1627
1608
|
barrelType: "named"
|
|
1628
|
-
}, group, exclude = [], include, override = [], dateType = "string", typed = false, operations = false, mini = false, guidType = "uuid", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, wrapOutput = void 0, paramsCasing, compatibilityPreset = "default",
|
|
1609
|
+
}, group, exclude = [], include, override = [], dateType = "string", typed = false, operations = false, mini = false, guidType = "uuid", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, wrapOutput = void 0, paramsCasing, printer, compatibilityPreset = "default", resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
|
|
1629
1610
|
const preset = getPreset({
|
|
1630
1611
|
preset: compatibilityPreset,
|
|
1631
1612
|
presets,
|
|
1632
|
-
|
|
1633
|
-
|
|
1613
|
+
resolver: userResolver,
|
|
1614
|
+
transformer: userTransformer,
|
|
1634
1615
|
generators: userGenerators
|
|
1635
1616
|
});
|
|
1636
1617
|
let resolveNameWarning = false;
|
|
@@ -1640,6 +1621,9 @@ const pluginZod = createPlugin((options) => {
|
|
|
1640
1621
|
get resolver() {
|
|
1641
1622
|
return preset.resolver;
|
|
1642
1623
|
},
|
|
1624
|
+
get transformer() {
|
|
1625
|
+
return preset.transformer;
|
|
1626
|
+
},
|
|
1643
1627
|
get options() {
|
|
1644
1628
|
return {
|
|
1645
1629
|
output,
|
|
@@ -1660,7 +1644,7 @@ const pluginZod = createPlugin((options) => {
|
|
|
1660
1644
|
mini,
|
|
1661
1645
|
wrapOutput,
|
|
1662
1646
|
paramsCasing,
|
|
1663
|
-
|
|
1647
|
+
printer
|
|
1664
1648
|
};
|
|
1665
1649
|
},
|
|
1666
1650
|
resolvePath(baseName, pathMode, options) {
|