omni-rest 0.3.3 → 0.4.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.mjs CHANGED
@@ -455,6 +455,68 @@ function buildListParameters() {
455
455
  { name: "select", in: "query", schema: { type: "string" }, description: "Comma-separated fields" }
456
456
  ];
457
457
  }
458
+
459
+ // src/config-generator.ts
460
+ function generateConfig(prisma) {
461
+ const models = getModels(prisma);
462
+ const PRISMA_TO_ZOD2 = {
463
+ String: "z.string()",
464
+ Int: "z.number().int()",
465
+ Float: "z.number()",
466
+ Decimal: "z.number()",
467
+ Boolean: "z.boolean()",
468
+ DateTime: "z.coerce.date()",
469
+ Json: "z.any()",
470
+ BigInt: "z.bigint()",
471
+ Bytes: "z.any()"
472
+ };
473
+ function fieldToZod2(field) {
474
+ if (field.isRelation) return null;
475
+ let zod = PRISMA_TO_ZOD2[field.type] ?? "z.any()";
476
+ if (!field.isRequired) zod = `${zod}.optional()`;
477
+ if (field.isList) zod = `z.array(${zod})`;
478
+ return zod;
479
+ }
480
+ const schemaBlocks = models.map((meta) => {
481
+ const createFields = meta.fields.filter((f) => !f.isRelation && !f.isId && !f.hasDefaultValue && !f.isUpdatedAt).map((f) => {
482
+ const z = fieldToZod2(f);
483
+ return z ? ` ${f.name}: ${z},` : null;
484
+ }).filter(Boolean).join("\n");
485
+ const updateFields = meta.fields.filter((f) => !f.isRelation && !f.isId && !f.isUpdatedAt).map((f) => {
486
+ const z = fieldToZod2(f);
487
+ if (!z) return null;
488
+ const opt = z.includes(".optional()") ? z : `${z}.optional()`;
489
+ return ` ${f.name}: ${opt},`;
490
+ }).filter(Boolean).join("\n");
491
+ return `
492
+ // \u2500\u2500\u2500 ${meta.name} \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
493
+
494
+ export const ${meta.name}CreateSchema = z.object({
495
+ ${createFields}
496
+ });
497
+
498
+ export const ${meta.name}UpdateSchema = z.object({
499
+ ${updateFields}
500
+ });
501
+
502
+ export type ${meta.name}Create = z.infer<typeof ${meta.name}CreateSchema>;
503
+ export type ${meta.name}Update = z.infer<typeof ${meta.name}UpdateSchema>;`.trim();
504
+ });
505
+ const zodSchemas = `/**
506
+ * Auto-generated Zod schemas from Prisma schema.
507
+ * Generated by omni-rest \u2014 do not edit manually.
508
+ */
509
+ import { z } from "zod";
510
+
511
+ ${schemaBlocks.join("\n\n")}
512
+ `;
513
+ return {
514
+ version: "1",
515
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
516
+ models,
517
+ zodSchemas
518
+ };
519
+ }
458
520
  var EXCLUDED_DIRS = /* @__PURE__ */ new Set(["node_modules", ".git", "dist", "build", ".next", "out"]);
459
521
  function readPackageJson(dir) {
460
522
  const pkgPath = path3.join(dir, "package.json");
@@ -1820,7 +1882,8 @@ var USAGE = `
1820
1882
  npx omni-rest generate Generate both Zod schemas and OpenAPI spec
1821
1883
  npx omni-rest generate:zod Generate Zod schemas only
1822
1884
  npx omni-rest generate:openapi Generate OpenAPI spec only
1823
- npx omni-rest generate:frontend Scaffold frontend components from Prisma schema
1885
+ npx omni-rest generate:config Generate omni-rest.config.json for omni-rest-client
1886
+ npx omni-rest generate:frontend Scaffold frontend components from Prisma schema (legacy)
1824
1887
  `;
1825
1888
  async function run2() {
1826
1889
  console.log(COLORS2.bold("\n omni-rest generator\n"));
@@ -1833,7 +1896,7 @@ async function run2() {
1833
1896
  }
1834
1897
  return;
1835
1898
  }
1836
- if (!["generate", "generate:zod", "generate:openapi"].includes(command)) {
1899
+ if (!["generate", "generate:zod", "generate:openapi", "generate:config"].includes(command)) {
1837
1900
  console.log(USAGE);
1838
1901
  console.log(COLORS2.bold("\n Done!\n"));
1839
1902
  return;
@@ -1868,6 +1931,16 @@ async function run2() {
1868
1931
  console.error(COLORS2.red(` \u2717 OpenAPI generation failed: ${e.message}`));
1869
1932
  }
1870
1933
  }
1934
+ if (command === "generate:config") {
1935
+ try {
1936
+ console.log(COLORS2.cyan(" \u2192 Generating omni-rest.config.json..."));
1937
+ const config = generateConfig(prisma);
1938
+ write("omni-rest.config.json", JSON.stringify(config, null, 2));
1939
+ console.log(COLORS2.green(" \u2713 Share omni-rest.config.json with your frontend and run: npx omni-rest-client generate:frontend"));
1940
+ } catch (e) {
1941
+ console.error(COLORS2.red(` \u2717 Config generation failed: ${e.message}`));
1942
+ }
1943
+ }
1871
1944
  } finally {
1872
1945
  await prisma.$disconnect();
1873
1946
  }