@amritk/helpers 0.6.2 → 0.7.1
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/build-dynamic-ref-map.d.ts +1 -0
- package/dist/build-dynamic-ref-map.d.ts.map +1 -0
- package/dist/build-dynamic-ref-map.js +31 -107
- package/dist/derive-root-type-name.d.ts +1 -0
- package/dist/derive-root-type-name.d.ts.map +1 -0
- package/dist/derive-root-type-name.js +37 -17
- package/dist/extract-dynamic-anchor-defs.d.ts +20 -0
- package/dist/extract-dynamic-anchor-defs.d.ts.map +1 -0
- package/dist/extract-dynamic-anchor-defs.js +30 -0
- package/dist/extract-refs.d.ts +1 -0
- package/dist/extract-refs.d.ts.map +1 -0
- package/dist/extract-refs.js +64 -33
- package/dist/generate-index-barrel.d.ts +29 -0
- package/dist/generate-index-barrel.d.ts.map +1 -0
- package/dist/generate-index-barrel.js +43 -0
- package/dist/generate-type-definition.d.ts +1 -0
- package/dist/generate-type-definition.d.ts.map +1 -0
- package/dist/generate-type-definition.js +449 -567
- package/dist/has-ref.d.ts +1 -0
- package/dist/has-ref.d.ts.map +1 -0
- package/dist/has-ref.js +7 -6
- package/dist/is-object.d.ts +1 -0
- package/dist/is-object.d.ts.map +1 -0
- package/dist/is-object.js +14 -5
- package/dist/mjst-extension.d.ts +1 -0
- package/dist/mjst-extension.d.ts.map +1 -0
- package/dist/mjst-extension.js +50 -122
- package/dist/parse-documentation.d.ts +1 -0
- package/dist/parse-documentation.d.ts.map +1 -0
- package/dist/parse-documentation.js +124 -87
- package/dist/ref-to-filename.d.ts +1 -0
- package/dist/ref-to-filename.d.ts.map +1 -0
- package/dist/ref-to-filename.js +106 -42
- package/dist/ref-to-name.d.ts +1 -0
- package/dist/ref-to-name.d.ts.map +1 -0
- package/dist/ref-to-name.js +39 -53
- package/dist/resolve-dynamic-refs.d.ts +1 -0
- package/dist/resolve-dynamic-refs.d.ts.map +1 -0
- package/dist/resolve-dynamic-refs.js +36 -28
- package/dist/resolve-ref.d.ts +1 -0
- package/dist/resolve-ref.d.ts.map +1 -0
- package/dist/resolve-ref.js +74 -39
- package/dist/safe-accessor.d.ts +1 -0
- package/dist/safe-accessor.d.ts.map +1 -0
- package/dist/safe-accessor.js +42 -19
- package/dist/schema-guards.d.ts +2 -1
- package/dist/schema-guards.d.ts.map +1 -0
- package/dist/schema-guards.js +119 -120
- package/dist/upgrade-draft07-schema.d.ts +1 -0
- package/dist/upgrade-draft07-schema.d.ts.map +1 -0
- package/dist/upgrade-draft07-schema.js +152 -130
- package/dist/validate-array.d.ts +1 -0
- package/dist/validate-array.d.ts.map +1 -0
- package/dist/validate-array.js +12 -14
- package/dist/validate-record.d.ts +1 -0
- package/dist/validate-record.d.ts.map +1 -0
- package/dist/validate-record.js +15 -17
- package/dist/walk-ref-graph.d.ts +54 -0
- package/dist/walk-ref-graph.d.ts.map +1 -0
- package/dist/walk-ref-graph.js +123 -0
- package/package.json +9 -88
- package/src/extract-dynamic-anchor-defs.test.ts +29 -0
- package/src/extract-dynamic-anchor-defs.ts +33 -0
- package/src/generate-index-barrel.test.ts +41 -0
- package/src/generate-index-barrel.ts +62 -0
- package/src/walk-ref-graph.test.ts +118 -0
- package/src/walk-ref-graph.ts +184 -0
|
@@ -1,600 +1,482 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
|
|
9
|
-
};
|
|
10
|
-
var hasType = (schema) => {
|
|
11
|
-
return isSchemaObject(schema) && "type" in schema && typeof schema.type === "string";
|
|
12
|
-
};
|
|
13
|
-
var isObjectSchema = (schema) => {
|
|
14
|
-
return isSchemaObject(schema) && (("type" in schema) && schema.type === "object" || ("properties" in schema));
|
|
15
|
-
};
|
|
16
|
-
var hasProperties = (schema) => {
|
|
17
|
-
return isSchemaObject(schema) && "properties" in schema && typeof schema.properties === "object" && schema.properties !== null;
|
|
18
|
-
};
|
|
19
|
-
var hasEnum = (schema) => {
|
|
20
|
-
return isSchemaObject(schema) && "enum" in schema && Array.isArray(schema.enum);
|
|
21
|
-
};
|
|
22
|
-
var hasConst = (schema) => {
|
|
23
|
-
return isSchemaObject(schema) && "const" in schema;
|
|
24
|
-
};
|
|
25
|
-
var hasPattern = (schema) => {
|
|
26
|
-
return isSchemaObject(schema) && "pattern" in schema && typeof schema.pattern === "string";
|
|
27
|
-
};
|
|
28
|
-
var hasFormat = (schema) => {
|
|
29
|
-
return isSchemaObject(schema) && "format" in schema && typeof schema.format === "string";
|
|
30
|
-
};
|
|
31
|
-
var hasDefault = (schema) => {
|
|
32
|
-
return isSchemaObject(schema) && "default" in schema;
|
|
33
|
-
};
|
|
34
|
-
var hasExamples = (schema) => {
|
|
35
|
-
return isSchemaObject(schema) && "examples" in schema && Array.isArray(schema.examples);
|
|
36
|
-
};
|
|
37
|
-
var hasOneOf = (schema) => {
|
|
38
|
-
return isSchemaObject(schema) && "oneOf" in schema && Array.isArray(schema.oneOf);
|
|
39
|
-
};
|
|
40
|
-
var hasAnyOf = (schema) => {
|
|
41
|
-
return isSchemaObject(schema) && "anyOf" in schema && Array.isArray(schema.anyOf);
|
|
42
|
-
};
|
|
43
|
-
var hasAllOf = (schema) => {
|
|
44
|
-
return isSchemaObject(schema) && "allOf" in schema && Array.isArray(schema.allOf);
|
|
45
|
-
};
|
|
46
|
-
var hasRequired = (schema) => {
|
|
47
|
-
return isSchemaObject(schema) && "required" in schema && Array.isArray(schema.required);
|
|
48
|
-
};
|
|
49
|
-
var hasItems = (schema) => {
|
|
50
|
-
return isSchemaObject(schema) && "items" in schema && typeof schema.items === "object" && schema.items !== null && typeof schema.items !== "boolean";
|
|
51
|
-
};
|
|
52
|
-
var hasAdditionalProperties = (schema) => {
|
|
53
|
-
return isSchemaObject(schema) && "additionalProperties" in schema;
|
|
54
|
-
};
|
|
55
|
-
var hasMinLength = (schema) => {
|
|
56
|
-
return isSchemaObject(schema) && "minLength" in schema && typeof schema.minLength === "number";
|
|
57
|
-
};
|
|
58
|
-
var hasMaxLength = (schema) => {
|
|
59
|
-
return isSchemaObject(schema) && "maxLength" in schema && typeof schema.maxLength === "number";
|
|
60
|
-
};
|
|
61
|
-
var hasMinimum = (schema) => {
|
|
62
|
-
return isSchemaObject(schema) && "minimum" in schema && typeof schema.minimum === "number";
|
|
63
|
-
};
|
|
64
|
-
var hasMaximum = (schema) => {
|
|
65
|
-
return isSchemaObject(schema) && "maximum" in schema && typeof schema.maximum === "number";
|
|
66
|
-
};
|
|
67
|
-
var hasExclusiveMinimum = (schema) => {
|
|
68
|
-
return isSchemaObject(schema) && "exclusiveMinimum" in schema && typeof schema.exclusiveMinimum === "number";
|
|
69
|
-
};
|
|
70
|
-
var hasExclusiveMaximum = (schema) => {
|
|
71
|
-
return isSchemaObject(schema) && "exclusiveMaximum" in schema && typeof schema.exclusiveMaximum === "number";
|
|
72
|
-
};
|
|
73
|
-
var hasMultipleOf = (schema) => {
|
|
74
|
-
return isSchemaObject(schema) && "multipleOf" in schema && typeof schema.multipleOf === "number";
|
|
75
|
-
};
|
|
76
|
-
var hasMinItems = (schema) => {
|
|
77
|
-
return isSchemaObject(schema) && "minItems" in schema && typeof schema.minItems === "number";
|
|
78
|
-
};
|
|
79
|
-
var hasMaxItems = (schema) => {
|
|
80
|
-
return isSchemaObject(schema) && "maxItems" in schema && typeof schema.maxItems === "number";
|
|
81
|
-
};
|
|
82
|
-
var hasUniqueItems = (schema) => {
|
|
83
|
-
return isSchemaObject(schema) && "uniqueItems" in schema && typeof schema.uniqueItems === "boolean";
|
|
84
|
-
};
|
|
85
|
-
var hasMinProperties = (schema) => {
|
|
86
|
-
return isSchemaObject(schema) && "minProperties" in schema && typeof schema.minProperties === "number";
|
|
87
|
-
};
|
|
88
|
-
var hasMaxProperties = (schema) => {
|
|
89
|
-
return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// src/mjst-extension.ts
|
|
93
|
-
var MJST_EXTENSION_KEY = "x-mjst";
|
|
94
|
-
var IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
95
|
-
var SUPPORTED_PRIMITIVES = new Set(["bigint"]);
|
|
96
|
-
var SAFE_BRAND = /^[\w$ -]+$/;
|
|
97
|
-
var readExtensionString = (schema, field) => {
|
|
98
|
-
if (!isSchemaObject(schema))
|
|
99
|
-
return;
|
|
100
|
-
const extension = schema[MJST_EXTENSION_KEY];
|
|
101
|
-
if (typeof extension !== "object" || extension === null)
|
|
102
|
-
return;
|
|
103
|
-
const value = extension[field];
|
|
104
|
-
return typeof value === "string" ? value : undefined;
|
|
105
|
-
};
|
|
106
|
-
var getMjstInstanceOf = (schema) => {
|
|
107
|
-
const instanceOf = readExtensionString(schema, "instanceOf");
|
|
108
|
-
return instanceOf !== undefined && IDENTIFIER.test(instanceOf) ? instanceOf : undefined;
|
|
109
|
-
};
|
|
110
|
-
var getMjstPrimitive = (schema) => {
|
|
111
|
-
const primitive = readExtensionString(schema, "primitive");
|
|
112
|
-
return primitive !== undefined && SUPPORTED_PRIMITIVES.has(primitive) ? primitive : undefined;
|
|
113
|
-
};
|
|
114
|
-
var getMjstBrand = (schema) => {
|
|
115
|
-
const brand = readExtensionString(schema, "brand");
|
|
116
|
-
return brand !== undefined && SAFE_BRAND.test(brand) ? brand : undefined;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
// src/ref-to-filename.ts
|
|
120
|
-
var toKebabCase = (value) => value.replace(/OAuth/g, "Oauth").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").replace(/([a-z\d])([A-Z])/g, "$1-$2").toLowerCase();
|
|
121
|
-
var uriRefToFilename = (uri) => {
|
|
122
|
-
const hashIndex = uri.indexOf("#");
|
|
123
|
-
const baseUri = hashIndex === -1 ? uri : uri.slice(0, hashIndex);
|
|
124
|
-
const fragment = hashIndex === -1 ? "" : uri.slice(hashIndex + 1);
|
|
125
|
-
const withoutProtocol = baseUri.replace(/^https?:\/\/[^/]+\//, "");
|
|
126
|
-
const withoutExt = withoutProtocol.replace(/\.json$/, "");
|
|
127
|
-
const rawSegments = withoutExt.split("/");
|
|
128
|
-
const SKIP_KEYS = new Set(["definitions", "$defs"]);
|
|
129
|
-
const segments = [];
|
|
130
|
-
for (let i = 0;i < rawSegments.length; i++) {
|
|
131
|
-
const s = rawSegments[i];
|
|
132
|
-
if (SKIP_KEYS.has(s))
|
|
133
|
-
continue;
|
|
134
|
-
const prevRaw = rawSegments[i - 1];
|
|
135
|
-
if (/^\d+\.\d+/.test(s) && prevRaw !== undefined && SKIP_KEYS.has(prevRaw))
|
|
136
|
-
continue;
|
|
137
|
-
segments.push(s);
|
|
138
|
-
}
|
|
139
|
-
const baseName = segments.map((s) => toKebabCase(s).replace(/\./g, "-")).join("-");
|
|
140
|
-
if (!fragment)
|
|
141
|
-
return baseName;
|
|
142
|
-
const fragSegments = fragment.split("/").filter((s) => s && !SKIP_KEYS.has(s) && s !== "properties");
|
|
143
|
-
const fragLast = fragSegments[fragSegments.length - 1];
|
|
144
|
-
if (!fragLast)
|
|
145
|
-
return baseName;
|
|
146
|
-
return `${baseName}-${toKebabCase(fragLast)}`;
|
|
147
|
-
};
|
|
148
|
-
var refToFilename = (ref) => {
|
|
149
|
-
if (ref.startsWith("http://") || ref.startsWith("https://")) {
|
|
150
|
-
return uriRefToFilename(ref);
|
|
151
|
-
}
|
|
152
|
-
const segments = ref.split("/");
|
|
153
|
-
let filename = segments[segments.length - 1];
|
|
154
|
-
if (/[A-Z]/.test(filename)) {
|
|
155
|
-
filename = toKebabCase(filename);
|
|
156
|
-
}
|
|
157
|
-
return filename;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
// src/ref-to-name.ts
|
|
161
|
-
var kebabToPascal = (kebab, suffix) => {
|
|
162
|
-
const words = kebab.split("-");
|
|
163
|
-
let pascalCase = "";
|
|
164
|
-
for (const word of words) {
|
|
165
|
-
pascalCase += word.charAt(0).toUpperCase() + word.slice(1);
|
|
166
|
-
}
|
|
167
|
-
return pascalCase + suffix;
|
|
168
|
-
};
|
|
169
|
-
var refToName = (ref, suffix = "") => kebabToPascal(refToFilename(ref), suffix);
|
|
170
|
-
|
|
171
|
-
// src/safe-accessor.ts
|
|
172
|
-
var JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
173
|
-
var safeAccessor = (variable, key) => {
|
|
174
|
-
if (JS_IDENTIFIER.test(key)) {
|
|
175
|
-
return `${variable}.${key}`;
|
|
176
|
-
}
|
|
177
|
-
if (variable.endsWith("?")) {
|
|
178
|
-
return `${variable}.['${key}']`;
|
|
179
|
-
}
|
|
180
|
-
return `${variable}['${key}']`;
|
|
181
|
-
};
|
|
182
|
-
var safeKey = (key) => {
|
|
183
|
-
if (JS_IDENTIFIER.test(key)) {
|
|
184
|
-
return key;
|
|
185
|
-
}
|
|
186
|
-
return `'${key}'`;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// src/generate-type-definition.ts
|
|
190
|
-
var getConditionalObjectSchema = (schema) => {
|
|
191
|
-
if (!isSchemaObject(schema)) {
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
if (!("if" in schema) || !("then" in schema)) {
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
const ifSchema = schema.if;
|
|
198
|
-
const thenSchema = schema.then;
|
|
199
|
-
if (!isSchemaObject(ifSchema) || !isSchemaObject(thenSchema)) {
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
const ifProperties = ifSchema.properties;
|
|
203
|
-
const thenProperties = thenSchema.properties;
|
|
204
|
-
const hasIfProperties = ifProperties && typeof ifProperties === "object";
|
|
205
|
-
const hasThenProperties = thenProperties && typeof thenProperties === "object";
|
|
206
|
-
if (!hasIfProperties && !hasThenProperties) {
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
const properties = {
|
|
210
|
-
...hasIfProperties ? ifProperties : {},
|
|
211
|
-
...hasThenProperties ? thenProperties : {}
|
|
212
|
-
};
|
|
213
|
-
const required = new Set;
|
|
214
|
-
if (Array.isArray(ifSchema.required)) {
|
|
215
|
-
for (const key of ifSchema.required) {
|
|
216
|
-
required.add(key);
|
|
1
|
+
import { getMjstBrand, getMjstInstanceOf, getMjstPrimitive } from './mjst-extension.js';
|
|
2
|
+
import { refToName } from './ref-to-name.js';
|
|
3
|
+
import { safeKey } from './safe-accessor.js';
|
|
4
|
+
import { isObjectSchema, isSchemaObject } from './schema-guards.js';
|
|
5
|
+
const getConditionalObjectSchema = (schema) => {
|
|
6
|
+
if (!isSchemaObject(schema)) {
|
|
7
|
+
return null;
|
|
217
8
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
for (const key in ifProperties) {
|
|
221
|
-
required.add(key);
|
|
9
|
+
if (!('if' in schema) || !('then' in schema)) {
|
|
10
|
+
return null;
|
|
222
11
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
12
|
+
const ifSchema = schema.if;
|
|
13
|
+
const thenSchema = schema.then;
|
|
14
|
+
if (!isSchemaObject(ifSchema) || !isSchemaObject(thenSchema)) {
|
|
15
|
+
return null;
|
|
227
16
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
17
|
+
const ifProperties = ifSchema.properties;
|
|
18
|
+
const thenProperties = thenSchema.properties;
|
|
19
|
+
const hasIfProperties = ifProperties && typeof ifProperties === 'object';
|
|
20
|
+
const hasThenProperties = thenProperties && typeof thenProperties === 'object';
|
|
21
|
+
if (!hasIfProperties && !hasThenProperties) {
|
|
22
|
+
return null;
|
|
232
23
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
if (isObjectSchema(schema)) {
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
return "patternProperties" in schema || "additionalProperties" in schema || "if" in schema && "then" in schema;
|
|
252
|
-
};
|
|
253
|
-
var getBooleanSubSchemaType = (schema) => {
|
|
254
|
-
return schema ? "unknown" : "never";
|
|
255
|
-
};
|
|
256
|
-
var buildJsDocBlock = (title, description, commentUrl) => {
|
|
257
|
-
let block = `/**
|
|
258
|
-
`;
|
|
259
|
-
block += `* ${title}
|
|
260
|
-
`;
|
|
261
|
-
block += `*
|
|
262
|
-
`;
|
|
263
|
-
block += `* ${description}
|
|
264
|
-
`;
|
|
265
|
-
if (commentUrl?.startsWith("http")) {
|
|
266
|
-
block += `*
|
|
267
|
-
`;
|
|
268
|
-
block += `* @see {@link ${commentUrl}}
|
|
269
|
-
`;
|
|
270
|
-
}
|
|
271
|
-
block += `*/
|
|
272
|
-
`;
|
|
273
|
-
return block;
|
|
274
|
-
};
|
|
275
|
-
var getTypeScriptType = (schema, options = {}) => {
|
|
276
|
-
const base = getUnbrandedType(schema, options);
|
|
277
|
-
const brand = getMjstBrand(schema);
|
|
278
|
-
return brand ? `(${base} & { readonly __brand: '${brand}' })` : base;
|
|
279
|
-
};
|
|
280
|
-
var recordType = (keyType, valueType, options) => options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`;
|
|
281
|
-
var getUnbrandedType = (schema, options = {}) => {
|
|
282
|
-
if (typeof schema === "boolean") {
|
|
283
|
-
return getBooleanSubSchemaType(schema);
|
|
284
|
-
}
|
|
285
|
-
if (typeof schema !== "object" || schema === null) {
|
|
286
|
-
return "unknown";
|
|
287
|
-
}
|
|
288
|
-
const instanceOf = getMjstInstanceOf(schema);
|
|
289
|
-
if (instanceOf) {
|
|
290
|
-
return instanceOf;
|
|
291
|
-
}
|
|
292
|
-
const primitive = getMjstPrimitive(schema);
|
|
293
|
-
if (primitive) {
|
|
294
|
-
return primitive;
|
|
295
|
-
}
|
|
296
|
-
if (schema.$ref) {
|
|
297
|
-
if (!schema.$ref.startsWith("#")) {
|
|
298
|
-
return "unknown";
|
|
24
|
+
const properties = {
|
|
25
|
+
...(hasIfProperties ? ifProperties : {}),
|
|
26
|
+
...(hasThenProperties ? thenProperties : {}),
|
|
27
|
+
};
|
|
28
|
+
const required = new Set();
|
|
29
|
+
if (Array.isArray(ifSchema.required)) {
|
|
30
|
+
for (const key of ifSchema.required) {
|
|
31
|
+
required.add(key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (hasIfProperties) {
|
|
35
|
+
for (const key in ifProperties) {
|
|
36
|
+
required.add(key);
|
|
37
|
+
}
|
|
299
38
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
return `Schema${options.typeSuffix ?? ""}`;
|
|
39
|
+
if (Array.isArray(thenSchema.required)) {
|
|
40
|
+
for (const key of thenSchema.required) {
|
|
41
|
+
required.add(key);
|
|
42
|
+
}
|
|
305
43
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
312
|
-
if (schema.enum.length === 1) {
|
|
313
|
-
return JSON.stringify(schema.enum[0]);
|
|
44
|
+
if (hasThenProperties) {
|
|
45
|
+
for (const key in thenProperties) {
|
|
46
|
+
required.add(key);
|
|
47
|
+
}
|
|
314
48
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
49
|
+
const thenRef = typeof thenSchema.$ref === 'string' ? thenSchema.$ref : null;
|
|
50
|
+
return {
|
|
51
|
+
schema: {
|
|
52
|
+
type: 'object',
|
|
53
|
+
properties,
|
|
54
|
+
...(required.size > 0 ? { required: Array.from(required) } : {}),
|
|
55
|
+
},
|
|
56
|
+
thenRef,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const isObjectLikeSchema = (schema) => {
|
|
60
|
+
if (!isSchemaObject(schema)) {
|
|
61
|
+
return false;
|
|
318
62
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
if (schema.enum && schema.enum.length > 1) {
|
|
322
|
-
let multiEnumUnion = JSON.stringify(schema.enum[0]);
|
|
323
|
-
for (let i = 1;i < schema.enum.length; i++) {
|
|
324
|
-
multiEnumUnion += " | " + JSON.stringify(schema.enum[i]);
|
|
63
|
+
if (isObjectSchema(schema)) {
|
|
64
|
+
return true;
|
|
325
65
|
}
|
|
326
|
-
return
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
66
|
+
return 'patternProperties' in schema || 'additionalProperties' in schema || ('if' in schema && 'then' in schema);
|
|
67
|
+
};
|
|
68
|
+
const getBooleanSubSchemaType = (schema) => {
|
|
69
|
+
return schema ? 'unknown' : 'never';
|
|
70
|
+
};
|
|
71
|
+
const buildJsDocBlock = (title, description, commentUrl) => {
|
|
72
|
+
let block = `/**\n`;
|
|
73
|
+
block += `* ${title}\n`;
|
|
74
|
+
block += `*\n`;
|
|
75
|
+
block += `* ${description}\n`;
|
|
76
|
+
if (commentUrl?.startsWith('http')) {
|
|
77
|
+
block += `* \n`;
|
|
78
|
+
block += `* @see {@link ${commentUrl}}\n`;
|
|
332
79
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
80
|
+
block += `*/\n`;
|
|
81
|
+
return block;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Converts a JSON Schema type to its TypeScript equivalent, applying any
|
|
85
|
+
* `x-mjst` brand. Branding is type-level only, so we compute the underlying
|
|
86
|
+
* type and intersect it with a unique brand marker. This is the recursion entry
|
|
87
|
+
* point, so branded nested fields are wrapped too.
|
|
88
|
+
*/
|
|
89
|
+
const getTypeScriptType = (schema, options = {}) => {
|
|
90
|
+
const base = getUnbrandedType(schema, options);
|
|
91
|
+
const brand = getMjstBrand(schema);
|
|
92
|
+
return brand ? `(${base} & { readonly __brand: '${brand}' })` : base;
|
|
93
|
+
};
|
|
94
|
+
/** Wraps a `Record<...>` in `Readonly<...>` when readonly output is requested. */
|
|
95
|
+
const recordType = (keyType, valueType, options) => options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`;
|
|
96
|
+
/**
|
|
97
|
+
* Converts a JSON Schema type to its TypeScript equivalent, ignoring any brand.
|
|
98
|
+
* Recursively handles nested objects and arrays.
|
|
99
|
+
*/
|
|
100
|
+
const getUnbrandedType = (schema, options = {}) => {
|
|
101
|
+
// Boolean schema: `true` means any value is valid (unknown), `false` means no value is valid (never)
|
|
102
|
+
if (typeof schema === 'boolean') {
|
|
103
|
+
return getBooleanSubSchemaType(schema);
|
|
339
104
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
let intersectionTypes = getTypeScriptType(schema.allOf[0], options);
|
|
344
|
-
for (let i = 1;i < schema.allOf.length; i++) {
|
|
345
|
-
intersectionTypes += " & " + getTypeScriptType(schema.allOf[i], options);
|
|
105
|
+
// Check if schema is an object (not a boolean schema)
|
|
106
|
+
if (typeof schema !== 'object' || schema === null) {
|
|
107
|
+
return 'unknown';
|
|
346
108
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
if (conditionalResult.thenRef) {
|
|
353
|
-
return `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}`;
|
|
109
|
+
// An x-mjst instanceOf hint means the value is a runtime class (e.g. Date)
|
|
110
|
+
// that JSON Schema cannot describe — emit the class name as the type directly.
|
|
111
|
+
const instanceOf = getMjstInstanceOf(schema);
|
|
112
|
+
if (instanceOf) {
|
|
113
|
+
return instanceOf;
|
|
354
114
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
if (
|
|
359
|
-
|
|
360
|
-
return recordType("string", getBooleanSubSchemaType(schema.additionalProperties), options);
|
|
361
|
-
}
|
|
362
|
-
return recordType("string", getTypeScriptType(schema.additionalProperties, options), options);
|
|
115
|
+
// An x-mjst primitive hint (e.g. bigint) names a non-JSON primitive — emit it
|
|
116
|
+
// directly as the TypeScript type.
|
|
117
|
+
const primitive = getMjstPrimitive(schema);
|
|
118
|
+
if (primitive) {
|
|
119
|
+
return primitive;
|
|
363
120
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
const valueType = typeof value === "boolean" ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options);
|
|
370
|
-
if (pattern === "^x-") {
|
|
371
|
-
return recordType("`x-${string}`", valueType, options);
|
|
372
|
-
}
|
|
373
|
-
return recordType("string", valueType, options);
|
|
121
|
+
// Handle $ref
|
|
122
|
+
if (schema.$ref) {
|
|
123
|
+
// External refs (e.g. http://json-schema.org/...) cannot be resolved locally — treat as unknown
|
|
124
|
+
if (!schema.$ref.startsWith('#')) {
|
|
125
|
+
return 'unknown';
|
|
374
126
|
}
|
|
375
|
-
|
|
127
|
+
return refToName(schema.$ref, options.typeSuffix);
|
|
376
128
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (typeof schema.default === "boolean") {
|
|
385
|
-
return "boolean";
|
|
386
|
-
}
|
|
129
|
+
// Handle $dynamicRef (used for recursive schemas)
|
|
130
|
+
if (schema.$dynamicRef) {
|
|
131
|
+
// For #meta, this refers to the Schema type itself (JSON Schema 2020-12 $dynamicAnchor pattern)
|
|
132
|
+
if (schema.$dynamicRef === '#meta') {
|
|
133
|
+
return `Schema${options.typeSuffix ?? ''}`;
|
|
134
|
+
}
|
|
135
|
+
return refToName(schema.$dynamicRef, options.typeSuffix);
|
|
387
136
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const mapType = (t) => {
|
|
392
|
-
switch (t) {
|
|
393
|
-
case "string":
|
|
394
|
-
return "string";
|
|
395
|
-
case "number":
|
|
396
|
-
case "integer":
|
|
397
|
-
return "number";
|
|
398
|
-
case "boolean":
|
|
399
|
-
return "boolean";
|
|
400
|
-
case "null":
|
|
401
|
-
return "null";
|
|
402
|
-
case "array":
|
|
403
|
-
return "unknown[]";
|
|
404
|
-
case "object":
|
|
405
|
-
return "Record<string, unknown>";
|
|
406
|
-
default:
|
|
407
|
-
return "unknown";
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
let typeUnion = mapType(schema.type[0]);
|
|
411
|
-
for (let i = 1;i < schema.type.length; i++) {
|
|
412
|
-
typeUnion += " | " + mapType(schema.type[i]);
|
|
137
|
+
// Handle const - literal type
|
|
138
|
+
if (schema.const !== undefined) {
|
|
139
|
+
return JSON.stringify(schema.const);
|
|
413
140
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
return "string";
|
|
419
|
-
case "number":
|
|
420
|
-
case "integer":
|
|
421
|
-
return "number";
|
|
422
|
-
case "boolean":
|
|
423
|
-
return "boolean";
|
|
424
|
-
case "array":
|
|
425
|
-
if (schema.items) {
|
|
426
|
-
const itemType = getTypeScriptType(schema.items, options);
|
|
427
|
-
const wrappedItemType = itemType.includes(" | ") ? `(${itemType})` : itemType;
|
|
428
|
-
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`;
|
|
429
|
-
}
|
|
430
|
-
return options.readonly ? "readonly unknown[]" : "unknown[]";
|
|
431
|
-
case "object":
|
|
432
|
-
if (schema.properties) {
|
|
433
|
-
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
434
|
-
const hasDescriptions = Object.values(schema.properties).some((p) => isSchemaObject(p) && (typeof p.description === "string" || typeof p.$comment === "string"));
|
|
435
|
-
if (hasDescriptions) {
|
|
436
|
-
let properties2 = "";
|
|
437
|
-
let first2 = true;
|
|
438
|
-
for (const key in schema.properties) {
|
|
439
|
-
const propSchema = schema.properties[key];
|
|
440
|
-
const isRequired = schema.required?.includes(key) ?? false;
|
|
441
|
-
const optional = isRequired ? "" : "?";
|
|
442
|
-
const propType = getTypeScriptType(propSchema, options);
|
|
443
|
-
const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === "string" ? propSchema.description : isSchemaObject(propSchema) && typeof propSchema.$comment === "string" ? propSchema.$comment : undefined;
|
|
444
|
-
if (!first2)
|
|
445
|
-
properties2 += `
|
|
446
|
-
`;
|
|
447
|
-
first2 = false;
|
|
448
|
-
if (inlineDescription) {
|
|
449
|
-
properties2 += " /** " + inlineDescription + ` */
|
|
450
|
-
` + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
|
|
451
|
-
} else {
|
|
452
|
-
properties2 += " " + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
return `{
|
|
456
|
-
` + properties2 + `
|
|
457
|
-
}`;
|
|
141
|
+
// Handle enum - union of literal types
|
|
142
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
143
|
+
if (schema.enum.length === 1) {
|
|
144
|
+
return JSON.stringify(schema.enum[0]);
|
|
458
145
|
}
|
|
459
|
-
let
|
|
460
|
-
let
|
|
461
|
-
|
|
462
|
-
const propSchema = schema.properties[key];
|
|
463
|
-
const isRequired = schema.required?.includes(key) ?? false;
|
|
464
|
-
const optional = isRequired ? "" : "?";
|
|
465
|
-
const propType = getTypeScriptType(propSchema, options);
|
|
466
|
-
if (!first)
|
|
467
|
-
properties += "; ";
|
|
468
|
-
properties += readonlyPrefix + safeKey(key) + optional + ": " + propType;
|
|
469
|
-
first = false;
|
|
146
|
+
let enumUnion = JSON.stringify(schema.enum[0]);
|
|
147
|
+
for (let i = 1; i < schema.enum.length; i++) {
|
|
148
|
+
enumUnion += ' | ' + JSON.stringify(schema.enum[i]);
|
|
470
149
|
}
|
|
471
|
-
return
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const firstEntry = Object.entries(schema.patternProperties)[0];
|
|
479
|
-
if (firstEntry) {
|
|
480
|
-
const [pattern, patternVal] = firstEntry;
|
|
481
|
-
if (patternVal) {
|
|
482
|
-
const valueType = getTypeScriptType(patternVal, options);
|
|
483
|
-
if (pattern === "^x-") {
|
|
484
|
-
return recordType("`x-${string}`", valueType, options);
|
|
485
|
-
}
|
|
486
|
-
return recordType("string", valueType, options);
|
|
487
|
-
}
|
|
150
|
+
return enumUnion;
|
|
151
|
+
}
|
|
152
|
+
// Handle multi-value enum - union of literal types
|
|
153
|
+
if (schema.enum && schema.enum.length > 1) {
|
|
154
|
+
let multiEnumUnion = JSON.stringify(schema.enum[0]);
|
|
155
|
+
for (let i = 1; i < schema.enum.length; i++) {
|
|
156
|
+
multiEnumUnion += ' | ' + JSON.stringify(schema.enum[i]);
|
|
488
157
|
}
|
|
489
|
-
|
|
490
|
-
return "object";
|
|
491
|
-
default:
|
|
492
|
-
return "unknown";
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
var generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
496
|
-
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
497
|
-
if (!isObjectLikeSchema(schema)) {
|
|
498
|
-
const tsType = getTypeScriptType(schema, options);
|
|
499
|
-
let result = "";
|
|
500
|
-
const topLevelComment = isSchemaObject(schema) && typeof schema.description === "string" && schema.description || isSchemaObject(schema) && typeof schema.$comment === "string" && schema.$comment || undefined;
|
|
501
|
-
if (topLevelComment) {
|
|
502
|
-
result += buildJsDocBlock(typeName, topLevelComment);
|
|
158
|
+
return multiEnumUnion;
|
|
503
159
|
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
let jsDocDescription;
|
|
513
|
-
const topLevelComment = isSchemaObject(schema) && typeof schema.description === "string" && schema.description || isSchemaObject(schema) && typeof schema.$comment === "string" && schema.$comment || undefined;
|
|
514
|
-
if (topLevelComment) {
|
|
515
|
-
jsDocTitle = typeName;
|
|
516
|
-
jsDocDescription = topLevelComment;
|
|
160
|
+
// Handle union types (oneOf, anyOf) - check this before returning unknown
|
|
161
|
+
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
162
|
+
// schema.oneOf[0] is safe: we guard with .length > 0 above
|
|
163
|
+
let oneOfUnion = getTypeScriptType(schema.oneOf[0], options);
|
|
164
|
+
for (let i = 1; i < schema.oneOf.length; i++) {
|
|
165
|
+
oneOfUnion += ' | ' + getTypeScriptType(schema.oneOf[i], options);
|
|
166
|
+
}
|
|
167
|
+
return oneOfUnion;
|
|
517
168
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if (firstPatternProperty === undefined) {
|
|
526
|
-
return `export type ${typeName} = Record<string, unknown>;`;
|
|
527
|
-
}
|
|
528
|
-
const patternPropType = typeof firstPatternProperty === "boolean" ? getBooleanSubSchemaType(firstPatternProperty) : getTypeScriptType(firstPatternProperty, options);
|
|
529
|
-
const keyType = firstPattern === "^x-" ? "`x-${string}`" : "string";
|
|
530
|
-
let result2 = "";
|
|
531
|
-
if (jsDocTitle && jsDocDescription) {
|
|
532
|
-
result2 += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
533
|
-
}
|
|
534
|
-
result2 += `export type ${typeName} = ${recordType(keyType, patternPropType, options)};`;
|
|
535
|
-
return result2;
|
|
169
|
+
if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
170
|
+
// schema.anyOf[0] is safe: we guard with .length > 0 above
|
|
171
|
+
let anyOfUnion = getTypeScriptType(schema.anyOf[0], options);
|
|
172
|
+
for (let i = 1; i < schema.anyOf.length; i++) {
|
|
173
|
+
anyOfUnion += ' | ' + getTypeScriptType(schema.anyOf[i], options);
|
|
174
|
+
}
|
|
175
|
+
return anyOfUnion;
|
|
536
176
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
};`;
|
|
546
|
-
return result2;
|
|
177
|
+
// Handle allOf (intersection types)
|
|
178
|
+
if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length > 0) {
|
|
179
|
+
// schema.allOf[0] is safe: we guard with .length > 0 above
|
|
180
|
+
let intersectionTypes = getTypeScriptType(schema.allOf[0], options);
|
|
181
|
+
for (let i = 1; i < schema.allOf.length; i++) {
|
|
182
|
+
intersectionTypes += ' & ' + getTypeScriptType(schema.allOf[i], options);
|
|
183
|
+
}
|
|
184
|
+
return intersectionTypes;
|
|
547
185
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
const quotedKey = readonlyPrefix + safeKey(key);
|
|
557
|
-
if (!isFirstProp)
|
|
558
|
-
properties += `
|
|
559
|
-
`;
|
|
560
|
-
isFirstProp = false;
|
|
561
|
-
const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === "string" ? propSchema.description : isSchemaObject(propSchema) && typeof propSchema.$comment === "string" ? propSchema.$comment : undefined;
|
|
562
|
-
if (inlineDescription) {
|
|
563
|
-
properties += " /** " + inlineDescription + ` */
|
|
564
|
-
` + quotedKey + optional + ": " + propType + ";";
|
|
565
|
-
} else {
|
|
566
|
-
properties += " " + quotedKey + optional + ": " + propType + ";";
|
|
567
|
-
}
|
|
186
|
+
// Handle object-like conditional schemas that use if/then without declaring type
|
|
187
|
+
const conditionalResult = getConditionalObjectSchema(schema);
|
|
188
|
+
if (conditionalResult) {
|
|
189
|
+
const baseType = getTypeScriptType(conditionalResult.schema, options);
|
|
190
|
+
if (conditionalResult.thenRef) {
|
|
191
|
+
return `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}`;
|
|
192
|
+
}
|
|
193
|
+
return baseType;
|
|
568
194
|
}
|
|
569
|
-
|
|
570
|
-
if (
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
195
|
+
// No type so we return unknown
|
|
196
|
+
if (!schema.type) {
|
|
197
|
+
if (schema.additionalProperties !== undefined) {
|
|
198
|
+
if (typeof schema.additionalProperties === 'boolean') {
|
|
199
|
+
return recordType('string', getBooleanSubSchemaType(schema.additionalProperties), options);
|
|
200
|
+
}
|
|
201
|
+
return recordType('string', getTypeScriptType(schema.additionalProperties, options), options);
|
|
202
|
+
}
|
|
203
|
+
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
204
|
+
const firstEntry = Object.entries(schema.patternProperties)[0];
|
|
205
|
+
if (firstEntry) {
|
|
206
|
+
const [pattern, value] = firstEntry;
|
|
207
|
+
if (value !== undefined) {
|
|
208
|
+
const valueType = typeof value === 'boolean' ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options);
|
|
209
|
+
// The ^x- pattern is a common JSON Schema convention for vendor extensions
|
|
210
|
+
// that maps naturally to the TypeScript template literal `x-${string}`
|
|
211
|
+
if (pattern === '^x-') {
|
|
212
|
+
return recordType('`x-${string}`', valueType, options);
|
|
213
|
+
}
|
|
214
|
+
return recordType('string', valueType, options);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
574
217
|
}
|
|
575
|
-
|
|
218
|
+
if (schema.default !== undefined) {
|
|
219
|
+
if (typeof schema.default === 'string') {
|
|
220
|
+
return 'string';
|
|
221
|
+
}
|
|
222
|
+
if (typeof schema.default === 'number') {
|
|
223
|
+
return 'number';
|
|
224
|
+
}
|
|
225
|
+
if (typeof schema.default === 'boolean') {
|
|
226
|
+
return 'boolean';
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return 'unknown';
|
|
576
230
|
}
|
|
577
|
-
|
|
578
|
-
|
|
231
|
+
// Handle type as an array (union of types)
|
|
232
|
+
if (Array.isArray(schema.type)) {
|
|
233
|
+
const mapType = (t) => {
|
|
234
|
+
switch (t) {
|
|
235
|
+
case 'string':
|
|
236
|
+
return 'string';
|
|
237
|
+
case 'number':
|
|
238
|
+
case 'integer':
|
|
239
|
+
return 'number';
|
|
240
|
+
case 'boolean':
|
|
241
|
+
return 'boolean';
|
|
242
|
+
case 'null':
|
|
243
|
+
return 'null';
|
|
244
|
+
case 'array':
|
|
245
|
+
return 'unknown[]';
|
|
246
|
+
case 'object':
|
|
247
|
+
return 'Record<string, unknown>';
|
|
248
|
+
default:
|
|
249
|
+
return 'unknown';
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
let typeUnion = mapType(schema.type[0]);
|
|
253
|
+
for (let i = 1; i < schema.type.length; i++) {
|
|
254
|
+
typeUnion += ' | ' + mapType(schema.type[i]);
|
|
255
|
+
}
|
|
256
|
+
return typeUnion;
|
|
579
257
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
258
|
+
switch (schema.type) {
|
|
259
|
+
// String
|
|
260
|
+
case 'string':
|
|
261
|
+
return 'string';
|
|
262
|
+
// Number
|
|
263
|
+
case 'number':
|
|
264
|
+
case 'integer':
|
|
265
|
+
return 'number';
|
|
266
|
+
// Boolean
|
|
267
|
+
case 'boolean':
|
|
268
|
+
return 'boolean';
|
|
269
|
+
// Array
|
|
270
|
+
case 'array':
|
|
271
|
+
if (schema.items) {
|
|
272
|
+
const itemType = getTypeScriptType(schema.items, options);
|
|
273
|
+
// Wrap union types in parentheses so `(A | B)[]` is not misread as `A | B[]`
|
|
274
|
+
const wrappedItemType = itemType.includes(' | ') ? `(${itemType})` : itemType;
|
|
275
|
+
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`;
|
|
276
|
+
}
|
|
277
|
+
return options.readonly ? 'readonly unknown[]' : 'unknown[]';
|
|
278
|
+
// Object
|
|
279
|
+
case 'object':
|
|
280
|
+
if (schema.properties) {
|
|
281
|
+
const readonlyPrefix = options.readonly ? 'readonly ' : '';
|
|
282
|
+
const hasDescriptions = Object.values(schema.properties).some((p) => isSchemaObject(p) && (typeof p.description === 'string' || typeof p.$comment === 'string'));
|
|
283
|
+
if (hasDescriptions) {
|
|
284
|
+
let properties = '';
|
|
285
|
+
let first = true;
|
|
286
|
+
for (const key in schema.properties) {
|
|
287
|
+
// schema.properties[key] is safe: key comes from iterating schema.properties
|
|
288
|
+
const propSchema = schema.properties[key];
|
|
289
|
+
const isRequired = schema.required?.includes(key) ?? false;
|
|
290
|
+
const optional = isRequired ? '' : '?';
|
|
291
|
+
const propType = getTypeScriptType(propSchema, options);
|
|
292
|
+
const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === 'string'
|
|
293
|
+
? propSchema.description
|
|
294
|
+
: isSchemaObject(propSchema) && typeof propSchema.$comment === 'string'
|
|
295
|
+
? propSchema.$comment
|
|
296
|
+
: undefined;
|
|
297
|
+
if (!first)
|
|
298
|
+
properties += '\n';
|
|
299
|
+
first = false;
|
|
300
|
+
if (inlineDescription) {
|
|
301
|
+
properties +=
|
|
302
|
+
' /** ' +
|
|
303
|
+
inlineDescription +
|
|
304
|
+
' */\n ' +
|
|
305
|
+
readonlyPrefix +
|
|
306
|
+
safeKey(key) +
|
|
307
|
+
optional +
|
|
308
|
+
': ' +
|
|
309
|
+
propType +
|
|
310
|
+
';';
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
properties += ' ' + readonlyPrefix + safeKey(key) + optional + ': ' + propType + ';';
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return '{\n' + properties + '\n}';
|
|
317
|
+
}
|
|
318
|
+
let properties = '';
|
|
319
|
+
let first = true;
|
|
320
|
+
for (const key in schema.properties) {
|
|
321
|
+
// schema.properties[key] is safe: key comes from iterating schema.properties
|
|
322
|
+
const propSchema = schema.properties[key];
|
|
323
|
+
const isRequired = schema.required?.includes(key) ?? false;
|
|
324
|
+
const optional = isRequired ? '' : '?';
|
|
325
|
+
const propType = getTypeScriptType(propSchema, options);
|
|
326
|
+
if (!first)
|
|
327
|
+
properties += '; ';
|
|
328
|
+
properties += readonlyPrefix + safeKey(key) + optional + ': ' + propType;
|
|
329
|
+
first = false;
|
|
330
|
+
}
|
|
331
|
+
return '{ ' + properties + ' }';
|
|
332
|
+
}
|
|
333
|
+
// Handle additionalProperties with $ref or $dynamicRef
|
|
334
|
+
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
|
|
335
|
+
const additionalPropType = getTypeScriptType(schema.additionalProperties, options);
|
|
336
|
+
return recordType('string', additionalPropType, options);
|
|
337
|
+
}
|
|
338
|
+
// Handle patternProperties as a Record type
|
|
339
|
+
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
340
|
+
const firstEntry = Object.entries(schema.patternProperties)[0];
|
|
341
|
+
if (firstEntry) {
|
|
342
|
+
const [pattern, patternVal] = firstEntry;
|
|
343
|
+
if (patternVal) {
|
|
344
|
+
const valueType = getTypeScriptType(patternVal, options);
|
|
345
|
+
if (pattern === '^x-') {
|
|
346
|
+
return recordType('`x-${string}`', valueType, options);
|
|
347
|
+
}
|
|
348
|
+
return recordType('string', valueType, options);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return 'object';
|
|
353
|
+
// Default to unknown
|
|
354
|
+
default:
|
|
355
|
+
return 'unknown';
|
|
583
356
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Generates a TypeScript type definition from a JSON Schema.
|
|
360
|
+
* Handles required vs optional properties based on the schema's required array.
|
|
361
|
+
* Uses $comment as inline JSDoc description when present.
|
|
362
|
+
*/
|
|
363
|
+
export const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
364
|
+
const readonlyPrefix = options.readonly ? 'readonly ' : '';
|
|
365
|
+
// Handle non-object schemas first
|
|
366
|
+
if (!isObjectLikeSchema(schema)) {
|
|
367
|
+
const tsType = getTypeScriptType(schema, options);
|
|
368
|
+
let result = '';
|
|
369
|
+
const topLevelComment = (isSchemaObject(schema) && typeof schema.description === 'string' && schema.description) ||
|
|
370
|
+
(isSchemaObject(schema) && typeof schema.$comment === 'string' && schema.$comment) ||
|
|
371
|
+
undefined;
|
|
372
|
+
if (topLevelComment) {
|
|
373
|
+
result += buildJsDocBlock(typeName, topLevelComment);
|
|
374
|
+
}
|
|
375
|
+
result += `export type ${typeName} = ${tsType};`;
|
|
376
|
+
return result;
|
|
589
377
|
}
|
|
590
|
-
|
|
591
|
-
|
|
378
|
+
if (isObjectLikeSchema(schema)) {
|
|
379
|
+
const conditionalResult = getConditionalObjectSchema(schema);
|
|
380
|
+
const normalizedSchema = conditionalResult?.schema ?? schema;
|
|
381
|
+
const conditionalThenRef = conditionalResult?.thenRef ?? null;
|
|
382
|
+
let jsDocTitle;
|
|
383
|
+
let jsDocDescription;
|
|
384
|
+
const topLevelComment = (isSchemaObject(schema) && typeof schema.description === 'string' && schema.description) ||
|
|
385
|
+
(isSchemaObject(schema) && typeof schema.$comment === 'string' && schema.$comment) ||
|
|
386
|
+
undefined;
|
|
387
|
+
if (topLevelComment) {
|
|
388
|
+
jsDocTitle = typeName;
|
|
389
|
+
jsDocDescription = topLevelComment;
|
|
390
|
+
}
|
|
391
|
+
const hasProperties = normalizedSchema.properties && Object.keys(normalizedSchema.properties).length > 0;
|
|
392
|
+
const hasAdditionalProperties = normalizedSchema.additionalProperties && typeof normalizedSchema.additionalProperties === 'object';
|
|
393
|
+
const hasPatternProperties = normalizedSchema.patternProperties &&
|
|
394
|
+
typeof normalizedSchema.patternProperties === 'object' &&
|
|
395
|
+
Object.keys(normalizedSchema.patternProperties).length > 0;
|
|
396
|
+
// Handle objects with only patternProperties (no fixed properties)
|
|
397
|
+
if (!hasProperties && hasPatternProperties && normalizedSchema.patternProperties) {
|
|
398
|
+
const firstEntry = Object.entries(normalizedSchema.patternProperties)[0];
|
|
399
|
+
const firstPattern = firstEntry?.[0];
|
|
400
|
+
const firstPatternProperty = firstEntry?.[1];
|
|
401
|
+
if (firstPatternProperty === undefined) {
|
|
402
|
+
return `export type ${typeName} = Record<string, unknown>;`;
|
|
403
|
+
}
|
|
404
|
+
const patternPropType = typeof firstPatternProperty === 'boolean'
|
|
405
|
+
? getBooleanSubSchemaType(firstPatternProperty)
|
|
406
|
+
: getTypeScriptType(firstPatternProperty, options);
|
|
407
|
+
// The ^x- pattern is a common JSON Schema convention for vendor extensions
|
|
408
|
+
// that maps naturally to the TypeScript template literal `x-${string}`
|
|
409
|
+
const keyType = firstPattern === '^x-' ? '`x-${string}`' : 'string';
|
|
410
|
+
let result = '';
|
|
411
|
+
if (jsDocTitle && jsDocDescription) {
|
|
412
|
+
result += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
413
|
+
}
|
|
414
|
+
result += `export type ${typeName} = ${recordType(keyType, patternPropType, options)};`;
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
417
|
+
// Handle objects with only additionalProperties (no fixed properties)
|
|
418
|
+
if (!hasProperties && hasAdditionalProperties && normalizedSchema.additionalProperties) {
|
|
419
|
+
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties, options);
|
|
420
|
+
let result = '';
|
|
421
|
+
if (jsDocTitle && jsDocDescription) {
|
|
422
|
+
result += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
423
|
+
}
|
|
424
|
+
result += `export type ${typeName} = {\n ${readonlyPrefix}[key: string]: ${additionalPropType};\n};`;
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
const schemaProps = normalizedSchema.properties ?? {};
|
|
428
|
+
let properties = '';
|
|
429
|
+
let isFirstProp = true;
|
|
430
|
+
for (const key in schemaProps) {
|
|
431
|
+
// schemaProps[key] is safe: key comes from iterating schemaProps
|
|
432
|
+
const propSchema = schemaProps[key];
|
|
433
|
+
const isRequired = normalizedSchema.required?.includes(key) ?? false;
|
|
434
|
+
const optional = isRequired ? '' : '?';
|
|
435
|
+
const propType = getTypeScriptType(propSchema, options);
|
|
436
|
+
const quotedKey = readonlyPrefix + safeKey(key);
|
|
437
|
+
if (!isFirstProp)
|
|
438
|
+
properties += '\n';
|
|
439
|
+
isFirstProp = false;
|
|
440
|
+
// Add JSDoc comment from $comment or description if available
|
|
441
|
+
const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === 'string'
|
|
442
|
+
? propSchema.description
|
|
443
|
+
: isSchemaObject(propSchema) && typeof propSchema.$comment === 'string'
|
|
444
|
+
? propSchema.$comment
|
|
445
|
+
: undefined;
|
|
446
|
+
if (inlineDescription) {
|
|
447
|
+
properties += ' /** ' + inlineDescription + ' */\n ' + quotedKey + optional + ': ' + propType + ';';
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
properties += ' ' + quotedKey + optional + ': ' + propType + ';';
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// Collect allOf $ref intersections
|
|
454
|
+
const allOfIntersections = [];
|
|
455
|
+
if (isSchemaObject(schema) && Array.isArray(schema.allOf)) {
|
|
456
|
+
for (const entry of schema.allOf) {
|
|
457
|
+
if (isSchemaObject(entry) && entry.$ref) {
|
|
458
|
+
allOfIntersections.push(refToName(entry.$ref, options.typeSuffix));
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// JSON Schema 2019-09+ allows $ref as a sibling to other keywords.
|
|
463
|
+
// Treat it as an additional intersection type (e.g. for specification-extensions).
|
|
464
|
+
if (isSchemaObject(schema) && typeof schema.$ref === 'string' && schema.$ref.startsWith('#')) {
|
|
465
|
+
allOfIntersections.push(refToName(schema.$ref, options.typeSuffix));
|
|
466
|
+
}
|
|
467
|
+
let result = '';
|
|
468
|
+
if (jsDocTitle && jsDocDescription) {
|
|
469
|
+
result += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
470
|
+
}
|
|
471
|
+
let typeBody = '{\n' + properties + '\n}';
|
|
472
|
+
if (conditionalThenRef) {
|
|
473
|
+
typeBody += ' & ' + refToName(conditionalThenRef, options.typeSuffix);
|
|
474
|
+
}
|
|
475
|
+
for (const intersectionType of allOfIntersections) {
|
|
476
|
+
typeBody += ' & ' + intersectionType;
|
|
477
|
+
}
|
|
478
|
+
result += 'export type ' + typeName + ' = ' + typeBody + ';';
|
|
479
|
+
return result;
|
|
592
480
|
}
|
|
593
|
-
|
|
594
|
-
return result;
|
|
595
|
-
}
|
|
596
|
-
return "export type " + typeName + " = unknown;";
|
|
597
|
-
};
|
|
598
|
-
export {
|
|
599
|
-
generateTypeDefinition
|
|
481
|
+
return 'export type ' + typeName + ' = unknown;';
|
|
600
482
|
};
|