@famgia/omnify-typescript 0.0.1 → 0.0.4
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-J46F3EBS.js +638 -0
- package/dist/chunk-J46F3EBS.js.map +1 -0
- package/dist/index.cjs +315 -155
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +43 -36
- package/dist/index.d.ts +43 -36
- package/dist/index.js +2 -7
- package/dist/plugin.cjs +324 -186
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +10 -20
- package/dist/plugin.d.ts +10 -20
- package/dist/plugin.js +11 -40
- package/dist/plugin.js.map +1 -1
- package/package.json +6 -4
- package/scripts/postinstall.js +276 -0
- package/dist/chunk-2BXDNKIN.js +0 -477
- package/dist/chunk-2BXDNKIN.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -29,10 +29,8 @@ __export(index_exports, {
|
|
|
29
29
|
generateEnums: () => generateEnums,
|
|
30
30
|
generateInterfaces: () => generateInterfaces,
|
|
31
31
|
generateTypeScript: () => generateTypeScript,
|
|
32
|
-
|
|
33
|
-
generateTypeScriptFiles: () => generateTypeScriptFiles,
|
|
32
|
+
generateTypeScriptFiles: () => generateTypeScript,
|
|
34
33
|
getPropertyType: () => getPropertyType,
|
|
35
|
-
getTypeScriptPath: () => getTypeScriptPath,
|
|
36
34
|
propertyToTSProperty: () => propertyToTSProperty,
|
|
37
35
|
schemaToEnum: () => schemaToEnum,
|
|
38
36
|
schemaToInterface: () => schemaToInterface,
|
|
@@ -44,6 +42,7 @@ __export(index_exports, {
|
|
|
44
42
|
module.exports = __toCommonJS(index_exports);
|
|
45
43
|
|
|
46
44
|
// src/interface-generator.ts
|
|
45
|
+
var import_omnify_types = require("@famgia/omnify-types");
|
|
47
46
|
var TYPE_MAP = {
|
|
48
47
|
String: "string",
|
|
49
48
|
Int: "number",
|
|
@@ -69,6 +68,15 @@ var PK_TYPE_MAP = {
|
|
|
69
68
|
Uuid: "string",
|
|
70
69
|
String: "string"
|
|
71
70
|
};
|
|
71
|
+
function resolveDisplayName(value, options = {}) {
|
|
72
|
+
if (value === void 0) {
|
|
73
|
+
return void 0;
|
|
74
|
+
}
|
|
75
|
+
return (0, import_omnify_types.resolveLocalizedString)(value, {
|
|
76
|
+
locale: options.locale,
|
|
77
|
+
config: options.localeConfig
|
|
78
|
+
});
|
|
79
|
+
}
|
|
72
80
|
function toPropertyName(name) {
|
|
73
81
|
return name;
|
|
74
82
|
}
|
|
@@ -130,6 +138,7 @@ function getPropertyType(property, _allSchemas) {
|
|
|
130
138
|
function propertyToTSProperties(propertyName, property, allSchemas, options = {}) {
|
|
131
139
|
const baseProp = property;
|
|
132
140
|
const isReadonly = options.readonly ?? true;
|
|
141
|
+
const displayName = resolveDisplayName(baseProp.displayName, options);
|
|
133
142
|
if (property.type === "Association") {
|
|
134
143
|
const assocProp = property;
|
|
135
144
|
if (assocProp.relation === "MorphTo" && assocProp.targets && assocProp.targets.length > 0) {
|
|
@@ -157,7 +166,7 @@ function propertyToTSProperties(propertyName, property, allSchemas, options = {}
|
|
|
157
166
|
type: `${relationUnion} | null`,
|
|
158
167
|
optional: true,
|
|
159
168
|
readonly: isReadonly,
|
|
160
|
-
comment:
|
|
169
|
+
comment: displayName ?? `Polymorphic relation to ${assocProp.targets.join(", ")}`
|
|
161
170
|
}
|
|
162
171
|
];
|
|
163
172
|
}
|
|
@@ -168,7 +177,7 @@ function propertyToTSProperties(propertyName, property, allSchemas, options = {}
|
|
|
168
177
|
type,
|
|
169
178
|
optional: baseProp.nullable ?? false,
|
|
170
179
|
readonly: isReadonly,
|
|
171
|
-
comment:
|
|
180
|
+
comment: displayName
|
|
172
181
|
}];
|
|
173
182
|
}
|
|
174
183
|
function propertyToTSProperty(propertyName, property, allSchemas, options = {}) {
|
|
@@ -218,10 +227,11 @@ function schemaToInterface(schema, allSchemas, options = {}) {
|
|
|
218
227
|
comment: "Soft delete timestamp"
|
|
219
228
|
});
|
|
220
229
|
}
|
|
230
|
+
const schemaDisplayName = resolveDisplayName(schema.displayName, options);
|
|
221
231
|
return {
|
|
222
232
|
name: toInterfaceName(schema.name),
|
|
223
233
|
properties,
|
|
224
|
-
comment:
|
|
234
|
+
comment: schemaDisplayName ?? schema.name
|
|
225
235
|
};
|
|
226
236
|
}
|
|
227
237
|
function formatProperty(property) {
|
|
@@ -254,31 +264,54 @@ function generateInterfaces(schemas, options = {}) {
|
|
|
254
264
|
}
|
|
255
265
|
|
|
256
266
|
// src/enum-generator.ts
|
|
267
|
+
var import_omnify_types2 = require("@famgia/omnify-types");
|
|
268
|
+
function resolveDisplayName2(value, options = {}) {
|
|
269
|
+
if (value === void 0) {
|
|
270
|
+
return void 0;
|
|
271
|
+
}
|
|
272
|
+
return (0, import_omnify_types2.resolveLocalizedString)(value, {
|
|
273
|
+
locale: options.locale,
|
|
274
|
+
config: options.localeConfig
|
|
275
|
+
});
|
|
276
|
+
}
|
|
257
277
|
function toEnumMemberName(value) {
|
|
258
278
|
return value.split(/[-_\s]+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
259
279
|
}
|
|
260
280
|
function toEnumName(schemaName) {
|
|
261
281
|
return schemaName;
|
|
262
282
|
}
|
|
263
|
-
function
|
|
283
|
+
function parseEnumValue(value) {
|
|
284
|
+
if (typeof value === "string") {
|
|
285
|
+
return {
|
|
286
|
+
name: toEnumMemberName(value),
|
|
287
|
+
value
|
|
288
|
+
// No label or extra - will fallback to value
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
name: toEnumMemberName(value.value),
|
|
293
|
+
value: value.value,
|
|
294
|
+
label: value.label,
|
|
295
|
+
extra: value.extra
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function schemaToEnum(schema, options = {}) {
|
|
264
299
|
if (schema.kind !== "enum" || !schema.values) {
|
|
265
300
|
return null;
|
|
266
301
|
}
|
|
267
|
-
const values = schema.values.map((value) => (
|
|
268
|
-
|
|
269
|
-
value
|
|
270
|
-
}));
|
|
302
|
+
const values = schema.values.map((value) => parseEnumValue(value));
|
|
303
|
+
const displayName = resolveDisplayName2(schema.displayName, options);
|
|
271
304
|
return {
|
|
272
305
|
name: toEnumName(schema.name),
|
|
273
306
|
values,
|
|
274
|
-
comment:
|
|
307
|
+
comment: displayName ?? schema.name
|
|
275
308
|
};
|
|
276
309
|
}
|
|
277
|
-
function generateEnums(schemas) {
|
|
310
|
+
function generateEnums(schemas, options = {}) {
|
|
278
311
|
const enums = [];
|
|
279
312
|
for (const schema of Object.values(schemas)) {
|
|
280
313
|
if (schema.kind === "enum") {
|
|
281
|
-
const enumDef = schemaToEnum(schema);
|
|
314
|
+
const enumDef = schemaToEnum(schema, options);
|
|
282
315
|
if (enumDef) {
|
|
283
316
|
enums.push(enumDef);
|
|
284
317
|
}
|
|
@@ -287,14 +320,85 @@ function generateEnums(schemas) {
|
|
|
287
320
|
return enums;
|
|
288
321
|
}
|
|
289
322
|
function formatEnum(enumDef) {
|
|
290
|
-
const comment = enumDef
|
|
291
|
-
|
|
323
|
+
const { name, values, comment } = enumDef;
|
|
324
|
+
const parts = [];
|
|
325
|
+
if (comment) {
|
|
326
|
+
parts.push(`/**
|
|
327
|
+
* ${comment}
|
|
292
328
|
*/
|
|
293
|
-
`
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
${
|
|
297
|
-
}
|
|
329
|
+
`);
|
|
330
|
+
}
|
|
331
|
+
const enumValues = values.map((v) => ` ${v.name} = '${v.value}',`).join("\n");
|
|
332
|
+
parts.push(`export enum ${name} {
|
|
333
|
+
${enumValues}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
`);
|
|
337
|
+
parts.push(`/** All ${name} values */
|
|
338
|
+
`);
|
|
339
|
+
parts.push(`export const ${name}Values = Object.values(${name});
|
|
340
|
+
|
|
341
|
+
`);
|
|
342
|
+
parts.push(`/** Type guard for ${name} */
|
|
343
|
+
`);
|
|
344
|
+
parts.push(`export function is${name}(value: unknown): value is ${name} {
|
|
345
|
+
`);
|
|
346
|
+
parts.push(` return ${name}Values.includes(value as ${name});
|
|
347
|
+
`);
|
|
348
|
+
parts.push(`}
|
|
349
|
+
|
|
350
|
+
`);
|
|
351
|
+
const hasLabels = values.some((v) => v.label !== void 0);
|
|
352
|
+
if (hasLabels) {
|
|
353
|
+
const labelEntries = values.filter((v) => v.label !== void 0).map((v) => ` [${name}.${v.name}]: '${v.label}',`).join("\n");
|
|
354
|
+
parts.push(`const ${lowerFirst(name)}Labels: Partial<Record<${name}, string>> = {
|
|
355
|
+
${labelEntries}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
`);
|
|
359
|
+
}
|
|
360
|
+
parts.push(`/** Get label for ${name} value (fallback to value if no label) */
|
|
361
|
+
`);
|
|
362
|
+
parts.push(`export function get${name}Label(value: ${name}): string {
|
|
363
|
+
`);
|
|
364
|
+
if (hasLabels) {
|
|
365
|
+
parts.push(` return ${lowerFirst(name)}Labels[value] ?? value;
|
|
366
|
+
`);
|
|
367
|
+
} else {
|
|
368
|
+
parts.push(` return value;
|
|
369
|
+
`);
|
|
370
|
+
}
|
|
371
|
+
parts.push(`}
|
|
372
|
+
|
|
373
|
+
`);
|
|
374
|
+
const hasExtra = values.some((v) => v.extra !== void 0);
|
|
375
|
+
if (hasExtra) {
|
|
376
|
+
const extraEntries = values.filter((v) => v.extra !== void 0).map((v) => ` [${name}.${v.name}]: ${JSON.stringify(v.extra)},`).join("\n");
|
|
377
|
+
parts.push(`const ${lowerFirst(name)}Extra: Partial<Record<${name}, Record<string, unknown>>> = {
|
|
378
|
+
${extraEntries}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
`);
|
|
382
|
+
parts.push(`/** Get extra metadata for ${name} value (undefined if not defined) */
|
|
383
|
+
`);
|
|
384
|
+
parts.push(`export function get${name}Extra(value: ${name}): Record<string, unknown> | undefined {
|
|
385
|
+
`);
|
|
386
|
+
parts.push(` return ${lowerFirst(name)}Extra[value];
|
|
387
|
+
`);
|
|
388
|
+
parts.push(`}`);
|
|
389
|
+
} else {
|
|
390
|
+
parts.push(`/** Get extra metadata for ${name} value (undefined if not defined) */
|
|
391
|
+
`);
|
|
392
|
+
parts.push(`export function get${name}Extra(_value: ${name}): Record<string, unknown> | undefined {
|
|
393
|
+
`);
|
|
394
|
+
parts.push(` return undefined;
|
|
395
|
+
`);
|
|
396
|
+
parts.push(`}`);
|
|
397
|
+
}
|
|
398
|
+
return parts.join("");
|
|
399
|
+
}
|
|
400
|
+
function lowerFirst(str) {
|
|
401
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
298
402
|
}
|
|
299
403
|
function enumToUnionType(enumDef) {
|
|
300
404
|
const type = enumDef.values.map((v) => `'${v.value}'`).join(" | ");
|
|
@@ -305,13 +409,51 @@ function enumToUnionType(enumDef) {
|
|
|
305
409
|
};
|
|
306
410
|
}
|
|
307
411
|
function formatTypeAlias(alias) {
|
|
308
|
-
const comment = alias
|
|
309
|
-
|
|
412
|
+
const { name, type, comment } = alias;
|
|
413
|
+
const parts = [];
|
|
414
|
+
if (comment) {
|
|
415
|
+
parts.push(`/**
|
|
416
|
+
* ${comment}
|
|
310
417
|
*/
|
|
311
|
-
`
|
|
312
|
-
|
|
418
|
+
`);
|
|
419
|
+
}
|
|
420
|
+
parts.push(`export type ${name} = ${type};
|
|
421
|
+
|
|
422
|
+
`);
|
|
423
|
+
const values = type.split(" | ").map((v) => v.trim());
|
|
424
|
+
parts.push(`/** All ${name} values */
|
|
425
|
+
`);
|
|
426
|
+
parts.push(`export const ${name}Values: ${name}[] = [${values.join(", ")}];
|
|
427
|
+
|
|
428
|
+
`);
|
|
429
|
+
parts.push(`/** Type guard for ${name} */
|
|
430
|
+
`);
|
|
431
|
+
parts.push(`export function is${name}(value: unknown): value is ${name} {
|
|
432
|
+
`);
|
|
433
|
+
parts.push(` return ${name}Values.includes(value as ${name});
|
|
434
|
+
`);
|
|
435
|
+
parts.push(`}
|
|
436
|
+
|
|
437
|
+
`);
|
|
438
|
+
parts.push(`/** Get label for ${name} value (returns value as-is) */
|
|
439
|
+
`);
|
|
440
|
+
parts.push(`export function get${name}Label(value: ${name}): string {
|
|
441
|
+
`);
|
|
442
|
+
parts.push(` return value;
|
|
443
|
+
`);
|
|
444
|
+
parts.push(`}
|
|
445
|
+
|
|
446
|
+
`);
|
|
447
|
+
parts.push(`/** Get extra metadata for ${name} value (always undefined for type aliases) */
|
|
448
|
+
`);
|
|
449
|
+
parts.push(`export function get${name}Extra(_value: ${name}): Record<string, unknown> | undefined {
|
|
450
|
+
`);
|
|
451
|
+
parts.push(` return undefined;
|
|
452
|
+
`);
|
|
453
|
+
parts.push(`}`);
|
|
454
|
+
return parts.join("");
|
|
313
455
|
}
|
|
314
|
-
function extractInlineEnums(schemas) {
|
|
456
|
+
function extractInlineEnums(schemas, options = {}) {
|
|
315
457
|
const typeAliases = [];
|
|
316
458
|
for (const schema of Object.values(schemas)) {
|
|
317
459
|
if (schema.kind === "enum" || !schema.properties) {
|
|
@@ -322,10 +464,14 @@ function extractInlineEnums(schemas) {
|
|
|
322
464
|
const enumProp = property;
|
|
323
465
|
if (Array.isArray(enumProp.enum) && enumProp.enum.length > 0) {
|
|
324
466
|
const typeName = `${schema.name}${propName.charAt(0).toUpperCase() + propName.slice(1)}`;
|
|
467
|
+
const values = enumProp.enum.map(
|
|
468
|
+
(v) => typeof v === "string" ? v : v.value
|
|
469
|
+
);
|
|
470
|
+
const displayName = resolveDisplayName2(enumProp.displayName, options);
|
|
325
471
|
typeAliases.push({
|
|
326
472
|
name: typeName,
|
|
327
|
-
type:
|
|
328
|
-
comment:
|
|
473
|
+
type: values.map((v) => `'${v}'`).join(" | "),
|
|
474
|
+
comment: displayName ?? `${schema.name} ${propName} enum`
|
|
329
475
|
});
|
|
330
476
|
}
|
|
331
477
|
}
|
|
@@ -333,10 +479,11 @@ function extractInlineEnums(schemas) {
|
|
|
333
479
|
const selectProp = property;
|
|
334
480
|
if (selectProp.options && selectProp.options.length > 0) {
|
|
335
481
|
const typeName = `${schema.name}${propName.charAt(0).toUpperCase() + propName.slice(1)}`;
|
|
482
|
+
const displayName = resolveDisplayName2(selectProp.displayName, options);
|
|
336
483
|
typeAliases.push({
|
|
337
484
|
name: typeName,
|
|
338
485
|
type: selectProp.options.map((v) => `'${v}'`).join(" | "),
|
|
339
|
-
comment:
|
|
486
|
+
comment: displayName ?? `${schema.name} ${propName} options`
|
|
340
487
|
});
|
|
341
488
|
}
|
|
342
489
|
}
|
|
@@ -347,154 +494,169 @@ function extractInlineEnums(schemas) {
|
|
|
347
494
|
|
|
348
495
|
// src/generator.ts
|
|
349
496
|
var DEFAULT_OPTIONS = {
|
|
350
|
-
singleFile: true,
|
|
351
|
-
fileName: "types.ts",
|
|
352
497
|
readonly: true,
|
|
353
498
|
strictNullChecks: true
|
|
354
499
|
};
|
|
355
|
-
function
|
|
500
|
+
function generateBaseHeader() {
|
|
356
501
|
return `/**
|
|
357
502
|
* Auto-generated TypeScript types from Omnify schemas.
|
|
358
|
-
* DO NOT EDIT - This file is automatically generated.
|
|
503
|
+
* DO NOT EDIT - This file is automatically generated and will be overwritten.
|
|
359
504
|
*/
|
|
360
505
|
|
|
361
506
|
`;
|
|
362
507
|
}
|
|
363
|
-
function
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
508
|
+
function generateModelHeader(schemaName) {
|
|
509
|
+
return `/**
|
|
510
|
+
* ${schemaName} Model
|
|
511
|
+
*
|
|
512
|
+
* This file extends the auto-generated base interface.
|
|
513
|
+
* You can add custom methods, computed properties, or override types here.
|
|
514
|
+
* This file will NOT be overwritten by the generator.
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
`;
|
|
518
|
+
}
|
|
519
|
+
function generateBaseInterfaceFile(schemaName, schemas, options) {
|
|
520
|
+
const interfaces = generateInterfaces(schemas, options);
|
|
521
|
+
const iface = interfaces.find((i) => i.name === schemaName);
|
|
522
|
+
if (!iface) {
|
|
523
|
+
throw new Error(`Interface not found for schema: ${schemaName}`);
|
|
524
|
+
}
|
|
525
|
+
const parts = [generateBaseHeader()];
|
|
526
|
+
parts.push(formatInterface(iface));
|
|
527
|
+
parts.push("\n");
|
|
528
|
+
return {
|
|
529
|
+
filePath: `base/${schemaName}.ts`,
|
|
530
|
+
content: parts.join(""),
|
|
531
|
+
types: [schemaName],
|
|
532
|
+
overwrite: true
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function generateEnumFile(enumDef) {
|
|
536
|
+
const parts = [generateBaseHeader()];
|
|
537
|
+
parts.push(formatEnum(enumDef));
|
|
538
|
+
parts.push("\n");
|
|
539
|
+
return {
|
|
540
|
+
filePath: `enum/${enumDef.name}.ts`,
|
|
541
|
+
content: parts.join(""),
|
|
542
|
+
types: [enumDef.name],
|
|
543
|
+
overwrite: true
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
function generateTypeAliasFile(alias) {
|
|
547
|
+
const parts = [generateBaseHeader()];
|
|
548
|
+
parts.push(formatTypeAlias(alias));
|
|
549
|
+
parts.push("\n");
|
|
550
|
+
return {
|
|
551
|
+
filePath: `enum/${alias.name}.ts`,
|
|
552
|
+
content: parts.join(""),
|
|
553
|
+
types: [alias.name],
|
|
554
|
+
overwrite: true
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
function generateModelFile(schemaName) {
|
|
558
|
+
const parts = [generateModelHeader(schemaName)];
|
|
559
|
+
parts.push(`import type { ${schemaName} as ${schemaName}Base } from './base/${schemaName}.js';
|
|
560
|
+
|
|
561
|
+
`);
|
|
562
|
+
parts.push(`/**
|
|
563
|
+
* ${schemaName} model interface.
|
|
564
|
+
* Add custom properties or methods here.
|
|
565
|
+
*/
|
|
566
|
+
`);
|
|
567
|
+
parts.push(`export interface ${schemaName} extends ${schemaName}Base {
|
|
568
|
+
`);
|
|
569
|
+
parts.push(` // Add custom properties here
|
|
570
|
+
`);
|
|
571
|
+
parts.push(`}
|
|
572
|
+
|
|
573
|
+
`);
|
|
574
|
+
parts.push(`// Re-export base type for internal use
|
|
575
|
+
`);
|
|
576
|
+
parts.push(`export type { ${schemaName}Base };
|
|
577
|
+
`);
|
|
578
|
+
return {
|
|
579
|
+
filePath: `${schemaName}.ts`,
|
|
580
|
+
content: parts.join(""),
|
|
581
|
+
types: [schemaName],
|
|
582
|
+
overwrite: false
|
|
583
|
+
// Never overwrite user models
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
function generateIndexFile(schemas, enums, typeAliases) {
|
|
587
|
+
const parts = [generateBaseHeader()];
|
|
588
|
+
if (enums.length > 0 || typeAliases.length > 0) {
|
|
589
|
+
parts.push(`// Enums
|
|
590
|
+
`);
|
|
370
591
|
for (const enumDef of enums) {
|
|
371
|
-
parts.push(
|
|
372
|
-
|
|
373
|
-
|
|
592
|
+
parts.push(`export {
|
|
593
|
+
`);
|
|
594
|
+
parts.push(` ${enumDef.name},
|
|
595
|
+
`);
|
|
596
|
+
parts.push(` ${enumDef.name}Values,
|
|
597
|
+
`);
|
|
598
|
+
parts.push(` is${enumDef.name},
|
|
599
|
+
`);
|
|
600
|
+
parts.push(` get${enumDef.name}Label,
|
|
601
|
+
`);
|
|
602
|
+
parts.push(` get${enumDef.name}Extra,
|
|
603
|
+
`);
|
|
604
|
+
parts.push(`} from './enum/${enumDef.name}.js';
|
|
605
|
+
`);
|
|
374
606
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
parts.push(
|
|
381
|
-
|
|
382
|
-
|
|
607
|
+
for (const alias of typeAliases) {
|
|
608
|
+
parts.push(`export {
|
|
609
|
+
`);
|
|
610
|
+
parts.push(` type ${alias.name},
|
|
611
|
+
`);
|
|
612
|
+
parts.push(` ${alias.name}Values,
|
|
613
|
+
`);
|
|
614
|
+
parts.push(` is${alias.name},
|
|
615
|
+
`);
|
|
616
|
+
parts.push(` get${alias.name}Label,
|
|
617
|
+
`);
|
|
618
|
+
parts.push(` get${alias.name}Extra,
|
|
619
|
+
`);
|
|
620
|
+
parts.push(`} from './enum/${alias.name}.js';
|
|
621
|
+
`);
|
|
383
622
|
}
|
|
623
|
+
parts.push("\n");
|
|
384
624
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
types.push(iface.name);
|
|
392
|
-
}
|
|
625
|
+
parts.push(`// Models
|
|
626
|
+
`);
|
|
627
|
+
for (const schema of Object.values(schemas)) {
|
|
628
|
+
if (schema.kind === "enum") continue;
|
|
629
|
+
parts.push(`export type { ${schema.name} } from './${schema.name}.js';
|
|
630
|
+
`);
|
|
393
631
|
}
|
|
394
632
|
return {
|
|
395
|
-
|
|
396
|
-
content: parts.join("")
|
|
397
|
-
types
|
|
633
|
+
filePath: "index.ts",
|
|
634
|
+
content: parts.join(""),
|
|
635
|
+
types: [],
|
|
636
|
+
overwrite: true
|
|
398
637
|
};
|
|
399
638
|
}
|
|
400
|
-
function
|
|
639
|
+
function generateTypeScript(schemas, options = {}) {
|
|
401
640
|
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
402
641
|
const files = [];
|
|
403
642
|
const enums = generateEnums(schemas);
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
files.push({
|
|
407
|
-
fileName: "enums.ts",
|
|
408
|
-
content,
|
|
409
|
-
types: enums.map((e) => e.name)
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
|
-
const inlineEnums = extractInlineEnums(schemas);
|
|
413
|
-
if (inlineEnums.length > 0) {
|
|
414
|
-
const content = generateHeader() + inlineEnums.map(formatTypeAlias).join("\n\n") + "\n";
|
|
415
|
-
files.push({
|
|
416
|
-
fileName: "type-aliases.ts",
|
|
417
|
-
content,
|
|
418
|
-
types: inlineEnums.map((a) => a.name)
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
const interfaces = generateInterfaces(schemas, opts);
|
|
422
|
-
for (const iface of interfaces) {
|
|
423
|
-
const imports = collectImports(iface, enums, inlineEnums, interfaces);
|
|
424
|
-
const importStatement = formatImports(imports);
|
|
425
|
-
const content = generateHeader() + (importStatement ? importStatement + "\n\n" : "") + formatInterface(iface) + "\n";
|
|
426
|
-
files.push({
|
|
427
|
-
fileName: `${toKebabCase(iface.name)}.ts`,
|
|
428
|
-
content,
|
|
429
|
-
types: [iface.name]
|
|
430
|
-
});
|
|
643
|
+
for (const enumDef of enums) {
|
|
644
|
+
files.push(generateEnumFile(enumDef));
|
|
431
645
|
}
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
content: indexContent,
|
|
436
|
-
types: []
|
|
437
|
-
});
|
|
438
|
-
return files;
|
|
439
|
-
}
|
|
440
|
-
function toKebabCase(name) {
|
|
441
|
-
return name.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
|
|
442
|
-
}
|
|
443
|
-
function collectImports(iface, enums, typeAliases, allInterfaces) {
|
|
444
|
-
const imports = /* @__PURE__ */ new Map();
|
|
445
|
-
const enumNames = new Set(enums.map((e) => e.name));
|
|
446
|
-
const aliasNames = new Set(typeAliases.map((a) => a.name));
|
|
447
|
-
const interfaceNames = new Set(allInterfaces.map((i) => i.name));
|
|
448
|
-
for (const prop of iface.properties) {
|
|
449
|
-
if (enumNames.has(prop.type)) {
|
|
450
|
-
const existing = imports.get("./enums.js") ?? [];
|
|
451
|
-
if (!existing.includes(prop.type)) {
|
|
452
|
-
imports.set("./enums.js", [...existing, prop.type]);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
if (aliasNames.has(prop.type)) {
|
|
456
|
-
const existing = imports.get("./type-aliases.js") ?? [];
|
|
457
|
-
if (!existing.includes(prop.type)) {
|
|
458
|
-
imports.set("./type-aliases.js", [...existing, prop.type]);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
const baseType = prop.type.replace("[]", "");
|
|
462
|
-
if (interfaceNames.has(baseType) && baseType !== iface.name) {
|
|
463
|
-
const fileName = `./${toKebabCase(baseType)}.js`;
|
|
464
|
-
const existing = imports.get(fileName) ?? [];
|
|
465
|
-
if (!existing.includes(baseType)) {
|
|
466
|
-
imports.set(fileName, [...existing, baseType]);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
646
|
+
const typeAliases = extractInlineEnums(schemas);
|
|
647
|
+
for (const alias of typeAliases) {
|
|
648
|
+
files.push(generateTypeAliasFile(alias));
|
|
469
649
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
if (imports.size === 0) return "";
|
|
474
|
-
const lines = [];
|
|
475
|
-
for (const [path, names] of imports) {
|
|
476
|
-
lines.push(`import type { ${names.join(", ")} } from '${path}';`);
|
|
477
|
-
}
|
|
478
|
-
return lines.join("\n");
|
|
479
|
-
}
|
|
480
|
-
function generateIndexFile(files) {
|
|
481
|
-
const exports2 = [generateHeader().trim(), ""];
|
|
482
|
-
for (const file of files) {
|
|
483
|
-
if (file.fileName === "index.ts") continue;
|
|
484
|
-
const moduleName = file.fileName.replace(".ts", ".js");
|
|
485
|
-
exports2.push(`export * from './${moduleName}';`);
|
|
650
|
+
for (const schema of Object.values(schemas)) {
|
|
651
|
+
if (schema.kind === "enum") continue;
|
|
652
|
+
files.push(generateBaseInterfaceFile(schema.name, schemas, opts));
|
|
486
653
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
491
|
-
if (opts.singleFile) {
|
|
492
|
-
return [generateTypeScriptFile(schemas, opts)];
|
|
654
|
+
for (const schema of Object.values(schemas)) {
|
|
655
|
+
if (schema.kind === "enum") continue;
|
|
656
|
+
files.push(generateModelFile(schema.name));
|
|
493
657
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
function getTypeScriptPath(file, outputDir = "src/types") {
|
|
497
|
-
return `${outputDir}/${file.fileName}`;
|
|
658
|
+
files.push(generateIndexFile(schemas, enums, typeAliases));
|
|
659
|
+
return files;
|
|
498
660
|
}
|
|
499
661
|
// Annotate the CommonJS export names for ESM import in node:
|
|
500
662
|
0 && (module.exports = {
|
|
@@ -507,10 +669,8 @@ function getTypeScriptPath(file, outputDir = "src/types") {
|
|
|
507
669
|
generateEnums,
|
|
508
670
|
generateInterfaces,
|
|
509
671
|
generateTypeScript,
|
|
510
|
-
generateTypeScriptFile,
|
|
511
672
|
generateTypeScriptFiles,
|
|
512
673
|
getPropertyType,
|
|
513
|
-
getTypeScriptPath,
|
|
514
674
|
propertyToTSProperty,
|
|
515
675
|
schemaToEnum,
|
|
516
676
|
schemaToInterface,
|