@amritk/helpers 0.4.0 → 0.5.0
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.
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
+
/** Options controlling generated type output. */
|
|
3
|
+
export type TypeOptions = {
|
|
4
|
+
/** When true, every property, array, and record in the generated types is emitted as readonly. */
|
|
5
|
+
readonly readonly?: boolean;
|
|
6
|
+
};
|
|
2
7
|
/**
|
|
3
8
|
* Generates a TypeScript type definition from a JSON Schema.
|
|
4
9
|
* Handles required vs optional properties based on the schema's required array.
|
|
5
10
|
* Uses $comment as inline JSDoc description when present.
|
|
6
11
|
*/
|
|
7
|
-
export declare const generateTypeDefinition: (schema: JSONSchema, typeName: string) => string;
|
|
12
|
+
export declare const generateTypeDefinition: (schema: JSONSchema, typeName: string, options?: TypeOptions) => string;
|
|
@@ -272,12 +272,13 @@ var buildJsDocBlock = (title, description, commentUrl) => {
|
|
|
272
272
|
`;
|
|
273
273
|
return block;
|
|
274
274
|
};
|
|
275
|
-
var getTypeScriptType = (schema) => {
|
|
276
|
-
const base = getUnbrandedType(schema);
|
|
275
|
+
var getTypeScriptType = (schema, options = {}) => {
|
|
276
|
+
const base = getUnbrandedType(schema, options);
|
|
277
277
|
const brand = getMjstBrand(schema);
|
|
278
278
|
return brand ? `(${base} & { readonly __brand: '${brand}' })` : base;
|
|
279
279
|
};
|
|
280
|
-
var
|
|
280
|
+
var recordType = (keyType, valueType, options) => options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`;
|
|
281
|
+
var getUnbrandedType = (schema, options = {}) => {
|
|
281
282
|
if (typeof schema === "boolean") {
|
|
282
283
|
return getBooleanSubSchemaType(schema);
|
|
283
284
|
}
|
|
@@ -325,29 +326,29 @@ var getUnbrandedType = (schema) => {
|
|
|
325
326
|
return multiEnumUnion;
|
|
326
327
|
}
|
|
327
328
|
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
328
|
-
let oneOfUnion = getTypeScriptType(schema.oneOf[0]);
|
|
329
|
+
let oneOfUnion = getTypeScriptType(schema.oneOf[0], options);
|
|
329
330
|
for (let i = 1;i < schema.oneOf.length; i++) {
|
|
330
|
-
oneOfUnion += " | " + getTypeScriptType(schema.oneOf[i]);
|
|
331
|
+
oneOfUnion += " | " + getTypeScriptType(schema.oneOf[i], options);
|
|
331
332
|
}
|
|
332
333
|
return oneOfUnion;
|
|
333
334
|
}
|
|
334
335
|
if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
335
|
-
let anyOfUnion = getTypeScriptType(schema.anyOf[0]);
|
|
336
|
+
let anyOfUnion = getTypeScriptType(schema.anyOf[0], options);
|
|
336
337
|
for (let i = 1;i < schema.anyOf.length; i++) {
|
|
337
|
-
anyOfUnion += " | " + getTypeScriptType(schema.anyOf[i]);
|
|
338
|
+
anyOfUnion += " | " + getTypeScriptType(schema.anyOf[i], options);
|
|
338
339
|
}
|
|
339
340
|
return anyOfUnion;
|
|
340
341
|
}
|
|
341
342
|
if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length > 0) {
|
|
342
|
-
let intersectionTypes = getTypeScriptType(schema.allOf[0]);
|
|
343
|
+
let intersectionTypes = getTypeScriptType(schema.allOf[0], options);
|
|
343
344
|
for (let i = 1;i < schema.allOf.length; i++) {
|
|
344
|
-
intersectionTypes += " & " + getTypeScriptType(schema.allOf[i]);
|
|
345
|
+
intersectionTypes += " & " + getTypeScriptType(schema.allOf[i], options);
|
|
345
346
|
}
|
|
346
347
|
return intersectionTypes;
|
|
347
348
|
}
|
|
348
349
|
const conditionalResult = getConditionalObjectSchema(schema);
|
|
349
350
|
if (conditionalResult) {
|
|
350
|
-
const baseType = getTypeScriptType(conditionalResult.schema);
|
|
351
|
+
const baseType = getTypeScriptType(conditionalResult.schema, options);
|
|
351
352
|
if (conditionalResult.thenRef) {
|
|
352
353
|
return `(${baseType}) & ${refToName(conditionalResult.thenRef)}`;
|
|
353
354
|
}
|
|
@@ -356,20 +357,20 @@ var getUnbrandedType = (schema) => {
|
|
|
356
357
|
if (!schema.type) {
|
|
357
358
|
if (schema.additionalProperties !== undefined) {
|
|
358
359
|
if (typeof schema.additionalProperties === "boolean") {
|
|
359
|
-
return
|
|
360
|
+
return recordType("string", getBooleanSubSchemaType(schema.additionalProperties), options);
|
|
360
361
|
}
|
|
361
|
-
return
|
|
362
|
+
return recordType("string", getTypeScriptType(schema.additionalProperties, options), options);
|
|
362
363
|
}
|
|
363
364
|
if (schema.patternProperties && typeof schema.patternProperties === "object") {
|
|
364
365
|
const firstEntry = Object.entries(schema.patternProperties)[0];
|
|
365
366
|
if (firstEntry) {
|
|
366
367
|
const [pattern, value] = firstEntry;
|
|
367
368
|
if (value !== undefined) {
|
|
368
|
-
const valueType = typeof value === "boolean" ? getBooleanSubSchemaType(value) : getTypeScriptType(value);
|
|
369
|
+
const valueType = typeof value === "boolean" ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options);
|
|
369
370
|
if (pattern === "^x-") {
|
|
370
|
-
return `
|
|
371
|
+
return recordType("`x-${string}`", valueType, options);
|
|
371
372
|
}
|
|
372
|
-
return
|
|
373
|
+
return recordType("string", valueType, options);
|
|
373
374
|
}
|
|
374
375
|
}
|
|
375
376
|
}
|
|
@@ -422,41 +423,42 @@ var getUnbrandedType = (schema) => {
|
|
|
422
423
|
return "boolean";
|
|
423
424
|
case "array":
|
|
424
425
|
if (schema.items) {
|
|
425
|
-
const itemType = getTypeScriptType(schema.items);
|
|
426
|
+
const itemType = getTypeScriptType(schema.items, options);
|
|
426
427
|
const wrappedItemType = itemType.includes(" | ") ? `(${itemType})` : itemType;
|
|
427
|
-
return `${wrappedItemType}[]`;
|
|
428
|
+
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`;
|
|
428
429
|
}
|
|
429
|
-
return "unknown[]";
|
|
430
|
+
return options.readonly ? "readonly unknown[]" : "unknown[]";
|
|
430
431
|
case "object":
|
|
431
432
|
if (schema.properties) {
|
|
433
|
+
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
432
434
|
let properties = "";
|
|
433
435
|
let first = true;
|
|
434
436
|
for (const key in schema.properties) {
|
|
435
437
|
const propSchema = schema.properties[key];
|
|
436
438
|
const isRequired = schema.required?.includes(key) ?? false;
|
|
437
439
|
const optional = isRequired ? "" : "?";
|
|
438
|
-
const propType = getTypeScriptType(propSchema);
|
|
440
|
+
const propType = getTypeScriptType(propSchema, options);
|
|
439
441
|
if (!first)
|
|
440
442
|
properties += "; ";
|
|
441
|
-
properties += safeKey(key) + optional + ": " + propType;
|
|
443
|
+
properties += readonlyPrefix + safeKey(key) + optional + ": " + propType;
|
|
442
444
|
first = false;
|
|
443
445
|
}
|
|
444
446
|
return "{ " + properties + " }";
|
|
445
447
|
}
|
|
446
448
|
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
447
|
-
const additionalPropType = getTypeScriptType(schema.additionalProperties);
|
|
448
|
-
return
|
|
449
|
+
const additionalPropType = getTypeScriptType(schema.additionalProperties, options);
|
|
450
|
+
return recordType("string", additionalPropType, options);
|
|
449
451
|
}
|
|
450
452
|
if (schema.patternProperties && typeof schema.patternProperties === "object") {
|
|
451
453
|
const firstEntry = Object.entries(schema.patternProperties)[0];
|
|
452
454
|
if (firstEntry) {
|
|
453
455
|
const [pattern, patternVal] = firstEntry;
|
|
454
456
|
if (patternVal) {
|
|
455
|
-
const valueType = getTypeScriptType(patternVal);
|
|
457
|
+
const valueType = getTypeScriptType(patternVal, options);
|
|
456
458
|
if (pattern === "^x-") {
|
|
457
|
-
return `
|
|
459
|
+
return recordType("`x-${string}`", valueType, options);
|
|
458
460
|
}
|
|
459
|
-
return
|
|
461
|
+
return recordType("string", valueType, options);
|
|
460
462
|
}
|
|
461
463
|
}
|
|
462
464
|
}
|
|
@@ -465,9 +467,10 @@ var getUnbrandedType = (schema) => {
|
|
|
465
467
|
return "unknown";
|
|
466
468
|
}
|
|
467
469
|
};
|
|
468
|
-
var generateTypeDefinition = (schema, typeName) => {
|
|
470
|
+
var generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
471
|
+
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
469
472
|
if (!isObjectLikeSchema(schema)) {
|
|
470
|
-
const tsType = getTypeScriptType(schema);
|
|
473
|
+
const tsType = getTypeScriptType(schema, options);
|
|
471
474
|
let result = "";
|
|
472
475
|
if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === "string") {
|
|
473
476
|
result += buildJsDocBlock(typeName, schema.$comment);
|
|
@@ -495,23 +498,23 @@ var generateTypeDefinition = (schema, typeName) => {
|
|
|
495
498
|
if (firstPatternProperty === undefined) {
|
|
496
499
|
return `export type ${typeName} = Record<string, unknown>;`;
|
|
497
500
|
}
|
|
498
|
-
const patternPropType = typeof firstPatternProperty === "boolean" ? getBooleanSubSchemaType(firstPatternProperty) : getTypeScriptType(firstPatternProperty);
|
|
501
|
+
const patternPropType = typeof firstPatternProperty === "boolean" ? getBooleanSubSchemaType(firstPatternProperty) : getTypeScriptType(firstPatternProperty, options);
|
|
499
502
|
const keyType = firstPattern === "^x-" ? "`x-${string}`" : "string";
|
|
500
503
|
let result2 = "";
|
|
501
504
|
if (jsDocTitle && jsDocDescription) {
|
|
502
505
|
result2 += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
503
506
|
}
|
|
504
|
-
result2 += `export type ${typeName} =
|
|
507
|
+
result2 += `export type ${typeName} = ${recordType(keyType, patternPropType, options)};`;
|
|
505
508
|
return result2;
|
|
506
509
|
}
|
|
507
510
|
if (!hasProperties2 && hasAdditionalProperties2 && normalizedSchema.additionalProperties) {
|
|
508
|
-
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties);
|
|
511
|
+
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties, options);
|
|
509
512
|
let result2 = "";
|
|
510
513
|
if (jsDocTitle && jsDocDescription) {
|
|
511
514
|
result2 += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
512
515
|
}
|
|
513
516
|
result2 += `export type ${typeName} = {
|
|
514
|
-
[key: string]: ${additionalPropType};
|
|
517
|
+
${readonlyPrefix}[key: string]: ${additionalPropType};
|
|
515
518
|
};`;
|
|
516
519
|
return result2;
|
|
517
520
|
}
|
|
@@ -522,8 +525,8 @@ var generateTypeDefinition = (schema, typeName) => {
|
|
|
522
525
|
const propSchema = schemaProps[key];
|
|
523
526
|
const isRequired = normalizedSchema.required?.includes(key) ?? false;
|
|
524
527
|
const optional = isRequired ? "" : "?";
|
|
525
|
-
const propType = getTypeScriptType(propSchema);
|
|
526
|
-
const quotedKey = safeKey(key);
|
|
528
|
+
const propType = getTypeScriptType(propSchema, options);
|
|
529
|
+
const quotedKey = readonlyPrefix + safeKey(key);
|
|
527
530
|
if (!isFirstProp)
|
|
528
531
|
properties += `
|
|
529
532
|
`;
|
package/package.json
CHANGED
|
@@ -1270,4 +1270,48 @@ describe('generateTypeDefinition', () => {
|
|
|
1270
1270
|
expect(result).not.toContain('doEvil')
|
|
1271
1271
|
expect(result).toBe('export type Bad = string;')
|
|
1272
1272
|
})
|
|
1273
|
+
|
|
1274
|
+
describe('readonly option', () => {
|
|
1275
|
+
it('marks every property as readonly, deeply', () => {
|
|
1276
|
+
const schema: JSONSchema = {
|
|
1277
|
+
type: 'object',
|
|
1278
|
+
properties: {
|
|
1279
|
+
id: { type: 'string' },
|
|
1280
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
1281
|
+
nested: {
|
|
1282
|
+
type: 'object',
|
|
1283
|
+
properties: { value: { type: 'number' } },
|
|
1284
|
+
},
|
|
1285
|
+
},
|
|
1286
|
+
required: ['id'],
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
const result = generateTypeDefinition(schema, 'Doc', { readonly: true })
|
|
1290
|
+
|
|
1291
|
+
expect(result).toContain('readonly id: string;')
|
|
1292
|
+
expect(result).toContain('readonly tags?: readonly string[];')
|
|
1293
|
+
expect(result).toContain('readonly nested?: { readonly value?: number }')
|
|
1294
|
+
})
|
|
1295
|
+
|
|
1296
|
+
it('wraps record types in Readonly', () => {
|
|
1297
|
+
const schema: JSONSchema = {
|
|
1298
|
+
type: 'object',
|
|
1299
|
+
additionalProperties: { type: 'number' },
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
expect(generateTypeDefinition(schema, 'Map', { readonly: true })).toContain('readonly [key: string]: number;')
|
|
1303
|
+
})
|
|
1304
|
+
|
|
1305
|
+
it('leaves output unchanged when readonly is not set', () => {
|
|
1306
|
+
const schema: JSONSchema = {
|
|
1307
|
+
type: 'object',
|
|
1308
|
+
properties: { id: { type: 'string' } },
|
|
1309
|
+
required: ['id'],
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
const result = generateTypeDefinition(schema, 'Doc')
|
|
1313
|
+
expect(result).not.toContain('readonly')
|
|
1314
|
+
expect(result).toContain('id: string;')
|
|
1315
|
+
})
|
|
1316
|
+
})
|
|
1273
1317
|
})
|
|
@@ -10,6 +10,12 @@ type ConditionalObjectResult = {
|
|
|
10
10
|
thenRef: string | null
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** Options controlling generated type output. */
|
|
14
|
+
export type TypeOptions = {
|
|
15
|
+
/** When true, every property, array, and record in the generated types is emitted as readonly. */
|
|
16
|
+
readonly readonly?: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
const getConditionalObjectSchema = (schema: JSONSchema): ConditionalObjectResult | null => {
|
|
14
20
|
if (!isSchemaObject(schema)) {
|
|
15
21
|
return null
|
|
@@ -113,17 +119,21 @@ const buildJsDocBlock = (title: string, description: string, commentUrl?: string
|
|
|
113
119
|
* type and intersect it with a unique brand marker. This is the recursion entry
|
|
114
120
|
* point, so branded nested fields are wrapped too.
|
|
115
121
|
*/
|
|
116
|
-
const getTypeScriptType = (schema: JSONSchema): string => {
|
|
117
|
-
const base = getUnbrandedType(schema)
|
|
122
|
+
const getTypeScriptType = (schema: JSONSchema, options: TypeOptions = {}): string => {
|
|
123
|
+
const base = getUnbrandedType(schema, options)
|
|
118
124
|
const brand = getMjstBrand(schema)
|
|
119
125
|
return brand ? `(${base} & { readonly __brand: '${brand}' })` : base
|
|
120
126
|
}
|
|
121
127
|
|
|
128
|
+
/** Wraps a `Record<...>` in `Readonly<...>` when readonly output is requested. */
|
|
129
|
+
const recordType = (keyType: string, valueType: string, options: TypeOptions): string =>
|
|
130
|
+
options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`
|
|
131
|
+
|
|
122
132
|
/**
|
|
123
133
|
* Converts a JSON Schema type to its TypeScript equivalent, ignoring any brand.
|
|
124
134
|
* Recursively handles nested objects and arrays.
|
|
125
135
|
*/
|
|
126
|
-
const getUnbrandedType = (schema: JSONSchema): string => {
|
|
136
|
+
const getUnbrandedType = (schema: JSONSchema, options: TypeOptions = {}): string => {
|
|
127
137
|
// Boolean schema: `true` means any value is valid (unknown), `false` means no value is valid (never)
|
|
128
138
|
if (typeof schema === 'boolean') {
|
|
129
139
|
return getBooleanSubSchemaType(schema)
|
|
@@ -195,17 +205,17 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
195
205
|
// Handle union types (oneOf, anyOf) - check this before returning unknown
|
|
196
206
|
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
197
207
|
// schema.oneOf[0] is safe: we guard with .length > 0 above
|
|
198
|
-
let oneOfUnion = getTypeScriptType(schema.oneOf[0]
|
|
208
|
+
let oneOfUnion = getTypeScriptType(schema.oneOf[0]!, options)
|
|
199
209
|
for (let i = 1; i < schema.oneOf.length; i++) {
|
|
200
|
-
oneOfUnion += ' | ' + getTypeScriptType(schema.oneOf[i]
|
|
210
|
+
oneOfUnion += ' | ' + getTypeScriptType(schema.oneOf[i]!, options)
|
|
201
211
|
}
|
|
202
212
|
return oneOfUnion
|
|
203
213
|
}
|
|
204
214
|
if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
205
215
|
// schema.anyOf[0] is safe: we guard with .length > 0 above
|
|
206
|
-
let anyOfUnion = getTypeScriptType(schema.anyOf[0]
|
|
216
|
+
let anyOfUnion = getTypeScriptType(schema.anyOf[0]!, options)
|
|
207
217
|
for (let i = 1; i < schema.anyOf.length; i++) {
|
|
208
|
-
anyOfUnion += ' | ' + getTypeScriptType(schema.anyOf[i]
|
|
218
|
+
anyOfUnion += ' | ' + getTypeScriptType(schema.anyOf[i]!, options)
|
|
209
219
|
}
|
|
210
220
|
return anyOfUnion
|
|
211
221
|
}
|
|
@@ -213,9 +223,9 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
213
223
|
// Handle allOf (intersection types)
|
|
214
224
|
if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length > 0) {
|
|
215
225
|
// schema.allOf[0] is safe: we guard with .length > 0 above
|
|
216
|
-
let intersectionTypes = getTypeScriptType(schema.allOf[0]
|
|
226
|
+
let intersectionTypes = getTypeScriptType(schema.allOf[0]!, options)
|
|
217
227
|
for (let i = 1; i < schema.allOf.length; i++) {
|
|
218
|
-
intersectionTypes += ' & ' + getTypeScriptType(schema.allOf[i]
|
|
228
|
+
intersectionTypes += ' & ' + getTypeScriptType(schema.allOf[i]!, options)
|
|
219
229
|
}
|
|
220
230
|
return intersectionTypes
|
|
221
231
|
}
|
|
@@ -223,7 +233,7 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
223
233
|
// Handle object-like conditional schemas that use if/then without declaring type
|
|
224
234
|
const conditionalResult = getConditionalObjectSchema(schema)
|
|
225
235
|
if (conditionalResult) {
|
|
226
|
-
const baseType = getTypeScriptType(conditionalResult.schema)
|
|
236
|
+
const baseType = getTypeScriptType(conditionalResult.schema, options)
|
|
227
237
|
if (conditionalResult.thenRef) {
|
|
228
238
|
return `(${baseType}) & ${refToName(conditionalResult.thenRef)}`
|
|
229
239
|
}
|
|
@@ -234,9 +244,9 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
234
244
|
if (!schema.type) {
|
|
235
245
|
if (schema.additionalProperties !== undefined) {
|
|
236
246
|
if (typeof schema.additionalProperties === 'boolean') {
|
|
237
|
-
return
|
|
247
|
+
return recordType('string', getBooleanSubSchemaType(schema.additionalProperties), options)
|
|
238
248
|
}
|
|
239
|
-
return
|
|
249
|
+
return recordType('string', getTypeScriptType(schema.additionalProperties, options), options)
|
|
240
250
|
}
|
|
241
251
|
|
|
242
252
|
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
@@ -244,13 +254,14 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
244
254
|
if (firstEntry) {
|
|
245
255
|
const [pattern, value] = firstEntry
|
|
246
256
|
if (value !== undefined) {
|
|
247
|
-
const valueType =
|
|
257
|
+
const valueType =
|
|
258
|
+
typeof value === 'boolean' ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options)
|
|
248
259
|
// The ^x- pattern is a common JSON Schema convention for vendor extensions
|
|
249
260
|
// that maps naturally to the TypeScript template literal `x-${string}`
|
|
250
261
|
if (pattern === '^x-') {
|
|
251
|
-
return `
|
|
262
|
+
return recordType('`x-${string}`', valueType, options)
|
|
252
263
|
}
|
|
253
|
-
return
|
|
264
|
+
return recordType('string', valueType, options)
|
|
254
265
|
}
|
|
255
266
|
}
|
|
256
267
|
}
|
|
@@ -315,16 +326,17 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
315
326
|
// Array
|
|
316
327
|
case 'array':
|
|
317
328
|
if (schema.items) {
|
|
318
|
-
const itemType = getTypeScriptType(schema.items)
|
|
329
|
+
const itemType = getTypeScriptType(schema.items, options)
|
|
319
330
|
// Wrap union types in parentheses so `(A | B)[]` is not misread as `A | B[]`
|
|
320
331
|
const wrappedItemType = itemType.includes(' | ') ? `(${itemType})` : itemType
|
|
321
|
-
return `${wrappedItemType}[]`
|
|
332
|
+
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`
|
|
322
333
|
}
|
|
323
|
-
return 'unknown[]'
|
|
334
|
+
return options.readonly ? 'readonly unknown[]' : 'unknown[]'
|
|
324
335
|
|
|
325
336
|
// Object
|
|
326
337
|
case 'object':
|
|
327
338
|
if (schema.properties) {
|
|
339
|
+
const readonlyPrefix = options.readonly ? 'readonly ' : ''
|
|
328
340
|
let properties = ''
|
|
329
341
|
let first = true
|
|
330
342
|
for (const key in schema.properties) {
|
|
@@ -332,17 +344,17 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
332
344
|
const propSchema = schema.properties[key]!
|
|
333
345
|
const isRequired = schema.required?.includes(key) ?? false
|
|
334
346
|
const optional = isRequired ? '' : '?'
|
|
335
|
-
const propType = getTypeScriptType(propSchema)
|
|
347
|
+
const propType = getTypeScriptType(propSchema, options)
|
|
336
348
|
if (!first) properties += '; '
|
|
337
|
-
properties += safeKey(key) + optional + ': ' + propType
|
|
349
|
+
properties += readonlyPrefix + safeKey(key) + optional + ': ' + propType
|
|
338
350
|
first = false
|
|
339
351
|
}
|
|
340
352
|
return '{ ' + properties + ' }'
|
|
341
353
|
}
|
|
342
354
|
// Handle additionalProperties with $ref or $dynamicRef
|
|
343
355
|
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
|
|
344
|
-
const additionalPropType = getTypeScriptType(schema.additionalProperties)
|
|
345
|
-
return
|
|
356
|
+
const additionalPropType = getTypeScriptType(schema.additionalProperties, options)
|
|
357
|
+
return recordType('string', additionalPropType, options)
|
|
346
358
|
}
|
|
347
359
|
// Handle patternProperties as a Record type
|
|
348
360
|
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
@@ -350,11 +362,11 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
350
362
|
if (firstEntry) {
|
|
351
363
|
const [pattern, patternVal] = firstEntry
|
|
352
364
|
if (patternVal) {
|
|
353
|
-
const valueType = getTypeScriptType(patternVal)
|
|
365
|
+
const valueType = getTypeScriptType(patternVal, options)
|
|
354
366
|
if (pattern === '^x-') {
|
|
355
|
-
return `
|
|
367
|
+
return recordType('`x-${string}`', valueType, options)
|
|
356
368
|
}
|
|
357
|
-
return
|
|
369
|
+
return recordType('string', valueType, options)
|
|
358
370
|
}
|
|
359
371
|
}
|
|
360
372
|
}
|
|
@@ -371,10 +383,12 @@ const getUnbrandedType = (schema: JSONSchema): string => {
|
|
|
371
383
|
* Handles required vs optional properties based on the schema's required array.
|
|
372
384
|
* Uses $comment as inline JSDoc description when present.
|
|
373
385
|
*/
|
|
374
|
-
export const generateTypeDefinition = (schema: JSONSchema, typeName: string): string => {
|
|
386
|
+
export const generateTypeDefinition = (schema: JSONSchema, typeName: string, options: TypeOptions = {}): string => {
|
|
387
|
+
const readonlyPrefix = options.readonly ? 'readonly ' : ''
|
|
388
|
+
|
|
375
389
|
// Handle non-object schemas first
|
|
376
390
|
if (!isObjectLikeSchema(schema)) {
|
|
377
|
-
const tsType = getTypeScriptType(schema)
|
|
391
|
+
const tsType = getTypeScriptType(schema, options)
|
|
378
392
|
let result = ''
|
|
379
393
|
|
|
380
394
|
if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === 'string') {
|
|
@@ -418,7 +432,7 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string): st
|
|
|
418
432
|
const patternPropType =
|
|
419
433
|
typeof firstPatternProperty === 'boolean'
|
|
420
434
|
? getBooleanSubSchemaType(firstPatternProperty)
|
|
421
|
-
: getTypeScriptType(firstPatternProperty)
|
|
435
|
+
: getTypeScriptType(firstPatternProperty, options)
|
|
422
436
|
|
|
423
437
|
// The ^x- pattern is a common JSON Schema convention for vendor extensions
|
|
424
438
|
// that maps naturally to the TypeScript template literal `x-${string}`
|
|
@@ -428,20 +442,20 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string): st
|
|
|
428
442
|
if (jsDocTitle && jsDocDescription) {
|
|
429
443
|
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
430
444
|
}
|
|
431
|
-
result += `export type ${typeName} =
|
|
445
|
+
result += `export type ${typeName} = ${recordType(keyType, patternPropType, options)};`
|
|
432
446
|
|
|
433
447
|
return result
|
|
434
448
|
}
|
|
435
449
|
|
|
436
450
|
// Handle objects with only additionalProperties (no fixed properties)
|
|
437
451
|
if (!hasProperties && hasAdditionalProperties && normalizedSchema.additionalProperties) {
|
|
438
|
-
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties)
|
|
452
|
+
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties, options)
|
|
439
453
|
|
|
440
454
|
let result = ''
|
|
441
455
|
if (jsDocTitle && jsDocDescription) {
|
|
442
456
|
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
443
457
|
}
|
|
444
|
-
result += `export type ${typeName} = {\n [key: string]: ${additionalPropType};\n};`
|
|
458
|
+
result += `export type ${typeName} = {\n ${readonlyPrefix}[key: string]: ${additionalPropType};\n};`
|
|
445
459
|
|
|
446
460
|
return result
|
|
447
461
|
}
|
|
@@ -454,8 +468,8 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string): st
|
|
|
454
468
|
const propSchema = schemaProps[key]!
|
|
455
469
|
const isRequired = normalizedSchema.required?.includes(key) ?? false
|
|
456
470
|
const optional = isRequired ? '' : '?'
|
|
457
|
-
const propType = getTypeScriptType(propSchema)
|
|
458
|
-
const quotedKey = safeKey(key)
|
|
471
|
+
const propType = getTypeScriptType(propSchema, options)
|
|
472
|
+
const quotedKey = readonlyPrefix + safeKey(key)
|
|
459
473
|
|
|
460
474
|
if (!isFirstProp) properties += '\n'
|
|
461
475
|
isFirstProp = false
|