@famgia/omnify-typescript 0.0.147 → 0.0.149

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/plugin.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,19 +15,12 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/plugin.ts
31
21
  var plugin_exports = {};
32
22
  __export(plugin_exports, {
23
+ DEFAULTS: () => DEFAULTS,
33
24
  LEGACY_DEFAULTS: () => LEGACY_DEFAULTS,
34
25
  MODERN_DEFAULTS: () => MODERN_DEFAULTS,
35
26
  default: () => typescriptPlugin,
@@ -358,6 +349,10 @@ function resolveDisplayName2(value, options = {}) {
358
349
  config: options.localeConfig
359
350
  });
360
351
  }
352
+ function toPascalCase(value) {
353
+ const normalized = value.replace(/([a-z])([A-Z])/g, "$1_$2");
354
+ return normalized.split(/[-_\s]+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
355
+ }
361
356
  function toEnumMemberName(value) {
362
357
  let result = value.split(/[-_\s]+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("").replace(/[^a-zA-Z0-9]/g, "");
363
358
  if (/^\d/.test(result)) {
@@ -637,7 +632,7 @@ function extractInlineEnums(schemas, options = {}) {
637
632
  if (property.type === "Enum") {
638
633
  const enumProp = property;
639
634
  if (Array.isArray(enumProp.enum) && enumProp.enum.length > 0) {
640
- const typeName = `${schema.name}${propName.charAt(0).toUpperCase() + propName.slice(1)}`;
635
+ const typeName = `${schema.name}${toPascalCase(propName)}`;
641
636
  const displayName = resolveDisplayName2(enumProp.displayName, options);
642
637
  const hasLabels = enumProp.enum.some((v) => typeof v !== "string" && v.label !== void 0);
643
638
  if (hasLabels) {
@@ -666,7 +661,7 @@ function extractInlineEnums(schemas, options = {}) {
666
661
  if (property.type === "Select") {
667
662
  const selectProp = property;
668
663
  if (selectProp.options && selectProp.options.length > 0) {
669
- const typeName = `${schema.name}${propName.charAt(0).toUpperCase() + propName.slice(1)}`;
664
+ const typeName = `${schema.name}${toPascalCase(propName)}`;
670
665
  const displayName = resolveDisplayName2(selectProp.displayName, options);
671
666
  const hasLabels = selectProp.options.some((v) => typeof v !== "string" && v.label !== void 0);
672
667
  if (hasLabels) {
@@ -1063,15 +1058,15 @@ function applyValidationRules(schema, rules, propType) {
1063
1058
  if (!rules) return schema;
1064
1059
  let result = schema;
1065
1060
  if (rules.url) {
1066
- result = "z.url()";
1061
+ result = "z.string().url()";
1067
1062
  } else if (rules.uuid) {
1068
- result = "z.uuid()";
1063
+ result = "z.string().uuid()";
1069
1064
  } else if (rules.ip) {
1070
- result = "z.ip()";
1065
+ result = "z.string().ip()";
1071
1066
  } else if (rules.ipv4) {
1072
- result = "z.ipv4()";
1067
+ result = 'z.string().ip({ version: "v4" })';
1073
1068
  } else if (rules.ipv6) {
1074
- result = "z.ipv6()";
1069
+ result = 'z.string().ip({ version: "v6" })';
1075
1070
  }
1076
1071
  const isStringType = ["String", "Text", "MediumText", "LongText", "Password", "Email"].includes(propType);
1077
1072
  if (isStringType) {
@@ -1188,7 +1183,7 @@ function getZodSchemaForType(propDef, fieldName, customTypes) {
1188
1183
  }
1189
1184
  break;
1190
1185
  case "Email":
1191
- schema = "z.email()";
1186
+ schema = "z.string().email()";
1192
1187
  if (def.maxLength || def.length) {
1193
1188
  schema += `.max(${def.maxLength ?? def.length ?? 255})`;
1194
1189
  }
@@ -1293,9 +1288,9 @@ function generateCompoundTypeSchemas(propName, propDef, customType, options) {
1293
1288
  const format = overrideRules?.format ?? pluginRules?.format;
1294
1289
  let schema = "z.string()";
1295
1290
  if (format === "email") {
1296
- schema = "z.email()";
1291
+ schema = "z.string().email()";
1297
1292
  } else if (format === "url") {
1298
- schema = "z.url()";
1293
+ schema = "z.string().url()";
1299
1294
  } else if (format === "phone") {
1300
1295
  schema = "z.string()";
1301
1296
  } else if (format === "postal_code") {
@@ -1800,7 +1795,7 @@ function generateBaseInterfaceFile(schemaName, schemas, options) {
1800
1795
  `);
1801
1796
  }
1802
1797
  if (iface.enumDependencies && iface.enumDependencies.length > 0) {
1803
- const schemaEnumPrefix = options.schemaEnumImportPrefix ?? (options.enumImportPrefix ? `../${options.enumImportPrefix}` : "../enum");
1798
+ const schemaEnumPrefix = options.schemaEnumImportPrefix ?? (options.enumImportPrefix?.startsWith("@") ? options.enumImportPrefix : options.enumImportPrefix ? `../${options.enumImportPrefix}` : "../enum");
1804
1799
  const pluginEnumNames = new Set(
1805
1800
  options.pluginEnums ? Array.from(options.pluginEnums.keys()) : []
1806
1801
  );
@@ -2149,6 +2144,7 @@ function generateIndexFile(schemas, enums, pluginEnums, typeAliases, options) {
2149
2144
  const ext = getImportExt2(options);
2150
2145
  const isNodeModulesBase = options.baseImportPrefix?.startsWith("@");
2151
2146
  const commonImportPath = isNodeModulesBase ? `${options.baseImportPrefix}/common` : "./common";
2147
+ const i18nImportPath = isNodeModulesBase ? `${options.baseImportPrefix}/i18n` : "./i18n";
2152
2148
  parts.push(`// Common Types
2153
2149
  `);
2154
2150
  parts.push(`export type { LocaleMap, Locale, ValidationRule, DateTimeString, DateString } from '${commonImportPath}${ext}';
@@ -2170,7 +2166,7 @@ function generateIndexFile(schemas, enums, pluginEnums, typeAliases, options) {
2170
2166
  `);
2171
2167
  parts.push(` getMessages,
2172
2168
  `);
2173
- parts.push(`} from './i18n${ext}';
2169
+ parts.push(`} from '${i18nImportPath}${ext}';
2174
2170
 
2175
2171
  `);
2176
2172
  const enumPrefix = options.enumImportPrefix ?? "./enum";
@@ -2383,19 +2379,8 @@ Hint: Rename your schema enum to avoid conflict with plugin-provided enums`
2383
2379
  }
2384
2380
 
2385
2381
  // src/plugin.ts
2386
- var import_fs = __toESM(require("fs"), 1);
2387
- var import_path = __toESM(require("path"), 1);
2388
- var import_url = require("url");
2389
- var import_meta = {};
2390
- var __filename = (0, import_url.fileURLToPath)(import_meta.url);
2391
- var __dirname = import_path.default.dirname(__filename);
2392
- var MODERN_DEFAULTS = {
2393
- modelsPath: "node_modules/.omnify/schemas",
2394
- stubsPath: false
2395
- };
2396
- var LEGACY_DEFAULTS = {
2397
- modelsPath: "types/schemas",
2398
- stubsPath: "omnify"
2382
+ var DEFAULTS = {
2383
+ modelsPath: "resources/ts/omnify"
2399
2384
  };
2400
2385
  var TYPESCRIPT_CONFIG_SCHEMA = {
2401
2386
  fields: [
@@ -2403,8 +2388,8 @@ var TYPESCRIPT_CONFIG_SCHEMA = {
2403
2388
  key: "modelsPath",
2404
2389
  type: "path",
2405
2390
  label: "Schemas Output Path",
2406
- description: 'Directory for generated TypeScript types and Zod schemas. Use "node_modules/.omnify/schemas" for modern mode with @famgia/omnify-react.',
2407
- default: LEGACY_DEFAULTS.modelsPath,
2391
+ description: "Directory for generated TypeScript types and Zod schemas.",
2392
+ default: DEFAULTS.modelsPath,
2408
2393
  group: "output"
2409
2394
  },
2410
2395
  {
@@ -2414,69 +2399,15 @@ var TYPESCRIPT_CONFIG_SCHEMA = {
2414
2399
  description: "Generate Zod schemas alongside TypeScript types for form validation",
2415
2400
  default: true,
2416
2401
  group: "output"
2417
- },
2418
- {
2419
- key: "stubsPath",
2420
- type: "path",
2421
- label: "React Stubs Path",
2422
- description: "Directory for React utility stubs (hooks, components). Leave empty to disable. Recommended: use @famgia/omnify-react instead.",
2423
- default: LEGACY_DEFAULTS.stubsPath,
2424
- group: "output"
2425
2402
  }
2426
2403
  ]
2427
2404
  };
2428
- function isNodeModulesPath(p) {
2429
- return p.includes("node_modules");
2430
- }
2431
2405
  function resolveOptions(options) {
2432
- const modelsPath = options?.modelsPath ?? LEGACY_DEFAULTS.modelsPath;
2433
- const isModernMode = isNodeModulesPath(modelsPath);
2434
- const defaultStubsPath = isModernMode ? false : LEGACY_DEFAULTS.stubsPath;
2435
2406
  return {
2436
- modelsPath,
2437
- generateZodSchemas: options?.generateZodSchemas ?? true,
2438
- stubsPath: options?.stubsPath ?? defaultStubsPath,
2439
- isModernMode
2407
+ modelsPath: options?.modelsPath ?? DEFAULTS.modelsPath,
2408
+ generateZodSchemas: options?.generateZodSchemas ?? true
2440
2409
  };
2441
2410
  }
2442
- var STUB_FILES = [
2443
- // Components
2444
- {
2445
- stub: "JapaneseNameField.tsx.stub",
2446
- output: "components/JapaneseNameField.tsx"
2447
- },
2448
- {
2449
- stub: "JapaneseAddressField.tsx.stub",
2450
- output: "components/JapaneseAddressField.tsx"
2451
- },
2452
- {
2453
- stub: "JapaneseBankField.tsx.stub",
2454
- output: "components/JapaneseBankField.tsx"
2455
- },
2456
- // Hooks
2457
- {
2458
- stub: "use-form-mutation.ts.stub",
2459
- output: "hooks/use-form-mutation.ts"
2460
- },
2461
- // Lib - validation utilities
2462
- {
2463
- stub: "zod-i18n.ts.stub",
2464
- output: "lib/zod-i18n.ts"
2465
- },
2466
- {
2467
- stub: "form-validation.ts.stub",
2468
- output: "lib/form-validation.ts"
2469
- },
2470
- // Rules - Japanese validation rules
2471
- {
2472
- stub: "rules/kana.ts.stub",
2473
- output: "lib/rules/kana.ts"
2474
- },
2475
- {
2476
- stub: "rules/index.ts.stub",
2477
- output: "lib/rules/index.ts"
2478
- }
2479
- ];
2480
2411
  function typescriptPlugin(options) {
2481
2412
  const resolved = resolveOptions(options);
2482
2413
  return {
@@ -2488,29 +2419,39 @@ function typescriptPlugin(options) {
2488
2419
  name: "typescript-models",
2489
2420
  description: "Generate TypeScript model definitions",
2490
2421
  generate: async (ctx) => {
2491
- const modelsDir = import_path.default.dirname(resolved.modelsPath);
2492
- const frontendRoot = modelsDir.replace(/\/src\/.*$/, "");
2493
- const pluginEnumBase = `${frontendRoot}/node_modules/@omnify-client`;
2494
- const pluginEnumPath = `${pluginEnumBase}/enum`;
2495
- const hasPluginEnums = ctx.pluginEnums && ctx.pluginEnums.size > 0;
2422
+ const omnifyBaseDir = "node_modules/@omnify-base";
2496
2423
  const files = generateTypeScript(ctx.schemas, {
2497
2424
  generateZodSchemas: resolved.generateZodSchemas,
2498
2425
  localeConfig: ctx.localeConfig,
2499
2426
  customTypes: ctx.customTypes,
2500
2427
  pluginEnums: ctx.pluginEnums,
2501
- pluginEnumImportPrefix: "@omnify-client/enum"
2428
+ // All generated files (enums, base) go to @omnify-base - they shouldn't be edited
2429
+ enumImportPrefix: "@omnify-base/enum",
2430
+ pluginEnumImportPrefix: "@omnify-base/enum",
2431
+ baseImportPrefix: "@omnify-base/schemas"
2502
2432
  });
2503
2433
  const outputs = [];
2504
- if (hasPluginEnums) {
2434
+ const hasOmnifyClientFiles = files.some(
2435
+ (f) => f.category === "enum" || f.category === "plugin-enum" || f.category === "base"
2436
+ );
2437
+ if (hasOmnifyClientFiles) {
2505
2438
  outputs.push({
2506
- path: `${pluginEnumBase}/package.json`,
2439
+ path: `${omnifyBaseDir}/package.json`,
2507
2440
  content: JSON.stringify({
2508
- name: "@omnify-client",
2441
+ name: "@omnify-base",
2509
2442
  version: "0.0.0",
2510
2443
  private: true,
2511
- main: "./enum/index.js",
2444
+ type: "module",
2512
2445
  exports: {
2513
- "./enum/*": "./enum/*.js"
2446
+ // Wildcard exports for TypeScript bundlers (Vite, esbuild, etc.)
2447
+ "./enum/*": {
2448
+ types: "./enum/*.ts",
2449
+ default: "./enum/*.ts"
2450
+ },
2451
+ "./schemas/*": {
2452
+ types: "./schemas/*.ts",
2453
+ default: "./schemas/*.ts"
2454
+ }
2514
2455
  }
2515
2456
  }, null, 2),
2516
2457
  type: "other",
@@ -2519,11 +2460,13 @@ function typescriptPlugin(options) {
2519
2460
  }
2520
2461
  for (const file of files) {
2521
2462
  let outputPath;
2522
- if (file.category === "plugin-enum") {
2523
- outputPath = `${pluginEnumPath}/${file.filePath}`;
2524
- } else if (file.category === "enum") {
2525
- const enumPath = resolved.modelsPath.replace(/\/schemas\/?$/, "/enum");
2526
- outputPath = `${enumPath}/${file.filePath}`;
2463
+ if (file.category === "plugin-enum" || file.category === "enum") {
2464
+ outputPath = `${omnifyBaseDir}/enum/${file.filePath}`;
2465
+ } else if (file.category === "base") {
2466
+ const fileName = file.filePath.replace(/^base\//, "");
2467
+ outputPath = `${omnifyBaseDir}/schemas/${fileName}`;
2468
+ } else if (file.overwrite && (file.filePath === "common.ts" || file.filePath === "i18n.ts")) {
2469
+ outputPath = `${omnifyBaseDir}/schemas/${file.filePath}`;
2527
2470
  } else {
2528
2471
  outputPath = `${resolved.modelsPath}/${file.filePath}`;
2529
2472
  }
@@ -2540,74 +2483,21 @@ function typescriptPlugin(options) {
2540
2483
  }
2541
2484
  return outputs;
2542
2485
  }
2543
- },
2544
- {
2545
- name: "typescript-stubs",
2546
- description: "Generate React utility stubs (hooks, components) - DEPRECATED: use @famgia/omnify-react",
2547
- generate: async (ctx) => {
2548
- if (resolved.stubsPath === false) {
2549
- return [];
2550
- }
2551
- if (ctx.logger) {
2552
- ctx.logger.warn(
2553
- "Stub generation is deprecated. Consider using @famgia/omnify-react package instead.\n npm install @famgia/omnify-react\n Then set stubsPath: false in your config."
2554
- );
2555
- }
2556
- const outputs = [];
2557
- const stubsDir = import_path.default.join(__dirname, "..", "stubs");
2558
- for (const { stub, output } of STUB_FILES) {
2559
- const stubPath = import_path.default.join(stubsDir, stub);
2560
- if (import_fs.default.existsSync(stubPath)) {
2561
- const content = import_fs.default.readFileSync(stubPath, "utf-8");
2562
- outputs.push({
2563
- path: `${resolved.stubsPath}/${output}`,
2564
- content,
2565
- type: "other",
2566
- skipIfExists: false
2567
- // Always overwrite - library files should stay in sync
2568
- });
2569
- }
2570
- }
2571
- outputs.push({
2572
- path: `${resolved.stubsPath}/components/index.ts`,
2573
- content: `export { JapaneseNameField, type JapaneseNameFieldProps } from './JapaneseNameField';
2574
- export { JapaneseAddressField, type JapaneseAddressFieldProps } from './JapaneseAddressField';
2575
- export { JapaneseBankField, type JapaneseBankFieldProps } from './JapaneseBankField';
2576
- `,
2577
- type: "other",
2578
- skipIfExists: false
2579
- });
2580
- outputs.push({
2581
- path: `${resolved.stubsPath}/hooks/index.ts`,
2582
- content: `export { useFormMutation } from './use-form-mutation';
2583
- `,
2584
- type: "other",
2585
- skipIfExists: false
2586
- });
2587
- outputs.push({
2588
- path: `${resolved.stubsPath}/lib/index.ts`,
2589
- content: `export { setZodLocale, getZodLocale, getZodMessage } from './zod-i18n';
2590
- export { zodRule, requiredRule } from './form-validation';
2591
- export * from './rules';
2592
- `,
2593
- type: "other",
2594
- skipIfExists: false
2595
- });
2596
- return outputs;
2597
- }
2598
2486
  }
2599
2487
  ]
2600
2488
  };
2601
2489
  }
2490
+ var MODERN_DEFAULTS = DEFAULTS;
2491
+ var LEGACY_DEFAULTS = DEFAULTS;
2602
2492
  function typescriptModern(options) {
2603
2493
  return typescriptPlugin({
2604
2494
  ...options,
2605
- modelsPath: MODERN_DEFAULTS.modelsPath,
2606
- stubsPath: MODERN_DEFAULTS.stubsPath
2495
+ modelsPath: options?.modelsPath ?? DEFAULTS.modelsPath
2607
2496
  });
2608
2497
  }
2609
2498
  // Annotate the CommonJS export names for ESM import in node:
2610
2499
  0 && (module.exports = {
2500
+ DEFAULTS,
2611
2501
  LEGACY_DEFAULTS,
2612
2502
  MODERN_DEFAULTS,
2613
2503
  typescriptModern,