@famgia/omnify-typescript 0.0.53 → 0.0.55

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
@@ -40,10 +40,10 @@ var TYPE_MAP = {
40
40
  Text: "string",
41
41
  MediumText: "string",
42
42
  LongText: "string",
43
- Date: "string",
43
+ Date: "DateString",
44
44
  Time: "string",
45
- DateTime: "string",
46
- Timestamp: "string",
45
+ DateTime: "DateTimeString",
46
+ Timestamp: "DateTimeString",
47
47
  Json: "unknown",
48
48
  Email: "string",
49
49
  Password: "string",
@@ -247,14 +247,14 @@ function schemaToInterface(schema, allSchemas, options = {}) {
247
247
  properties.push(
248
248
  {
249
249
  name: "created_at",
250
- type: "string",
250
+ type: "DateTimeString",
251
251
  optional: true,
252
252
  readonly: options.readonly ?? true,
253
253
  comment: "Creation timestamp"
254
254
  },
255
255
  {
256
256
  name: "updated_at",
257
- type: "string",
257
+ type: "DateTimeString",
258
258
  optional: true,
259
259
  readonly: options.readonly ?? true,
260
260
  comment: "Last update timestamp"
@@ -264,7 +264,7 @@ function schemaToInterface(schema, allSchemas, options = {}) {
264
264
  if (schema.options?.softDelete) {
265
265
  properties.push({
266
266
  name: "deleted_at",
267
- type: "string",
267
+ type: "DateTimeString",
268
268
  optional: true,
269
269
  readonly: options.readonly ?? true,
270
270
  comment: "Soft delete timestamp"
@@ -817,19 +817,7 @@ function formatRulesFile(schemaName, rules) {
817
817
  * DO NOT EDIT - This file is automatically generated and will be overwritten.
818
818
  */
819
819
 
820
- `);
821
- parts.push(`export interface LocaleMap { [locale: string]: string; }
822
-
823
- `);
824
- parts.push(`export interface ValidationRule {
825
- required?: boolean;
826
- type?: 'string' | 'number' | 'email' | 'url' | 'integer';
827
- min?: number;
828
- max?: number;
829
- len?: number;
830
- pattern?: RegExp;
831
- message: LocaleMap;
832
- }
820
+ import type { LocaleMap, ValidationRule } from '../common.js';
833
821
 
834
822
  `);
835
823
  parts.push(`/** Display name for ${schemaName} */
@@ -931,7 +919,8 @@ function generateRulesFiles(schemas, options = {}) {
931
919
 
932
920
  // src/generator.ts
933
921
  var DEFAULT_OPTIONS = {
934
- readonly: true,
922
+ readonly: false,
923
+ // Changed: interfaces should be mutable for forms/mutations
935
924
  strictNullChecks: true
936
925
  };
937
926
  function generateBaseHeader() {
@@ -953,26 +942,76 @@ function generateModelHeader(schemaName) {
953
942
 
954
943
  `;
955
944
  }
945
+ function generateUtilityTypes(schemaName, schema) {
946
+ const parts = [];
947
+ const excludeFields = [];
948
+ if (schema.options?.id !== false) {
949
+ excludeFields.push("'id'");
950
+ }
951
+ if (schema.options?.timestamps !== false) {
952
+ excludeFields.push("'created_at'", "'updated_at'");
953
+ }
954
+ if (schema.options?.softDelete) {
955
+ excludeFields.push("'deleted_at'");
956
+ }
957
+ const omitType = excludeFields.length > 0 ? `Omit<${schemaName}, ${excludeFields.join(" | ")}>` : schemaName;
958
+ parts.push(`
959
+ /** For creating new ${schemaName} (POST requests) */`);
960
+ parts.push(`
961
+ export type ${schemaName}Create = ${omitType};
962
+ `);
963
+ parts.push(`
964
+ /** For updating ${schemaName} (PUT/PATCH requests) */`);
965
+ parts.push(`
966
+ export type ${schemaName}Update = Partial<${schemaName}Create>;
967
+ `);
968
+ return parts.join("");
969
+ }
970
+ function needsDateTimeImports(iface) {
971
+ let dateTime = false;
972
+ let date = false;
973
+ for (const prop of iface.properties) {
974
+ if (prop.type === "DateTimeString" || prop.type.includes("DateTimeString")) {
975
+ dateTime = true;
976
+ }
977
+ if (prop.type === "DateString" || prop.type.includes("DateString")) {
978
+ date = true;
979
+ }
980
+ }
981
+ return { dateTime, date };
982
+ }
956
983
  function generateBaseInterfaceFile(schemaName, schemas, options) {
957
984
  const interfaces = generateInterfaces(schemas, options);
958
985
  const iface = interfaces.find((i) => i.name === schemaName);
959
- if (!iface) {
986
+ const schema = schemas[schemaName];
987
+ if (!iface || !schema) {
960
988
  throw new Error(`Interface not found for schema: ${schemaName}`);
961
989
  }
962
990
  const parts = [generateBaseHeader()];
991
+ const dateImports = needsDateTimeImports(iface);
992
+ const commonImports = [];
993
+ if (dateImports.dateTime) commonImports.push("DateTimeString");
994
+ if (dateImports.date) commonImports.push("DateString");
995
+ if (commonImports.length > 0) {
996
+ parts.push(`import type { ${commonImports.join(", ")} } from '../common.js';
997
+ `);
998
+ }
963
999
  if (iface.dependencies && iface.dependencies.length > 0) {
964
1000
  for (const dep of iface.dependencies) {
965
1001
  parts.push(`import type { ${dep} } from './${dep}.js';
966
1002
  `);
967
1003
  }
968
1004
  parts.push("\n");
1005
+ } else if (commonImports.length > 0) {
1006
+ parts.push("\n");
969
1007
  }
970
1008
  parts.push(formatInterface(iface));
971
1009
  parts.push("\n");
1010
+ parts.push(generateUtilityTypes(schemaName, schema));
972
1011
  return {
973
1012
  filePath: `base/${schemaName}.ts`,
974
1013
  content: parts.join(""),
975
- types: [schemaName],
1014
+ types: [schemaName, `${schemaName}Create`, `${schemaName}Update`],
976
1015
  overwrite: true
977
1016
  };
978
1017
  }
@@ -1027,8 +1066,59 @@ function generateModelFile(schemaName) {
1027
1066
  // Never overwrite user models
1028
1067
  };
1029
1068
  }
1030
- function generateIndexFile(schemas, enums, typeAliases) {
1069
+ function generateCommonFile(options) {
1070
+ const locales = options.localeConfig?.locales ?? ["ja", "en"];
1071
+ const localeUnion = locales.map((l) => `'${l}'`).join(" | ");
1072
+ const content = `${generateBaseHeader()}
1073
+ /**
1074
+ * Locale map for multi-language support.
1075
+ */
1076
+ export interface LocaleMap {
1077
+ [locale: string]: string;
1078
+ }
1079
+
1080
+ /**
1081
+ * Supported locales in this project.
1082
+ */
1083
+ export type Locale = ${localeUnion};
1084
+
1085
+ /**
1086
+ * Ant Design compatible validation rule.
1087
+ */
1088
+ export interface ValidationRule {
1089
+ required?: boolean;
1090
+ type?: 'string' | 'number' | 'email' | 'url' | 'integer' | 'array' | 'object';
1091
+ min?: number;
1092
+ max?: number;
1093
+ len?: number;
1094
+ pattern?: RegExp;
1095
+ message: string;
1096
+ }
1097
+
1098
+ /**
1099
+ * ISO 8601 date-time string.
1100
+ */
1101
+ export type DateTimeString = string;
1102
+
1103
+ /**
1104
+ * ISO 8601 date string (YYYY-MM-DD).
1105
+ */
1106
+ export type DateString = string;
1107
+ `;
1108
+ return {
1109
+ filePath: "common.ts",
1110
+ content,
1111
+ types: ["LocaleMap", "Locale", "ValidationRule", "DateTimeString", "DateString"],
1112
+ overwrite: true
1113
+ };
1114
+ }
1115
+ function generateIndexFile(schemas, enums, typeAliases, options) {
1031
1116
  const parts = [generateBaseHeader()];
1117
+ parts.push(`// Common Types
1118
+ `);
1119
+ parts.push(`export type { LocaleMap, Locale, ValidationRule, DateTimeString, DateString } from './common.js';
1120
+
1121
+ `);
1032
1122
  if (enums.length > 0 || typeAliases.length > 0) {
1033
1123
  parts.push(`// Enums
1034
1124
  `);
@@ -1066,13 +1156,34 @@ function generateIndexFile(schemas, enums, typeAliases) {
1066
1156
  }
1067
1157
  parts.push("\n");
1068
1158
  }
1069
- parts.push(`// Models
1159
+ parts.push(`// Models (with Create/Update utility types)
1070
1160
  `);
1071
1161
  for (const schema of Object.values(schemas)) {
1072
1162
  if (schema.kind === "enum") continue;
1073
1163
  if (schema.options?.hidden === true) continue;
1074
1164
  parts.push(`export type { ${schema.name} } from './${schema.name}.js';
1075
1165
  `);
1166
+ parts.push(`export type { ${schema.name}Create, ${schema.name}Update } from './base/${schema.name}.js';
1167
+ `);
1168
+ }
1169
+ if (options.generateRules) {
1170
+ parts.push(`
1171
+ // Validation Rules
1172
+ `);
1173
+ for (const schema of Object.values(schemas)) {
1174
+ if (schema.kind === "enum") continue;
1175
+ if (schema.options?.hidden === true) continue;
1176
+ parts.push(`export {
1177
+ `);
1178
+ parts.push(` get${schema.name}Rules,
1179
+ `);
1180
+ parts.push(` get${schema.name}DisplayName,
1181
+ `);
1182
+ parts.push(` get${schema.name}PropertyDisplayName,
1183
+ `);
1184
+ parts.push(`} from './rules/${schema.name}.rules.js';
1185
+ `);
1186
+ }
1076
1187
  }
1077
1188
  return {
1078
1189
  filePath: "index.ts",
@@ -1106,7 +1217,8 @@ function generateTypeScript(schemas, options = {}) {
1106
1217
  const rulesFiles = generateRulesFiles(schemas, opts);
1107
1218
  files.push(...rulesFiles);
1108
1219
  }
1109
- files.push(generateIndexFile(schemas, enums, typeAliases));
1220
+ files.push(generateCommonFile(opts));
1221
+ files.push(generateIndexFile(schemas, enums, typeAliases, opts));
1110
1222
  return files;
1111
1223
  }
1112
1224