@kubb/plugin-zod 5.0.0-beta.10 → 5.0.0-beta.15
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 +73 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +74 -32
- package/dist/index.js.map +1 -1
- package/extension.yaml +21 -1
- package/package.json +5 -5
- package/src/generators/zodGenerator.tsx +52 -19
- package/src/printers/printerZod.ts +4 -1
- package/src/printers/printerZodMini.ts +4 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
2
|
import { ast, defineGenerator, definePlugin, defineResolver } from "@kubb/core";
|
|
3
|
-
import { Const, File, Type,
|
|
3
|
+
import { Const, File, Type, jsxRendererSync } from "@kubb/renderer-jsx";
|
|
4
4
|
import { Fragment, jsx, jsxs } from "@kubb/renderer-jsx/jsx-runtime";
|
|
5
5
|
//#region ../../internals/utils/src/casing.ts
|
|
6
6
|
/**
|
|
@@ -459,9 +459,10 @@ const printerZod = ast.definePrinter((options) => {
|
|
|
459
459
|
const isOptional = schema.optional;
|
|
460
460
|
const isNullish = schema.nullish;
|
|
461
461
|
const hasSelfRef = this.options.cyclicSchemas != null && ast.containsCircularRef(schema, { circularSchemas: this.options.cyclicSchemas });
|
|
462
|
+
const savedCyclicSchemas = this.options.cyclicSchemas;
|
|
462
463
|
if (hasSelfRef) this.options.cyclicSchemas = void 0;
|
|
463
464
|
const baseOutput = this.transform(schema) ?? this.transform(ast.createSchema({ type: "unknown" }));
|
|
464
|
-
if (hasSelfRef) this.options.cyclicSchemas =
|
|
465
|
+
if (hasSelfRef) this.options.cyclicSchemas = savedCyclicSchemas;
|
|
465
466
|
const wrappedOutput = this.options.wrapOutput ? this.options.wrapOutput({
|
|
466
467
|
output: baseOutput,
|
|
467
468
|
schema
|
|
@@ -648,9 +649,10 @@ const printerZodMini = ast.definePrinter((options) => {
|
|
|
648
649
|
const isOptional = schema.optional;
|
|
649
650
|
const isNullish = schema.nullish;
|
|
650
651
|
const hasSelfRef = this.options.cyclicSchemas != null && ast.containsCircularRef(schema, { circularSchemas: this.options.cyclicSchemas });
|
|
652
|
+
const savedCyclicSchemas = this.options.cyclicSchemas;
|
|
651
653
|
if (hasSelfRef) this.options.cyclicSchemas = void 0;
|
|
652
654
|
const baseOutput = this.transform(schema) ?? this.transform(ast.createSchema({ type: "unknown" }));
|
|
653
|
-
if (hasSelfRef) this.options.cyclicSchemas =
|
|
655
|
+
if (hasSelfRef) this.options.cyclicSchemas = savedCyclicSchemas;
|
|
654
656
|
const value = applyMiniModifiers({
|
|
655
657
|
value: this.options.wrapOutput ? this.options.wrapOutput({
|
|
656
658
|
output: baseOutput,
|
|
@@ -740,11 +742,13 @@ const printerZodMini = ast.definePrinter((options) => {
|
|
|
740
742
|
});
|
|
741
743
|
//#endregion
|
|
742
744
|
//#region src/generators/zodGenerator.tsx
|
|
745
|
+
const zodPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
746
|
+
const zodMiniPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
743
747
|
const zodGenerator = defineGenerator({
|
|
744
748
|
name: "zod",
|
|
745
|
-
renderer:
|
|
749
|
+
renderer: jsxRendererSync,
|
|
746
750
|
schema(node, ctx) {
|
|
747
|
-
const { adapter, config, resolver, root } = ctx;
|
|
751
|
+
const { adapter, config, resolver, root, inputNode } = ctx;
|
|
748
752
|
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, printer } = ctx.options;
|
|
749
753
|
const dateType = adapter.options.dateType;
|
|
750
754
|
if (!node.name) return;
|
|
@@ -773,31 +777,53 @@ const zodGenerator = defineGenerator({
|
|
|
773
777
|
})
|
|
774
778
|
};
|
|
775
779
|
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(node.name) : void 0;
|
|
776
|
-
const cyclicSchemas =
|
|
777
|
-
const schemaPrinter = mini ?
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
780
|
+
const cyclicSchemas = ast.findCircularSchemas(inputNode.schemas);
|
|
781
|
+
const schemaPrinter = mini ? getCachedMiniPrinter() : getCachedStdPrinter();
|
|
782
|
+
function getCachedStdPrinter() {
|
|
783
|
+
const cached = zodPrinterCache.get(resolver);
|
|
784
|
+
if (cached && cached.coercion === coercion && cached.guidType === guidType && cached.dateType === dateType) return cached.printer;
|
|
785
|
+
const p = printerZod({
|
|
786
|
+
coercion,
|
|
787
|
+
guidType,
|
|
788
|
+
dateType,
|
|
789
|
+
wrapOutput,
|
|
790
|
+
resolver,
|
|
791
|
+
cyclicSchemas,
|
|
792
|
+
nodes: printer?.nodes
|
|
793
|
+
});
|
|
794
|
+
zodPrinterCache.set(resolver, {
|
|
795
|
+
printer: p,
|
|
796
|
+
coercion,
|
|
797
|
+
guidType,
|
|
798
|
+
dateType
|
|
799
|
+
});
|
|
800
|
+
return p;
|
|
801
|
+
}
|
|
802
|
+
function getCachedMiniPrinter() {
|
|
803
|
+
const cached = zodMiniPrinterCache.get(resolver);
|
|
804
|
+
if (cached && cached.guidType === guidType) return cached.printer;
|
|
805
|
+
const p = printerZodMini({
|
|
806
|
+
guidType,
|
|
807
|
+
wrapOutput,
|
|
808
|
+
resolver,
|
|
809
|
+
cyclicSchemas,
|
|
810
|
+
nodes: printer?.nodes
|
|
811
|
+
});
|
|
812
|
+
zodMiniPrinterCache.set(resolver, {
|
|
813
|
+
printer: p,
|
|
814
|
+
guidType
|
|
815
|
+
});
|
|
816
|
+
return p;
|
|
817
|
+
}
|
|
792
818
|
return /* @__PURE__ */ jsxs(File, {
|
|
793
819
|
baseName: meta.file.baseName,
|
|
794
820
|
path: meta.file.path,
|
|
795
821
|
meta: meta.file.meta,
|
|
796
|
-
banner: resolver.resolveBanner(
|
|
822
|
+
banner: resolver.resolveBanner(inputNode, {
|
|
797
823
|
output,
|
|
798
824
|
config
|
|
799
825
|
}),
|
|
800
|
-
footer: resolver.resolveFooter(
|
|
826
|
+
footer: resolver.resolveFooter(inputNode, {
|
|
801
827
|
output,
|
|
802
828
|
config
|
|
803
829
|
}),
|
|
@@ -822,7 +848,7 @@ const zodGenerator = defineGenerator({
|
|
|
822
848
|
});
|
|
823
849
|
},
|
|
824
850
|
operation(node, ctx) {
|
|
825
|
-
const { adapter, config, resolver, root } = ctx;
|
|
851
|
+
const { adapter, config, resolver, root, inputNode } = ctx;
|
|
826
852
|
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing, printer } = ctx.options;
|
|
827
853
|
const dateType = adapter.options.dateType;
|
|
828
854
|
const mode = ctx.getMode(output);
|
|
@@ -838,7 +864,7 @@ const zodGenerator = defineGenerator({
|
|
|
838
864
|
output,
|
|
839
865
|
group
|
|
840
866
|
}) };
|
|
841
|
-
const cyclicSchemas =
|
|
867
|
+
const cyclicSchemas = ast.findCircularSchemas(inputNode.schemas);
|
|
842
868
|
function renderSchemaEntry({ schema, name, keysToOmit }) {
|
|
843
869
|
if (!schema) return null;
|
|
844
870
|
const inferTypeName = inferred ? resolver.resolveTypeName(name) : void 0;
|
|
@@ -853,14 +879,22 @@ const zodGenerator = defineGenerator({
|
|
|
853
879
|
group
|
|
854
880
|
}).path
|
|
855
881
|
}));
|
|
856
|
-
const
|
|
882
|
+
const cachedStd = zodPrinterCache.get(resolver);
|
|
883
|
+
const cachedMini = zodMiniPrinterCache.get(resolver);
|
|
884
|
+
const schemaPrinter = mini ? keysToOmit?.length ? printerZodMini({
|
|
857
885
|
guidType,
|
|
858
886
|
wrapOutput,
|
|
859
887
|
resolver,
|
|
860
888
|
keysToOmit,
|
|
861
889
|
cyclicSchemas,
|
|
862
890
|
nodes: printer?.nodes
|
|
863
|
-
}) :
|
|
891
|
+
}) : cachedMini?.guidType === guidType ? cachedMini.printer : printerZodMini({
|
|
892
|
+
guidType,
|
|
893
|
+
wrapOutput,
|
|
894
|
+
resolver,
|
|
895
|
+
cyclicSchemas,
|
|
896
|
+
nodes: printer?.nodes
|
|
897
|
+
}) : keysToOmit?.length ? printerZod({
|
|
864
898
|
coercion,
|
|
865
899
|
guidType,
|
|
866
900
|
dateType,
|
|
@@ -869,6 +903,14 @@ const zodGenerator = defineGenerator({
|
|
|
869
903
|
keysToOmit,
|
|
870
904
|
cyclicSchemas,
|
|
871
905
|
nodes: printer?.nodes
|
|
906
|
+
}) : cachedStd?.coercion === coercion && cachedStd?.guidType === guidType && cachedStd?.dateType === dateType ? cachedStd.printer : printerZod({
|
|
907
|
+
coercion,
|
|
908
|
+
guidType,
|
|
909
|
+
dateType,
|
|
910
|
+
wrapOutput,
|
|
911
|
+
resolver,
|
|
912
|
+
cyclicSchemas,
|
|
913
|
+
nodes: printer?.nodes
|
|
872
914
|
});
|
|
873
915
|
return /* @__PURE__ */ jsxs(Fragment, { children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ jsx(File.Import, {
|
|
874
916
|
root: meta.file.path,
|
|
@@ -925,11 +967,11 @@ const zodGenerator = defineGenerator({
|
|
|
925
967
|
baseName: meta.file.baseName,
|
|
926
968
|
path: meta.file.path,
|
|
927
969
|
meta: meta.file.meta,
|
|
928
|
-
banner: resolver.resolveBanner(
|
|
970
|
+
banner: resolver.resolveBanner(inputNode, {
|
|
929
971
|
output,
|
|
930
972
|
config
|
|
931
973
|
}),
|
|
932
|
-
footer: resolver.resolveFooter(
|
|
974
|
+
footer: resolver.resolveFooter(inputNode, {
|
|
933
975
|
output,
|
|
934
976
|
config
|
|
935
977
|
}),
|
|
@@ -947,7 +989,7 @@ const zodGenerator = defineGenerator({
|
|
|
947
989
|
});
|
|
948
990
|
},
|
|
949
991
|
operations(nodes, ctx) {
|
|
950
|
-
const {
|
|
992
|
+
const { config, resolver, root, inputNode } = ctx;
|
|
951
993
|
const { output, importPath, group, operations, paramsCasing } = ctx.options;
|
|
952
994
|
if (!operations) return;
|
|
953
995
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -994,11 +1036,11 @@ const zodGenerator = defineGenerator({
|
|
|
994
1036
|
baseName: meta.file.baseName,
|
|
995
1037
|
path: meta.file.path,
|
|
996
1038
|
meta: meta.file.meta,
|
|
997
|
-
banner: resolver.resolveBanner(
|
|
1039
|
+
banner: resolver.resolveBanner(inputNode, {
|
|
998
1040
|
output,
|
|
999
1041
|
config
|
|
1000
1042
|
}),
|
|
1001
|
-
footer: resolver.resolveFooter(
|
|
1043
|
+
footer: resolver.resolveFooter(inputNode, {
|
|
1002
1044
|
output,
|
|
1003
1045
|
config
|
|
1004
1046
|
}),
|