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.js CHANGED
@@ -477,6 +477,68 @@ function buildListParameters() {
477
477
  { name: "select", in: "query", schema: { type: "string" }, description: "Comma-separated fields" }
478
478
  ];
479
479
  }
480
+
481
+ // src/config-generator.ts
482
+ function generateConfig(prisma) {
483
+ const models = getModels(prisma);
484
+ const PRISMA_TO_ZOD2 = {
485
+ String: "z.string()",
486
+ Int: "z.number().int()",
487
+ Float: "z.number()",
488
+ Decimal: "z.number()",
489
+ Boolean: "z.boolean()",
490
+ DateTime: "z.coerce.date()",
491
+ Json: "z.any()",
492
+ BigInt: "z.bigint()",
493
+ Bytes: "z.any()"
494
+ };
495
+ function fieldToZod2(field) {
496
+ if (field.isRelation) return null;
497
+ let zod = PRISMA_TO_ZOD2[field.type] ?? "z.any()";
498
+ if (!field.isRequired) zod = `${zod}.optional()`;
499
+ if (field.isList) zod = `z.array(${zod})`;
500
+ return zod;
501
+ }
502
+ const schemaBlocks = models.map((meta) => {
503
+ const createFields = meta.fields.filter((f) => !f.isRelation && !f.isId && !f.hasDefaultValue && !f.isUpdatedAt).map((f) => {
504
+ const z = fieldToZod2(f);
505
+ return z ? ` ${f.name}: ${z},` : null;
506
+ }).filter(Boolean).join("\n");
507
+ const updateFields = meta.fields.filter((f) => !f.isRelation && !f.isId && !f.isUpdatedAt).map((f) => {
508
+ const z = fieldToZod2(f);
509
+ if (!z) return null;
510
+ const opt = z.includes(".optional()") ? z : `${z}.optional()`;
511
+ return ` ${f.name}: ${opt},`;
512
+ }).filter(Boolean).join("\n");
513
+ return `
514
+ // \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
515
+
516
+ export const ${meta.name}CreateSchema = z.object({
517
+ ${createFields}
518
+ });
519
+
520
+ export const ${meta.name}UpdateSchema = z.object({
521
+ ${updateFields}
522
+ });
523
+
524
+ export type ${meta.name}Create = z.infer<typeof ${meta.name}CreateSchema>;
525
+ export type ${meta.name}Update = z.infer<typeof ${meta.name}UpdateSchema>;`.trim();
526
+ });
527
+ const zodSchemas = `/**
528
+ * Auto-generated Zod schemas from Prisma schema.
529
+ * Generated by omni-rest \u2014 do not edit manually.
530
+ */
531
+ import { z } from "zod";
532
+
533
+ ${schemaBlocks.join("\n\n")}
534
+ `;
535
+ return {
536
+ version: "1",
537
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
538
+ models,
539
+ zodSchemas
540
+ };
541
+ }
480
542
  var EXCLUDED_DIRS = /* @__PURE__ */ new Set(["node_modules", ".git", "dist", "build", ".next", "out"]);
481
543
  function readPackageJson(dir) {
482
544
  const pkgPath = path3__namespace.join(dir, "package.json");
@@ -1842,7 +1904,8 @@ var USAGE = `
1842
1904
  npx omni-rest generate Generate both Zod schemas and OpenAPI spec
1843
1905
  npx omni-rest generate:zod Generate Zod schemas only
1844
1906
  npx omni-rest generate:openapi Generate OpenAPI spec only
1845
- npx omni-rest generate:frontend Scaffold frontend components from Prisma schema
1907
+ npx omni-rest generate:config Generate omni-rest.config.json for omni-rest-client
1908
+ npx omni-rest generate:frontend Scaffold frontend components from Prisma schema (legacy)
1846
1909
  `;
1847
1910
  async function run2() {
1848
1911
  console.log(COLORS2.bold("\n omni-rest generator\n"));
@@ -1855,7 +1918,7 @@ async function run2() {
1855
1918
  }
1856
1919
  return;
1857
1920
  }
1858
- if (!["generate", "generate:zod", "generate:openapi"].includes(command)) {
1921
+ if (!["generate", "generate:zod", "generate:openapi", "generate:config"].includes(command)) {
1859
1922
  console.log(USAGE);
1860
1923
  console.log(COLORS2.bold("\n Done!\n"));
1861
1924
  return;
@@ -1890,6 +1953,16 @@ async function run2() {
1890
1953
  console.error(COLORS2.red(` \u2717 OpenAPI generation failed: ${e.message}`));
1891
1954
  }
1892
1955
  }
1956
+ if (command === "generate:config") {
1957
+ try {
1958
+ console.log(COLORS2.cyan(" \u2192 Generating omni-rest.config.json..."));
1959
+ const config = generateConfig(prisma);
1960
+ write("omni-rest.config.json", JSON.stringify(config, null, 2));
1961
+ console.log(COLORS2.green(" \u2713 Share omni-rest.config.json with your frontend and run: npx omni-rest-client generate:frontend"));
1962
+ } catch (e) {
1963
+ console.error(COLORS2.red(` \u2717 Config generation failed: ${e.message}`));
1964
+ }
1965
+ }
1893
1966
  } finally {
1894
1967
  await prisma.$disconnect();
1895
1968
  }