@supalive/codegen 1.22.0 → 1.22.2

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/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { n as generateToDisk, r as readEntryConfig } from "./src-DvV5kMq2.js";
2
+ import { n as generateToDisk, r as readEntryConfig } from "./src-DCmLjz97.js";
3
3
  import path from "node:path";
4
4
  //#region src/cli.ts
5
5
  function parseArgs(argv) {
package/dist/index.d.ts CHANGED
@@ -130,6 +130,9 @@ interface SchemaEnumEntry {
130
130
  field: string;
131
131
  /** Canonical Dart enum name, e.g. "PosDeviceStatus". */
132
132
  enumName: string;
133
+ /** True when {@link enumName} came from the column's own `.modelName("X")`
134
+ * rather than being derived from table + column. */
135
+ explicit: boolean;
133
136
  /** Values in schema (canonical) order. */
134
137
  values: string[];
135
138
  /** Order-independent value-set signature, for matching. */
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { a as emit, c as loadSchemaEnums, i as resolveConfig, n as generateToDisk, o as extractClient, r as readEntryConfig, s as SchemaEnumRegistry, t as generate } from "./src-DvV5kMq2.js";
1
+ import { a as emit, c as loadSchemaEnums, i as resolveConfig, n as generateToDisk, o as extractClient, r as readEntryConfig, s as SchemaEnumRegistry, t as generate } from "./src-DCmLjz97.js";
2
2
  export { SchemaEnumRegistry, emit, extractClient, generate, generateToDisk, loadSchemaEnums, readEntryConfig, resolveConfig };
@@ -358,7 +358,7 @@ function scanObjectSchema(schema) {
358
358
  }
359
359
  return map;
360
360
  }
361
- function topLevelZodMethods(node) {
361
+ function topLevelZodMethods(node, depth = 0) {
362
362
  const methods = /* @__PURE__ */ new Set();
363
363
  let cur = node;
364
364
  while (cur && Node.isCallExpression(cur)) {
@@ -368,8 +368,14 @@ function topLevelZodMethods(node) {
368
368
  cur = expr.getExpression();
369
369
  } else break;
370
370
  }
371
+ if (cur && Node.isIdentifier(cur) && depth < MAX_ALIAS_DEPTH) {
372
+ const init = resolveIdentifierInit(cur);
373
+ if (init) for (const m of topLevelZodMethods(init, depth + 1)) methods.add(m);
374
+ }
371
375
  return methods;
372
376
  }
377
+ /** Guards against a cyclic alias (`const A = B; const B = A;`) looping forever. */
378
+ const MAX_ALIAS_DEPTH = 8;
373
379
  function topLevelZodDescription(node) {
374
380
  let cur = node;
375
381
  while (cur && Node.isCallExpression(cur)) {
@@ -532,6 +538,7 @@ var SchemaEnumRegistry = class SchemaEnumRegistry {
532
538
  const matches = candidates.filter((c) => c.setSig === sig);
533
539
  if (matches.length === 0) return void 0;
534
540
  if (matches.length === 1) return matches[0];
541
+ if (matches.every((m) => m.enumName === matches[0].enumName)) return matches[0];
535
542
  const pick = (test) => {
536
543
  let best;
537
544
  for (const m of matches) if (test(m.tableStem) && (!best || m.tableStem.length > best.tableStem.length)) best = m;
@@ -549,11 +556,13 @@ var SchemaEnumRegistry = class SchemaEnumRegistry {
549
556
  for (const [fieldKey, zodType] of Object.entries(shape)) {
550
557
  const values = readEnumValues(zodType);
551
558
  if (!values || values.length === 0) continue;
559
+ const explicit = readGenType(zodType);
552
560
  entries.push({
553
561
  table,
554
562
  tableStem,
555
563
  field: camel(fieldKey),
556
- enumName: tableStem + pascal(fieldKey),
564
+ enumName: explicit ?? tableStem + pascal(fieldKey),
565
+ explicit: explicit !== void 0,
557
566
  values,
558
567
  setSig: setSignature(values)
559
568
  });
@@ -583,6 +592,32 @@ function readEnumValues(field) {
583
592
  const entries = def?.entries ?? def?.values;
584
593
  if (entries && typeof entries === "object") return Object.values(entries).map((v) => String(v));
585
594
  }
595
+ /** Read an explicit `.modelName("X")` (i.e. `.meta({ __genType: "X" })`) off a
596
+ * column, looking through the same optional/nullable/default wrappers as
597
+ * {@link readEnumValues} and taking the outermost name so a wrapper can override
598
+ * the shared base type. Realm-independent: we invoke the schema's OWN `.meta()`,
599
+ * which reads the registry belonging to whichever Zod copy built it. */
600
+ function readGenType(field) {
601
+ let cur = field;
602
+ for (let guard = 0; cur && guard < 12; guard++) {
603
+ const name = genTypeOf(cur);
604
+ if (name) return name;
605
+ const def = zodDef(cur);
606
+ if (def?.type && WRAPPERS.has(def.type) && def.innerType) {
607
+ cur = def.innerType;
608
+ continue;
609
+ }
610
+ return;
611
+ }
612
+ }
613
+ function genTypeOf(node) {
614
+ try {
615
+ const name = (node.meta?.())?.__genType;
616
+ return typeof name === "string" && name.length > 0 ? name : void 0;
617
+ } catch {
618
+ return;
619
+ }
620
+ }
586
621
  function zodDef(node) {
587
622
  const n = node;
588
623
  return n?._zod?.def ?? n?.def;
@@ -642,6 +677,8 @@ async function loadSchemaEnums(config) {
642
677
  }
643
678
  //#endregion
644
679
  //#region src/extract.ts
680
+ /** Do two enums carry the same constants, order aside? */
681
+ const sameValueSet = (a, b) => a.length === b.length && [...a].sort().join("\0") === [...b].sort().join("\0");
645
682
  /** Load the router from the entry file and extract a ClientIR. */
646
683
  function extractClient(config, schemaEnums = SchemaEnumRegistry.empty()) {
647
684
  const project = new Project({
@@ -976,6 +1013,7 @@ var Extractor = class {
976
1013
  type,
977
1014
  values: hit.values,
978
1015
  schemaName: hit.enumName,
1016
+ customName: hit.explicit ? hit.enumName : void 0,
979
1017
  stem: ctx.stem,
980
1018
  field: ctx.field,
981
1019
  hint: ctx.hint
@@ -1012,14 +1050,14 @@ var Extractor = class {
1012
1050
  const namedOwners = /* @__PURE__ */ new Map();
1013
1051
  const assignNamed = (g, name) => {
1014
1052
  const owner = namedOwners.get(name);
1015
- if (owner) {
1053
+ if (owner && sameValueSet(owner.values, g.values)) {
1016
1054
  g.type.name = owner.type.name;
1017
1055
  return;
1018
1056
  }
1019
1057
  const finalName = used.has(name) ? this.ensureUnique(name, used) : name;
1020
1058
  used.add(finalName);
1021
1059
  g.type.name = finalName;
1022
- namedOwners.set(name, g);
1060
+ if (!owner) namedOwners.set(name, g);
1023
1061
  this.pushEnumIR(finalName, g.values);
1024
1062
  };
1025
1063
  const customGroups = groups.filter((g) => g.customName).sort((a, b) => a.customName.localeCompare(b.customName));
@@ -1033,7 +1071,6 @@ var Extractor = class {
1033
1071
  if (!arr.some((x) => x.type.name === g.type.name)) arr.push(g);
1034
1072
  namedByField.set(g.field, arr);
1035
1073
  }
1036
- const sameValueSet = (a, b) => a.length === b.length && [...a].sort().join("\0") === [...b].sort().join("\0");
1037
1074
  const fallbackGroups = groups.filter((g) => !g.schemaName && !g.customName).sort((a, b) => fallbackBase(a).localeCompare(fallbackBase(b)));
1038
1075
  const baseCount = /* @__PURE__ */ new Map();
1039
1076
  for (const g of fallbackGroups) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supalive/codegen",
3
- "version": "1.22.0",
3
+ "version": "1.22.2",
4
4
  "description": "Generate type-safe client code (Dart, and more) from a Supalive TypeScript router.",
5
5
  "author": "Rebaz Raouf",
6
6
  "license": "MIT",