zodvex 0.7.1-beta.0 → 0.7.1-beta.1

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.
@@ -1,9 +1,13 @@
1
1
  /**
2
2
  * One-shot codegen. Discovers modules, generates files.
3
3
  */
4
- export declare function generate(convexDir?: string): Promise<void>;
4
+ export declare function generate(convexDir?: string, options?: {
5
+ mini?: boolean;
6
+ }): Promise<void>;
5
7
  /**
6
8
  * Watch mode. Runs generate() once, then watches for changes.
7
9
  */
8
- export declare function dev(convexDir?: string): Promise<void>;
10
+ export declare function dev(convexDir?: string, options?: {
11
+ mini?: boolean;
12
+ }): Promise<void>;
9
13
  //# sourceMappingURL=commands.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../src/cli/commands.ts"],"names":[],"mappings":"AAUA;;GAEG;AACH,wBAAsB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqChE;AAED;;GAEG;AACH,wBAAsB,GAAG,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAoC3D"}
1
+ {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../src/cli/commands.ts"],"names":[],"mappings":"AAUA;;GAEG;AACH,wBAAsB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAsC9F;AAED;;GAEG;AACH,wBAAsB,GAAG,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAoCzF"}
package/dist/cli/index.js CHANGED
@@ -555,14 +555,18 @@ async function discoverModules(convexDir2) {
555
555
  // src/codegen/zodToSource.ts
556
556
  function zodToSource(schema, ctx) {
557
557
  if (schema instanceof $ZodOptional) {
558
- return `${zodToSource(schema._zod.def.innerType, ctx)}.optional()`;
558
+ const inner = zodToSource(schema._zod.def.innerType, ctx);
559
+ return ctx?.mini ? `z.optional(${inner})` : `${inner}.optional()`;
559
560
  }
560
561
  if (schema instanceof $ZodNullable) {
561
- return `${zodToSource(schema._zod.def.innerType, ctx)}.nullable()`;
562
+ const inner = zodToSource(schema._zod.def.innerType, ctx);
563
+ return ctx?.mini ? `z.nullable(${inner})` : `${inner}.nullable()`;
562
564
  }
563
- if (schema instanceof $ZodString && schema.description?.startsWith("convexId:")) {
564
- const tableName = schema.description.slice("convexId:".length);
565
- return `zx.id("${tableName}")`;
565
+ if (schema instanceof $ZodString) {
566
+ const tableName = schema._tableName ?? (schema.description?.startsWith("convexId:") ? schema.description.slice("convexId:".length) : void 0);
567
+ if (tableName) {
568
+ return `zx.id("${tableName}")`;
569
+ }
566
570
  }
567
571
  if (schema instanceof $ZodCodec && schema._zod.def.in instanceof $ZodNumber && schema._zod.def.out instanceof $ZodCustom) {
568
572
  return "zx.date()";
@@ -640,28 +644,38 @@ ${exports$1}
640
644
  `;
641
645
  return { js: content, dts: content };
642
646
  }
643
- function tryUnwrapToIdentity(schema, identityMap) {
647
+ function tryUnwrapToIdentity(schema, identityMap, mini) {
644
648
  let current = schema;
645
- const suffixes = [];
649
+ const wrappers = [];
646
650
  const maxDepth = 5;
647
651
  for (let i = 0; i < maxDepth; i++) {
648
652
  if (current instanceof $ZodOptional) {
649
- suffixes.push(".optional()");
653
+ wrappers.push("optional");
650
654
  current = current._zod.def.innerType;
651
655
  } else if (current instanceof $ZodNullable) {
652
- suffixes.push(".nullable()");
656
+ wrappers.push("nullable");
653
657
  current = current._zod.def.innerType;
654
658
  } else {
655
659
  break;
656
660
  }
657
661
  const ref = identityMap.get(current);
658
662
  if (ref) {
659
- return { ref, suffix: suffixes.reverse().join("") };
663
+ const reversed = [...wrappers].reverse();
664
+ return {
665
+ ref,
666
+ wrapSource: (inner) => {
667
+ let result = inner;
668
+ for (const w of reversed) {
669
+ result = mini ? `z.${w}(${result})` : `${result}.${w}()`;
670
+ }
671
+ return result;
672
+ }
673
+ };
660
674
  }
661
675
  }
662
676
  return null;
663
677
  }
664
- function tryMatchPartial(schema, identityMap) {
678
+ function tryMatchPartial(schema, identityMap, mini) {
665
679
  if (!(schema instanceof $ZodObject)) return null;
666
680
  const candidateShape = schema._zod.def.shape;
667
681
  const candidateKeys = Object.keys(candidateShape).sort();
@@ -685,7 +699,10 @@ function tryMatchPartial(schema, identityMap) {
685
699
  }
686
700
  }
687
701
  if (allMatch) {
688
- return { ref, suffix: ".partial()" };
702
+ return {
703
+ ref,
704
+ wrapSource: (inner) => mini ? `z.partial(${inner})` : `${inner}.partial()`
705
+ };
689
706
  }
690
707
  }
691
708
  return null;
@@ -698,7 +715,7 @@ function deriveCodecVarName(modelExportName, accessPath) {
698
715
  const fieldPart = fields.map((f, i) => i === 0 ? f : f[0].toUpperCase() + f.slice(1)).join("");
699
716
  return `_${prefix}${fieldPart[0].toUpperCase() + fieldPart.slice(1)}`;
700
717
  }
701
- function generateApiFile(functions, models, codecs, modelCodecs, functionCodecs) {
718
+ function generateApiFile(functions, models, codecs, modelCodecs, functionCodecs, options) {
702
719
  const identityMap = /* @__PURE__ */ new Map();
703
720
  const neededModelImports = /* @__PURE__ */ new Set();
704
721
  for (const model of models) {
@@ -757,7 +774,8 @@ function generateApiFile(functions, models, codecs, modelCodecs, functionCodecs)
757
774
  const zodToSourceCtx = {
758
775
  codecMap,
759
776
  neededCodecImports: /* @__PURE__ */ new Map(),
760
- undiscoverableCodecs: []
777
+ undiscoverableCodecs: [],
778
+ mini: options?.mini
761
779
  };
762
780
  let needsZod = false;
763
781
  let needsZx = false;
@@ -769,15 +787,17 @@ function generateApiFile(functions, models, codecs, modelCodecs, functionCodecs)
769
787
  neededModelImports.add(ref.exportName);
770
788
  return `${ref.exportName}.schema.${ref.schemaKey}`;
771
789
  }
772
- const unwrapped = tryUnwrapToIdentity(s, identityMap);
790
+ const unwrapped = tryUnwrapToIdentity(s, identityMap, options?.mini);
773
791
  if (unwrapped) {
774
792
  neededModelImports.add(unwrapped.ref.exportName);
775
- return `${unwrapped.ref.exportName}.schema.${unwrapped.ref.schemaKey}${unwrapped.suffix}`;
793
+ const inner = `${unwrapped.ref.exportName}.schema.${unwrapped.ref.schemaKey}`;
794
+ return unwrapped.wrapSource(inner);
776
795
  }
777
- const partialMatch = tryMatchPartial(s, identityMap);
796
+ const partialMatch = tryMatchPartial(s, identityMap, options?.mini);
778
797
  if (partialMatch) {
779
798
  neededModelImports.add(partialMatch.ref.exportName);
780
- return `${partialMatch.ref.exportName}.schema.${partialMatch.ref.schemaKey}${partialMatch.suffix}`;
799
+ const inner = `${partialMatch.ref.exportName}.schema.${partialMatch.ref.schemaKey}`;
800
+ return partialMatch.wrapSource(inner);
781
801
  }
782
802
  const source = zodToSource(s, zodToSourceCtx);
783
803
  if (source.includes("z.")) needsZod = true;
@@ -802,12 +822,14 @@ function generateApiFile(functions, models, codecs, modelCodecs, functionCodecs)
802
822
  }
803
823
  }
804
824
  const imports = [];
805
- if (needsZod) imports.push("import { z } from 'zod'");
825
+ const zodImport = options?.mini ? "zod/mini" : "zod";
826
+ if (needsZod) imports.push(`import { z } from '${zodImport}'`);
827
+ const zodvexImport = options?.mini ? "zodvex/mini" : "zodvex/core";
806
828
  const coreImports = [];
807
829
  if (needsZx) coreImports.push("zx");
808
830
  if (usedModelCodecVars.length > 0) coreImports.push("extractCodec");
809
831
  if (coreImports.length > 0) {
810
- imports.push(`import { ${coreImports.join(", ")} } from 'zodvex/core'`);
832
+ imports.push(`import { ${coreImports.join(", ")} } from '${zodvexImport}'`);
811
833
  }
812
834
  for (const exportName of neededModelImports) {
813
835
  const model = models.find((m) => m.exportName === exportName);
@@ -833,7 +855,11 @@ ${importSection}${codecVarSection}export const zodvexRegistry = {
833
855
  ${entries},
834
856
  }
835
857
  `;
836
- const dts = `${HEADER}
858
+ const dts = options?.mini ? `${HEADER}
859
+ import type { $ZodType } from 'zod/v4/core'
860
+
861
+ export declare const zodvexRegistry: Record<string, { args: $ZodType; returns: $ZodType | undefined }>
862
+ ` : `${HEADER}
837
863
  import type { ZodTypeAny } from 'zod'
838
864
 
839
865
  export declare const zodvexRegistry: Record<string, { args: ZodTypeAny; returns: ZodTypeAny | undefined }>
@@ -860,12 +886,13 @@ export type ActionCtx = ZodvexActionCtx<DataModel>
860
886
  `;
861
887
  return { js, dts };
862
888
  }
863
- function generateClientFile() {
889
+ function generateClientFile(options) {
890
+ const zodvexCoreImport = options?.mini ? "zodvex/mini" : "zodvex/core";
864
891
  const jsImports = [
865
892
  "import { createZodvexHooks } from 'zodvex/react'",
866
893
  "import { createZodvexReactClient } from 'zodvex/react'",
867
894
  "import { createZodvexClient } from 'zodvex/client'",
868
- "import { createBoundaryHelpers } from 'zodvex/core'",
895
+ `import { createBoundaryHelpers } from '${zodvexCoreImport}'`,
869
896
  "import { zodvexRegistry } from './api.js'"
870
897
  ];
871
898
  const jsExports = [
@@ -888,7 +915,7 @@ ${jsExports.join("\n")}
888
915
  "import type { ZodvexHooks } from 'zodvex/react'",
889
916
  "import type { ZodvexClientOptions, ZodvexClient } from 'zodvex/client'",
890
917
  "import type { ZodvexReactClientOptions, ZodvexReactClient } from 'zodvex/react'",
891
- "import type { BoundaryHelpers } from 'zodvex/core'"
918
+ `import type { BoundaryHelpers } from '${zodvexCoreImport}'`
892
919
  ];
893
920
  const dtsDeclarations = [
894
921
  "export declare const useZodQuery: ZodvexHooks['useZodQuery']",
@@ -910,7 +937,7 @@ ${dtsDeclarations.join("\n")}
910
937
  }
911
938
 
912
939
  // src/cli/commands.ts
913
- async function generate(convexDir2) {
940
+ async function generate(convexDir2, options) {
914
941
  const resolved = resolveConvexDir(convexDir2);
915
942
  const zodvexDir = path3.join(resolved, "_zodvex");
916
943
  writeStubApi(zodvexDir);
@@ -921,9 +948,10 @@ async function generate(convexDir2) {
921
948
  result.models,
922
949
  result.codecs,
923
950
  result.modelCodecs,
924
- result.functionCodecs
951
+ result.functionCodecs,
952
+ { mini: options?.mini }
925
953
  );
926
- const clientContent = generateClientFile();
954
+ const clientContent = generateClientFile({ mini: options?.mini });
927
955
  const serverContent = generateServerFile();
928
956
  fs2.mkdirSync(zodvexDir, { recursive: true });
929
957
  writeIfChanged(path3.join(zodvexDir, "schema.js"), schemaContent.js);
@@ -939,10 +967,10 @@ async function generate(convexDir2) {
939
967
  `[zodvex] Generated ${result.models.length} model(s), ${result.functions.length} function(s), ${totalCodecs} codec(s)`
940
968
  );
941
969
  }
942
- async function dev(convexDir2) {
970
+ async function dev(convexDir2, options) {
943
971
  const resolved = resolveConvexDir(convexDir2);
944
972
  console.log("[zodvex] Starting watch mode...");
945
- await generate(resolved);
973
+ await generate(resolved, options);
946
974
  let debounceTimer = null;
947
975
  const watcher = fs2.watch(resolved, { recursive: true }, (_event, filename) => {
948
976
  if (!filename) return;
@@ -953,7 +981,7 @@ async function dev(convexDir2) {
953
981
  debounceTimer = setTimeout(async () => {
954
982
  console.log("[zodvex] Regenerating...");
955
983
  try {
956
- await generate(resolved);
984
+ await generate(resolved, options);
957
985
  } catch (err) {
958
986
  console.error("[zodvex] Generation failed:", err.message);
959
987
  }
@@ -1001,14 +1029,15 @@ function resolveConvexDir(dir) {
1001
1029
 
1002
1030
  // src/cli/index.ts
1003
1031
  var command = process.argv[2];
1004
- var convexDir = process.argv[3];
1032
+ var miniFlag = process.argv.includes("--mini");
1033
+ var convexDir = process.argv.slice(3).find((a) => !a.startsWith("--"));
1005
1034
  async function main() {
1006
1035
  switch (command) {
1007
1036
  case "generate":
1008
- await generate(convexDir);
1037
+ await generate(convexDir, { mini: miniFlag });
1009
1038
  break;
1010
1039
  case "dev":
1011
- await dev(convexDir);
1040
+ await dev(convexDir, { mini: miniFlag });
1012
1041
  break;
1013
1042
  case "init": {
1014
1043
  const { init: init2 } = await Promise.resolve().then(() => (init_init(), init_exports));
@@ -1062,12 +1091,15 @@ function printHelp() {
1062
1091
  zodvex - Convex codegen for Zod schemas
1063
1092
 
1064
1093
  Usage:
1065
- zodvex generate [convex-dir] Generate schema and validator files
1066
- zodvex dev [convex-dir] Watch mode \u2014 regenerate on changes
1067
- zodvex init Set up zodvex in an existing Convex project
1068
- zodvex migrate [dir] Migrate pre-0.6 APIs (renames + import fixes)
1069
- zodvex migrate [dir] --dry-run Preview changes without writing
1070
- zodvex help Show this help message
1094
+ zodvex generate [convex-dir] [--mini] Generate schema and validator files
1095
+ zodvex dev [convex-dir] [--mini] Watch mode \u2014 regenerate on changes
1096
+ zodvex init Set up zodvex in an existing Convex project
1097
+ zodvex migrate [dir] Migrate pre-0.6 APIs (renames + import fixes)
1098
+ zodvex migrate [dir] --dry-run Preview changes without writing
1099
+ zodvex help Show this help message
1100
+
1101
+ Flags:
1102
+ --mini Emit zod/mini-compatible output (functional forms, zodvex/mini imports)
1071
1103
  `);
1072
1104
  }
1073
1105
  main().catch((err) => {