@famgia/omnify-typescript 0.0.64 → 0.0.66
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/{chunk-53QQ3AMT.js → chunk-4L77AHAC.js} +544 -23
- package/dist/chunk-4L77AHAC.js.map +1 -0
- package/dist/index.cjs +543 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.ts +9 -2
- package/dist/index.js +1 -1
- package/dist/plugin.cjs +561 -27
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +13 -7
- package/dist/plugin.d.ts +13 -7
- package/dist/plugin.js +19 -6
- package/dist/plugin.js.map +1 -1
- package/package.json +10 -2
- package/dist/chunk-53QQ3AMT.js.map +0 -1
|
@@ -902,11 +902,460 @@ function generateRulesFiles(schemas, options = {}) {
|
|
|
902
902
|
return files;
|
|
903
903
|
}
|
|
904
904
|
|
|
905
|
+
// src/zod-generator.ts
|
|
906
|
+
function getMultiLocaleDisplayName2(value, locales, fallbackLocale, defaultValue) {
|
|
907
|
+
if (!value) {
|
|
908
|
+
const result2 = {};
|
|
909
|
+
for (const locale of locales) {
|
|
910
|
+
result2[locale] = defaultValue;
|
|
911
|
+
}
|
|
912
|
+
return result2;
|
|
913
|
+
}
|
|
914
|
+
if (typeof value === "string") {
|
|
915
|
+
const result2 = {};
|
|
916
|
+
for (const locale of locales) {
|
|
917
|
+
result2[locale] = value;
|
|
918
|
+
}
|
|
919
|
+
return result2;
|
|
920
|
+
}
|
|
921
|
+
const result = {};
|
|
922
|
+
for (const locale of locales) {
|
|
923
|
+
result[locale] = value[locale] ?? value[fallbackLocale] ?? value["en"] ?? defaultValue;
|
|
924
|
+
}
|
|
925
|
+
return result;
|
|
926
|
+
}
|
|
927
|
+
function getZodSchemaForType(propDef, fieldName, customTypes) {
|
|
928
|
+
const def = propDef;
|
|
929
|
+
const isNullable = def.nullable ?? false;
|
|
930
|
+
let schema = "";
|
|
931
|
+
if (customTypes) {
|
|
932
|
+
const customType = customTypes.get(propDef.type);
|
|
933
|
+
if (customType && !customType.compound) {
|
|
934
|
+
const sqlType = customType.sql?.sqlType || "VARCHAR";
|
|
935
|
+
schema = "z.string()";
|
|
936
|
+
if (customType.sql?.length) {
|
|
937
|
+
schema += `.max(${customType.sql.length})`;
|
|
938
|
+
}
|
|
939
|
+
if (isNullable) {
|
|
940
|
+
schema += ".optional().nullable()";
|
|
941
|
+
}
|
|
942
|
+
return schema;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
switch (propDef.type) {
|
|
946
|
+
case "String":
|
|
947
|
+
case "Text":
|
|
948
|
+
case "MediumText":
|
|
949
|
+
case "LongText":
|
|
950
|
+
case "Password":
|
|
951
|
+
schema = "z.string()";
|
|
952
|
+
if (!isNullable) {
|
|
953
|
+
schema += ".min(1)";
|
|
954
|
+
}
|
|
955
|
+
if (def.maxLength || def.length) {
|
|
956
|
+
schema += `.max(${def.maxLength ?? def.length})`;
|
|
957
|
+
}
|
|
958
|
+
if (def.minLength && def.minLength > 1) {
|
|
959
|
+
schema = schema.replace(".min(1)", `.min(${def.minLength})`);
|
|
960
|
+
}
|
|
961
|
+
break;
|
|
962
|
+
case "Email":
|
|
963
|
+
schema = "z.string().email()";
|
|
964
|
+
if (def.maxLength || def.length) {
|
|
965
|
+
schema += `.max(${def.maxLength ?? def.length ?? 255})`;
|
|
966
|
+
}
|
|
967
|
+
break;
|
|
968
|
+
case "TinyInt":
|
|
969
|
+
case "Int":
|
|
970
|
+
case "BigInt":
|
|
971
|
+
schema = "z.number().int()";
|
|
972
|
+
if (def.min !== void 0) {
|
|
973
|
+
schema += `.gte(${def.min})`;
|
|
974
|
+
}
|
|
975
|
+
if (def.max !== void 0) {
|
|
976
|
+
schema += `.lte(${def.max})`;
|
|
977
|
+
}
|
|
978
|
+
break;
|
|
979
|
+
case "Float":
|
|
980
|
+
schema = "z.number()";
|
|
981
|
+
if (def.min !== void 0) {
|
|
982
|
+
schema += `.gte(${def.min})`;
|
|
983
|
+
}
|
|
984
|
+
if (def.max !== void 0) {
|
|
985
|
+
schema += `.lte(${def.max})`;
|
|
986
|
+
}
|
|
987
|
+
break;
|
|
988
|
+
case "Boolean":
|
|
989
|
+
schema = "z.boolean()";
|
|
990
|
+
break;
|
|
991
|
+
case "Date":
|
|
992
|
+
schema = "z.string().date()";
|
|
993
|
+
break;
|
|
994
|
+
case "DateTime":
|
|
995
|
+
case "Timestamp":
|
|
996
|
+
schema = "z.string().datetime({ offset: true })";
|
|
997
|
+
break;
|
|
998
|
+
case "Time":
|
|
999
|
+
schema = "z.string().time()";
|
|
1000
|
+
break;
|
|
1001
|
+
case "Json":
|
|
1002
|
+
schema = "z.unknown()";
|
|
1003
|
+
break;
|
|
1004
|
+
case "Enum":
|
|
1005
|
+
if (typeof def.enum === "string") {
|
|
1006
|
+
schema = `${def.enum}Schema`;
|
|
1007
|
+
} else if (Array.isArray(def.enum)) {
|
|
1008
|
+
const values = def.enum.map((v) => `'${v}'`).join(", ");
|
|
1009
|
+
schema = `z.enum([${values}])`;
|
|
1010
|
+
} else {
|
|
1011
|
+
schema = "z.string()";
|
|
1012
|
+
}
|
|
1013
|
+
break;
|
|
1014
|
+
case "Select":
|
|
1015
|
+
if (def.options && def.options.length > 0) {
|
|
1016
|
+
const values = def.options.map((v) => `'${v}'`).join(", ");
|
|
1017
|
+
schema = `z.enum([${values}])`;
|
|
1018
|
+
} else {
|
|
1019
|
+
schema = "z.string()";
|
|
1020
|
+
}
|
|
1021
|
+
break;
|
|
1022
|
+
case "Lookup":
|
|
1023
|
+
schema = "z.number().int().positive()";
|
|
1024
|
+
break;
|
|
1025
|
+
case "Association":
|
|
1026
|
+
return "";
|
|
1027
|
+
case "File":
|
|
1028
|
+
return "";
|
|
1029
|
+
default:
|
|
1030
|
+
schema = "z.string()";
|
|
1031
|
+
}
|
|
1032
|
+
if (isNullable && schema) {
|
|
1033
|
+
schema += ".optional().nullable()";
|
|
1034
|
+
}
|
|
1035
|
+
if (def.pattern && schema) {
|
|
1036
|
+
schema += `.regex(/${def.pattern}/)`;
|
|
1037
|
+
}
|
|
1038
|
+
return schema;
|
|
1039
|
+
}
|
|
1040
|
+
function generateCompoundTypeSchemas(propName, propDef, customType, options) {
|
|
1041
|
+
const schemas = [];
|
|
1042
|
+
const propFields = propDef.fields;
|
|
1043
|
+
const locales = options.localeConfig?.locales ?? ["en"];
|
|
1044
|
+
const fallbackLocale = options.localeConfig?.fallbackLocale ?? "en";
|
|
1045
|
+
if (!customType.expand) return schemas;
|
|
1046
|
+
for (const field of customType.expand) {
|
|
1047
|
+
const fieldName = `${toSnakeCase(propName)}_${toSnakeCase(field.suffix)}`;
|
|
1048
|
+
const fieldOverride = propFields?.[field.suffix];
|
|
1049
|
+
const isNullable = fieldOverride?.nullable ?? propDef.nullable ?? false;
|
|
1050
|
+
const length = fieldOverride?.length ?? field.sql?.length;
|
|
1051
|
+
let schema = "z.string()";
|
|
1052
|
+
if (!isNullable) {
|
|
1053
|
+
schema += ".min(1)";
|
|
1054
|
+
}
|
|
1055
|
+
if (length) {
|
|
1056
|
+
schema += `.max(${length})`;
|
|
1057
|
+
}
|
|
1058
|
+
if (isNullable) {
|
|
1059
|
+
schema += ".optional().nullable()";
|
|
1060
|
+
}
|
|
1061
|
+
const propDisplayName = getMultiLocaleDisplayName2(
|
|
1062
|
+
propDef.displayName,
|
|
1063
|
+
locales,
|
|
1064
|
+
fallbackLocale,
|
|
1065
|
+
propName
|
|
1066
|
+
);
|
|
1067
|
+
schemas.push({
|
|
1068
|
+
fieldName,
|
|
1069
|
+
schema,
|
|
1070
|
+
inCreate: true,
|
|
1071
|
+
inUpdate: true,
|
|
1072
|
+
comment: `${propDisplayName["en"] ?? propName} (${field.suffix})`
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
return schemas;
|
|
1076
|
+
}
|
|
1077
|
+
function generateZodSchemas(schema, options) {
|
|
1078
|
+
const schemas = [];
|
|
1079
|
+
const customTypes = options.customTypes;
|
|
1080
|
+
if (!schema.properties) return schemas;
|
|
1081
|
+
for (const [propName, propDef] of Object.entries(schema.properties)) {
|
|
1082
|
+
if (customTypes) {
|
|
1083
|
+
const customType = customTypes.get(propDef.type);
|
|
1084
|
+
if (customType?.compound) {
|
|
1085
|
+
schemas.push(...generateCompoundTypeSchemas(propName, propDef, customType, options));
|
|
1086
|
+
continue;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const zodSchema = getZodSchemaForType(propDef, propName, customTypes);
|
|
1090
|
+
if (!zodSchema) continue;
|
|
1091
|
+
const fieldName = toSnakeCase(propName);
|
|
1092
|
+
schemas.push({
|
|
1093
|
+
fieldName,
|
|
1094
|
+
schema: zodSchema,
|
|
1095
|
+
inCreate: true,
|
|
1096
|
+
inUpdate: true,
|
|
1097
|
+
comment: void 0
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
return schemas;
|
|
1101
|
+
}
|
|
1102
|
+
function generateDisplayNames(schema, options) {
|
|
1103
|
+
const locales = options.localeConfig?.locales ?? ["en"];
|
|
1104
|
+
const fallbackLocale = options.localeConfig?.fallbackLocale ?? "en";
|
|
1105
|
+
const customTypes = options.customTypes;
|
|
1106
|
+
const displayName = getMultiLocaleDisplayName2(
|
|
1107
|
+
schema.displayName,
|
|
1108
|
+
locales,
|
|
1109
|
+
fallbackLocale,
|
|
1110
|
+
schema.name
|
|
1111
|
+
);
|
|
1112
|
+
const propertyDisplayNames = {};
|
|
1113
|
+
if (schema.properties) {
|
|
1114
|
+
for (const [propName, propDef] of Object.entries(schema.properties)) {
|
|
1115
|
+
const prop = propDef;
|
|
1116
|
+
const fieldName = toSnakeCase(propName);
|
|
1117
|
+
if (customTypes) {
|
|
1118
|
+
const customType = customTypes.get(propDef.type);
|
|
1119
|
+
if (customType?.compound && customType.expand) {
|
|
1120
|
+
for (const field of customType.expand) {
|
|
1121
|
+
const expandedFieldName = `${fieldName}_${toSnakeCase(field.suffix)}`;
|
|
1122
|
+
propertyDisplayNames[expandedFieldName] = getMultiLocaleDisplayName2(
|
|
1123
|
+
prop.displayName,
|
|
1124
|
+
locales,
|
|
1125
|
+
fallbackLocale,
|
|
1126
|
+
propName
|
|
1127
|
+
);
|
|
1128
|
+
for (const locale of locales) {
|
|
1129
|
+
propertyDisplayNames[expandedFieldName] = {
|
|
1130
|
+
...propertyDisplayNames[expandedFieldName],
|
|
1131
|
+
[locale]: `${propertyDisplayNames[expandedFieldName][locale]} (${field.suffix})`
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
continue;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
propertyDisplayNames[fieldName] = getMultiLocaleDisplayName2(
|
|
1139
|
+
prop.displayName,
|
|
1140
|
+
locales,
|
|
1141
|
+
fallbackLocale,
|
|
1142
|
+
propName
|
|
1143
|
+
);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
return { displayName, propertyDisplayNames };
|
|
1147
|
+
}
|
|
1148
|
+
function getExcludedFields(schema, customTypes) {
|
|
1149
|
+
const createExclude = /* @__PURE__ */ new Set();
|
|
1150
|
+
const updateExclude = /* @__PURE__ */ new Set();
|
|
1151
|
+
if (schema.options?.id !== false) {
|
|
1152
|
+
createExclude.add("id");
|
|
1153
|
+
updateExclude.add("id");
|
|
1154
|
+
}
|
|
1155
|
+
if (schema.options?.timestamps !== false) {
|
|
1156
|
+
createExclude.add("created_at");
|
|
1157
|
+
createExclude.add("updated_at");
|
|
1158
|
+
updateExclude.add("created_at");
|
|
1159
|
+
updateExclude.add("updated_at");
|
|
1160
|
+
}
|
|
1161
|
+
if (schema.options?.softDelete) {
|
|
1162
|
+
createExclude.add("deleted_at");
|
|
1163
|
+
updateExclude.add("deleted_at");
|
|
1164
|
+
}
|
|
1165
|
+
if (schema.properties) {
|
|
1166
|
+
if (schema.properties["emailVerifiedAt"] || schema.properties["email_verified_at"]) {
|
|
1167
|
+
createExclude.add("email_verified_at");
|
|
1168
|
+
updateExclude.add("email_verified_at");
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
if (schema.properties && customTypes) {
|
|
1172
|
+
for (const [propName, propDef] of Object.entries(schema.properties)) {
|
|
1173
|
+
const customType = customTypes.get(propDef.type);
|
|
1174
|
+
if (customType?.accessors) {
|
|
1175
|
+
for (const accessor of customType.accessors) {
|
|
1176
|
+
const fieldName = `${toSnakeCase(propName)}_${toSnakeCase(accessor.name)}`;
|
|
1177
|
+
createExclude.add(fieldName);
|
|
1178
|
+
updateExclude.add(fieldName);
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
return { create: createExclude, update: updateExclude };
|
|
1184
|
+
}
|
|
1185
|
+
function formatZodSchemasSection(schemaName, zodSchemas, displayNames, excludedFields) {
|
|
1186
|
+
const parts = [];
|
|
1187
|
+
const lowerName = schemaName.charAt(0).toLowerCase() + schemaName.slice(1);
|
|
1188
|
+
parts.push(`// ============================================================================
|
|
1189
|
+
`);
|
|
1190
|
+
parts.push(`// Display Names
|
|
1191
|
+
`);
|
|
1192
|
+
parts.push(`// ============================================================================
|
|
1193
|
+
|
|
1194
|
+
`);
|
|
1195
|
+
parts.push(`/** Display name for ${schemaName} */
|
|
1196
|
+
`);
|
|
1197
|
+
parts.push(`export const ${schemaName}DisplayName = ${JSON.stringify(displayNames.displayName, null, 2)} as const;
|
|
1198
|
+
|
|
1199
|
+
`);
|
|
1200
|
+
parts.push(`/** Property display names for ${schemaName} */
|
|
1201
|
+
`);
|
|
1202
|
+
parts.push(`export const ${schemaName}PropertyDisplayNames = {
|
|
1203
|
+
`);
|
|
1204
|
+
for (const [propName, localeMap] of Object.entries(displayNames.propertyDisplayNames)) {
|
|
1205
|
+
parts.push(` ${propName}: ${JSON.stringify(localeMap)},
|
|
1206
|
+
`);
|
|
1207
|
+
}
|
|
1208
|
+
parts.push(`} as const;
|
|
1209
|
+
|
|
1210
|
+
`);
|
|
1211
|
+
parts.push(`// ============================================================================
|
|
1212
|
+
`);
|
|
1213
|
+
parts.push(`// Zod Schemas
|
|
1214
|
+
`);
|
|
1215
|
+
parts.push(`// ============================================================================
|
|
1216
|
+
|
|
1217
|
+
`);
|
|
1218
|
+
parts.push(`/** Field schemas for ${schemaName} */
|
|
1219
|
+
`);
|
|
1220
|
+
parts.push(`export const base${schemaName}Schemas = {
|
|
1221
|
+
`);
|
|
1222
|
+
for (const prop of zodSchemas) {
|
|
1223
|
+
if (prop.comment) {
|
|
1224
|
+
parts.push(` /** ${prop.comment} */
|
|
1225
|
+
`);
|
|
1226
|
+
}
|
|
1227
|
+
parts.push(` ${prop.fieldName}: ${prop.schema},
|
|
1228
|
+
`);
|
|
1229
|
+
}
|
|
1230
|
+
parts.push(`} as const;
|
|
1231
|
+
|
|
1232
|
+
`);
|
|
1233
|
+
const createFields = zodSchemas.filter((p) => p.inCreate && !excludedFields.create.has(p.fieldName));
|
|
1234
|
+
parts.push(`/** Create schema for ${schemaName} (POST requests) */
|
|
1235
|
+
`);
|
|
1236
|
+
parts.push(`export const base${schemaName}CreateSchema = z.object({
|
|
1237
|
+
`);
|
|
1238
|
+
for (const prop of createFields) {
|
|
1239
|
+
parts.push(` ${prop.fieldName}: base${schemaName}Schemas.${prop.fieldName},
|
|
1240
|
+
`);
|
|
1241
|
+
}
|
|
1242
|
+
parts.push(`});
|
|
1243
|
+
|
|
1244
|
+
`);
|
|
1245
|
+
parts.push(`/** Update schema for ${schemaName} (PUT/PATCH requests) */
|
|
1246
|
+
`);
|
|
1247
|
+
parts.push(`export const base${schemaName}UpdateSchema = base${schemaName}CreateSchema.partial();
|
|
1248
|
+
|
|
1249
|
+
`);
|
|
1250
|
+
parts.push(`// ============================================================================
|
|
1251
|
+
`);
|
|
1252
|
+
parts.push(`// Inferred Types
|
|
1253
|
+
`);
|
|
1254
|
+
parts.push(`// ============================================================================
|
|
1255
|
+
|
|
1256
|
+
`);
|
|
1257
|
+
parts.push(`export type Base${schemaName}Create = z.infer<typeof base${schemaName}CreateSchema>;
|
|
1258
|
+
`);
|
|
1259
|
+
parts.push(`export type Base${schemaName}Update = z.infer<typeof base${schemaName}UpdateSchema>;
|
|
1260
|
+
|
|
1261
|
+
`);
|
|
1262
|
+
parts.push(`// ============================================================================
|
|
1263
|
+
`);
|
|
1264
|
+
parts.push(`// Helper Functions
|
|
1265
|
+
`);
|
|
1266
|
+
parts.push(`// ============================================================================
|
|
1267
|
+
|
|
1268
|
+
`);
|
|
1269
|
+
parts.push(`/** Get display name for a specific locale */
|
|
1270
|
+
`);
|
|
1271
|
+
parts.push(`export function get${schemaName}DisplayName(locale: string): string {
|
|
1272
|
+
`);
|
|
1273
|
+
parts.push(` return ${schemaName}DisplayName[locale as keyof typeof ${schemaName}DisplayName] ?? ${schemaName}DisplayName['en'] ?? '${schemaName}';
|
|
1274
|
+
`);
|
|
1275
|
+
parts.push(`}
|
|
1276
|
+
|
|
1277
|
+
`);
|
|
1278
|
+
parts.push(`/** Get property display name for a specific locale */
|
|
1279
|
+
`);
|
|
1280
|
+
parts.push(`export function get${schemaName}PropertyDisplayName(property: string, locale: string): string {
|
|
1281
|
+
`);
|
|
1282
|
+
parts.push(` const names = ${schemaName}PropertyDisplayNames[property as keyof typeof ${schemaName}PropertyDisplayNames];
|
|
1283
|
+
`);
|
|
1284
|
+
parts.push(` if (!names) return property;
|
|
1285
|
+
`);
|
|
1286
|
+
parts.push(` return names[locale as keyof typeof names] ?? names['en'] ?? property;
|
|
1287
|
+
`);
|
|
1288
|
+
parts.push(`}
|
|
1289
|
+
`);
|
|
1290
|
+
return parts.join("");
|
|
1291
|
+
}
|
|
1292
|
+
function formatZodModelFile(schemaName) {
|
|
1293
|
+
const lowerName = schemaName.charAt(0).toLowerCase() + schemaName.slice(1);
|
|
1294
|
+
return `/**
|
|
1295
|
+
* ${schemaName} Model
|
|
1296
|
+
*
|
|
1297
|
+
* This file extends the auto-generated base interface.
|
|
1298
|
+
* You can add custom methods, computed properties, or override types/schemas here.
|
|
1299
|
+
* This file will NOT be overwritten by the generator.
|
|
1300
|
+
*/
|
|
1301
|
+
|
|
1302
|
+
import { z } from 'zod';
|
|
1303
|
+
import type { ${schemaName} as ${schemaName}Base } from './base/${schemaName}.js';
|
|
1304
|
+
import {
|
|
1305
|
+
base${schemaName}Schemas,
|
|
1306
|
+
base${schemaName}CreateSchema,
|
|
1307
|
+
base${schemaName}UpdateSchema,
|
|
1308
|
+
${schemaName}DisplayName,
|
|
1309
|
+
${schemaName}PropertyDisplayNames,
|
|
1310
|
+
get${schemaName}DisplayName,
|
|
1311
|
+
get${schemaName}PropertyDisplayName,
|
|
1312
|
+
} from './base/${schemaName}.js';
|
|
1313
|
+
|
|
1314
|
+
// ============================================================================
|
|
1315
|
+
// Types (extend or re-export)
|
|
1316
|
+
// ============================================================================
|
|
1317
|
+
|
|
1318
|
+
export interface ${schemaName} extends ${schemaName}Base {
|
|
1319
|
+
// Add custom properties here
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// ============================================================================
|
|
1323
|
+
// Schemas (extend or re-export)
|
|
1324
|
+
// ============================================================================
|
|
1325
|
+
|
|
1326
|
+
export const ${lowerName}Schemas = { ...base${schemaName}Schemas };
|
|
1327
|
+
export const ${lowerName}CreateSchema = base${schemaName}CreateSchema;
|
|
1328
|
+
export const ${lowerName}UpdateSchema = base${schemaName}UpdateSchema;
|
|
1329
|
+
|
|
1330
|
+
// ============================================================================
|
|
1331
|
+
// Types
|
|
1332
|
+
// ============================================================================
|
|
1333
|
+
|
|
1334
|
+
export type ${schemaName}Create = z.infer<typeof ${lowerName}CreateSchema>;
|
|
1335
|
+
export type ${schemaName}Update = z.infer<typeof ${lowerName}UpdateSchema>;
|
|
1336
|
+
|
|
1337
|
+
// Re-export display names and helpers
|
|
1338
|
+
export {
|
|
1339
|
+
${schemaName}DisplayName,
|
|
1340
|
+
${schemaName}PropertyDisplayNames,
|
|
1341
|
+
get${schemaName}DisplayName,
|
|
1342
|
+
get${schemaName}PropertyDisplayName,
|
|
1343
|
+
};
|
|
1344
|
+
|
|
1345
|
+
// Re-export base type for internal use
|
|
1346
|
+
export type { ${schemaName}Base };
|
|
1347
|
+
`;
|
|
1348
|
+
}
|
|
1349
|
+
|
|
905
1350
|
// src/generator.ts
|
|
906
1351
|
var DEFAULT_OPTIONS = {
|
|
907
1352
|
readonly: false,
|
|
908
1353
|
// Changed: interfaces should be mutable for forms/mutations
|
|
909
|
-
strictNullChecks: true
|
|
1354
|
+
strictNullChecks: true,
|
|
1355
|
+
generateZodSchemas: true,
|
|
1356
|
+
// Generate Zod schemas by default
|
|
1357
|
+
generateRules: false
|
|
1358
|
+
// Legacy Ant Design rules (deprecated, ignored when generateZodSchemas=true)
|
|
910
1359
|
};
|
|
911
1360
|
function generateBaseHeader() {
|
|
912
1361
|
return `/**
|
|
@@ -927,7 +1376,21 @@ function generateModelHeader(schemaName) {
|
|
|
927
1376
|
|
|
928
1377
|
`;
|
|
929
1378
|
}
|
|
930
|
-
function
|
|
1379
|
+
function getComputedFields(schema, customTypes) {
|
|
1380
|
+
const computedFields = [];
|
|
1381
|
+
if (!schema.properties) return computedFields;
|
|
1382
|
+
for (const [propName, propDef] of Object.entries(schema.properties)) {
|
|
1383
|
+
const snakeName = toSnakeCase(propName);
|
|
1384
|
+
const customType = customTypes?.get(propDef.type);
|
|
1385
|
+
if (customType?.accessors) {
|
|
1386
|
+
for (const accessor of customType.accessors) {
|
|
1387
|
+
computedFields.push(`${snakeName}_${toSnakeCase(accessor.name)}`);
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
return computedFields;
|
|
1392
|
+
}
|
|
1393
|
+
function generateUtilityTypes(schemaName, schema, customTypes) {
|
|
931
1394
|
const parts = [];
|
|
932
1395
|
const excludeFields = [];
|
|
933
1396
|
if (schema.options?.id !== false) {
|
|
@@ -939,6 +1402,13 @@ function generateUtilityTypes(schemaName, schema) {
|
|
|
939
1402
|
if (schema.options?.softDelete) {
|
|
940
1403
|
excludeFields.push("'deleted_at'");
|
|
941
1404
|
}
|
|
1405
|
+
if (schema.properties?.["emailVerifiedAt"] || schema.properties?.["email_verified_at"]) {
|
|
1406
|
+
excludeFields.push("'email_verified_at'");
|
|
1407
|
+
}
|
|
1408
|
+
const computedFields = getComputedFields(schema, customTypes);
|
|
1409
|
+
for (const field of computedFields) {
|
|
1410
|
+
excludeFields.push(`'${field}'`);
|
|
1411
|
+
}
|
|
942
1412
|
const omitType = excludeFields.length > 0 ? `Omit<${schemaName}, ${excludeFields.join(" | ")}>` : schemaName;
|
|
943
1413
|
parts.push(`
|
|
944
1414
|
/** For creating new ${schemaName} (POST requests) */`);
|
|
@@ -973,6 +1443,10 @@ function generateBaseInterfaceFile(schemaName, schemas, options) {
|
|
|
973
1443
|
throw new Error(`Interface not found for schema: ${schemaName}`);
|
|
974
1444
|
}
|
|
975
1445
|
const parts = [generateBaseHeader()];
|
|
1446
|
+
if (options.generateZodSchemas) {
|
|
1447
|
+
parts.push(`import { z } from 'zod';
|
|
1448
|
+
`);
|
|
1449
|
+
}
|
|
976
1450
|
const dateImports = needsDateTimeImports(iface);
|
|
977
1451
|
const commonImports = [];
|
|
978
1452
|
if (dateImports.dateTime) commonImports.push("DateTimeString");
|
|
@@ -987,12 +1461,20 @@ function generateBaseInterfaceFile(schemaName, schemas, options) {
|
|
|
987
1461
|
`);
|
|
988
1462
|
}
|
|
989
1463
|
parts.push("\n");
|
|
990
|
-
} else if (commonImports.length > 0) {
|
|
1464
|
+
} else if (commonImports.length > 0 || options.generateZodSchemas) {
|
|
991
1465
|
parts.push("\n");
|
|
992
1466
|
}
|
|
993
1467
|
parts.push(formatInterface(iface));
|
|
994
1468
|
parts.push("\n");
|
|
995
|
-
|
|
1469
|
+
if (options.generateZodSchemas) {
|
|
1470
|
+
const zodSchemas = generateZodSchemas(schema, options);
|
|
1471
|
+
const displayNames = generateDisplayNames(schema, options);
|
|
1472
|
+
const excludedFields = getExcludedFields(schema, options.customTypes);
|
|
1473
|
+
parts.push("\n");
|
|
1474
|
+
parts.push(formatZodSchemasSection(schemaName, zodSchemas, displayNames, excludedFields));
|
|
1475
|
+
} else {
|
|
1476
|
+
parts.push(generateUtilityTypes(schemaName, schema, options.customTypes));
|
|
1477
|
+
}
|
|
996
1478
|
return {
|
|
997
1479
|
filePath: `base/${schemaName}.ts`,
|
|
998
1480
|
content: parts.join(""),
|
|
@@ -1022,7 +1504,16 @@ function generateTypeAliasFile(alias) {
|
|
|
1022
1504
|
overwrite: true
|
|
1023
1505
|
};
|
|
1024
1506
|
}
|
|
1025
|
-
function generateModelFile(schemaName) {
|
|
1507
|
+
function generateModelFile(schemaName, options) {
|
|
1508
|
+
if (options.generateZodSchemas) {
|
|
1509
|
+
return {
|
|
1510
|
+
filePath: `${schemaName}.ts`,
|
|
1511
|
+
content: formatZodModelFile(schemaName),
|
|
1512
|
+
types: [schemaName],
|
|
1513
|
+
overwrite: false
|
|
1514
|
+
// Never overwrite user models
|
|
1515
|
+
};
|
|
1516
|
+
}
|
|
1026
1517
|
const parts = [generateModelHeader(schemaName)];
|
|
1027
1518
|
parts.push(`import type { ${schemaName} as ${schemaName}Base } from './base/${schemaName}.js';
|
|
1028
1519
|
|
|
@@ -1142,33 +1633,63 @@ function generateIndexFile(schemas, enums, typeAliases, options) {
|
|
|
1142
1633
|
}
|
|
1143
1634
|
parts.push("\n");
|
|
1144
1635
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
for (const schema of Object.values(schemas)) {
|
|
1148
|
-
if (schema.kind === "enum") continue;
|
|
1149
|
-
if (schema.options?.hidden === true) continue;
|
|
1150
|
-
parts.push(`export type { ${schema.name} } from './${schema.name}.js';
|
|
1151
|
-
`);
|
|
1152
|
-
parts.push(`export type { ${schema.name}Create, ${schema.name}Update } from './base/${schema.name}.js';
|
|
1153
|
-
`);
|
|
1154
|
-
}
|
|
1155
|
-
if (options.generateRules) {
|
|
1156
|
-
parts.push(`
|
|
1157
|
-
// Validation Rules
|
|
1636
|
+
if (options.generateZodSchemas) {
|
|
1637
|
+
parts.push(`// Models (with Zod schemas and Create/Update types)
|
|
1158
1638
|
`);
|
|
1159
1639
|
for (const schema of Object.values(schemas)) {
|
|
1160
1640
|
if (schema.kind === "enum") continue;
|
|
1161
1641
|
if (schema.options?.hidden === true) continue;
|
|
1642
|
+
const lowerName = schema.name.charAt(0).toLowerCase() + schema.name.slice(1);
|
|
1643
|
+
parts.push(`export type { ${schema.name}, ${schema.name}Create, ${schema.name}Update } from './${schema.name}.js';
|
|
1644
|
+
`);
|
|
1162
1645
|
parts.push(`export {
|
|
1163
1646
|
`);
|
|
1164
|
-
parts.push(`
|
|
1647
|
+
parts.push(` ${lowerName}Schemas,
|
|
1648
|
+
`);
|
|
1649
|
+
parts.push(` ${lowerName}CreateSchema,
|
|
1650
|
+
`);
|
|
1651
|
+
parts.push(` ${lowerName}UpdateSchema,
|
|
1652
|
+
`);
|
|
1653
|
+
parts.push(` ${schema.name}DisplayName,
|
|
1654
|
+
`);
|
|
1655
|
+
parts.push(` ${schema.name}PropertyDisplayNames,
|
|
1165
1656
|
`);
|
|
1166
1657
|
parts.push(` get${schema.name}DisplayName,
|
|
1167
1658
|
`);
|
|
1168
1659
|
parts.push(` get${schema.name}PropertyDisplayName,
|
|
1169
1660
|
`);
|
|
1170
|
-
parts.push(`} from '
|
|
1661
|
+
parts.push(`} from './${schema.name}.js';
|
|
1662
|
+
`);
|
|
1663
|
+
}
|
|
1664
|
+
} else {
|
|
1665
|
+
parts.push(`// Models (with Create/Update utility types)
|
|
1666
|
+
`);
|
|
1667
|
+
for (const schema of Object.values(schemas)) {
|
|
1668
|
+
if (schema.kind === "enum") continue;
|
|
1669
|
+
if (schema.options?.hidden === true) continue;
|
|
1670
|
+
parts.push(`export type { ${schema.name} } from './${schema.name}.js';
|
|
1671
|
+
`);
|
|
1672
|
+
parts.push(`export type { ${schema.name}Create, ${schema.name}Update } from './base/${schema.name}.js';
|
|
1673
|
+
`);
|
|
1674
|
+
}
|
|
1675
|
+
if (options.generateRules) {
|
|
1676
|
+
parts.push(`
|
|
1677
|
+
// Validation Rules
|
|
1171
1678
|
`);
|
|
1679
|
+
for (const schema of Object.values(schemas)) {
|
|
1680
|
+
if (schema.kind === "enum") continue;
|
|
1681
|
+
if (schema.options?.hidden === true) continue;
|
|
1682
|
+
parts.push(`export {
|
|
1683
|
+
`);
|
|
1684
|
+
parts.push(` get${schema.name}Rules,
|
|
1685
|
+
`);
|
|
1686
|
+
parts.push(` get${schema.name}DisplayName,
|
|
1687
|
+
`);
|
|
1688
|
+
parts.push(` get${schema.name}PropertyDisplayName,
|
|
1689
|
+
`);
|
|
1690
|
+
parts.push(`} from './rules/${schema.name}.rules.js';
|
|
1691
|
+
`);
|
|
1692
|
+
}
|
|
1172
1693
|
}
|
|
1173
1694
|
}
|
|
1174
1695
|
return {
|
|
@@ -1197,9 +1718,9 @@ function generateTypeScript(schemas, options = {}) {
|
|
|
1197
1718
|
for (const schema of Object.values(schemas)) {
|
|
1198
1719
|
if (schema.kind === "enum") continue;
|
|
1199
1720
|
if (schema.options?.hidden === true) continue;
|
|
1200
|
-
files.push(generateModelFile(schema.name));
|
|
1721
|
+
files.push(generateModelFile(schema.name, opts));
|
|
1201
1722
|
}
|
|
1202
|
-
if (opts.generateRules) {
|
|
1723
|
+
if (!opts.generateZodSchemas && opts.generateRules) {
|
|
1203
1724
|
const rulesFiles = generateRulesFiles(schemas, opts);
|
|
1204
1725
|
files.push(...rulesFiles);
|
|
1205
1726
|
}
|
|
@@ -1233,4 +1754,4 @@ export {
|
|
|
1233
1754
|
generateRulesFiles,
|
|
1234
1755
|
generateTypeScript
|
|
1235
1756
|
};
|
|
1236
|
-
//# sourceMappingURL=chunk-
|
|
1757
|
+
//# sourceMappingURL=chunk-4L77AHAC.js.map
|