@amritk/helpers 0.13.5 → 0.15.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.
- package/AI.md +13 -5
- package/README.md +4 -1
- package/dist/assert-id-scopes.d.ts +20 -0
- package/dist/assert-id-scopes.js +45 -0
- package/dist/build-anchor-map.d.ts +26 -0
- package/dist/build-anchor-map.js +35 -0
- package/dist/build-dynamic-ref-map.d.ts +8 -0
- package/dist/build-dynamic-ref-map.js +7 -5
- package/dist/build-resource-registry.d.ts +88 -0
- package/dist/build-resource-registry.js +90 -0
- package/dist/escape-regex-pattern.d.ts +27 -0
- package/dist/escape-regex-pattern.js +21 -1
- package/dist/extract-refs.js +6 -4
- package/dist/fold-nullable.d.ts +19 -0
- package/dist/fold-nullable.js +42 -0
- package/dist/generate-index-barrel.js +13 -6
- package/dist/generate-type-definition.d.ts +8 -0
- package/dist/generate-type-definition.js +249 -185
- package/dist/graft-external-schemas.d.ts +83 -0
- package/dist/graft-external-schemas.js +70 -0
- package/dist/max-schema-depth.d.ts +22 -0
- package/dist/max-schema-depth.js +10 -0
- package/dist/mjst-extension.d.ts +4 -2
- package/dist/mjst-extension.js +11 -2
- package/dist/multiple-of-check.d.ts +31 -0
- package/dist/multiple-of-check.js +6 -10
- package/dist/normalize-ref-scopes.d.ts +29 -0
- package/dist/normalize-ref-scopes.js +53 -0
- package/dist/prune-external-schemas.d.ts +16 -0
- package/dist/prune-external-schemas.js +80 -0
- package/dist/ref-to-filename.d.ts +1 -0
- package/dist/ref-to-filename.js +19 -3
- package/dist/ref-to-name.js +7 -4
- package/dist/resolve-dynamic-refs.d.ts +10 -0
- package/dist/resolve-dynamic-refs.js +18 -16
- package/dist/resolve-ref.d.ts +4 -2
- package/dist/resolve-ref.js +32 -6
- package/dist/resolve-scoped-ref.d.ts +35 -0
- package/dist/resolve-scoped-ref.js +35 -0
- package/dist/safe-accessor.d.ts +38 -1
- package/dist/safe-accessor.js +15 -0
- package/dist/string-length-check.d.ts +8 -0
- package/dist/string-length-check.js +23 -0
- package/dist/upgrade-draft07-schema.js +9 -6
- package/dist/walk-ref-graph.d.ts +38 -3
- package/dist/walk-ref-graph.js +173 -18
- package/package.json +16 -3
- package/src/build-dynamic-ref-map.ts +0 -61
- package/src/derive-root-type-name.ts +0 -54
- package/src/escape-regex-pattern.ts +0 -87
- package/src/extract-dynamic-anchor-defs.ts +0 -25
- package/src/extract-refs.ts +0 -73
- package/src/generate-index-barrel.ts +0 -118
- package/src/generate-type-definition.ts +0 -599
- package/src/mjst-extension.ts +0 -81
- package/src/multiple-of-check.ts +0 -32
- package/src/parse-documentation.ts +0 -177
- package/src/quote-js-string.ts +0 -20
- package/src/ref-to-filename.ts +0 -116
- package/src/ref-to-name.ts +0 -54
- package/src/resolve-dynamic-refs.ts +0 -74
- package/src/resolve-ref.ts +0 -82
- package/src/safe-accessor.ts +0 -53
- package/src/schema-guards.ts +0 -234
- package/src/unknown-key-check.ts +0 -68
- package/src/upgrade-draft07-schema.ts +0 -179
- package/src/walk-ref-graph.ts +0 -240
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { getMjstBrand, getMjstInstanceOf, getMjstPrimitive } from "./mjst-extension.js";
|
|
2
2
|
import { refToName } from "./ref-to-name.js";
|
|
3
|
+
import { resolveRef } from "./resolve-ref.js";
|
|
3
4
|
import { safeKey } from "./safe-accessor.js";
|
|
4
5
|
import { isObjectSchema, isSchemaObject } from "./schema-guards.js";
|
|
6
|
+
const refTypeName = (ref, options) => {
|
|
7
|
+
if (ref.startsWith("#"))
|
|
8
|
+
return refToName(ref, options.typeSuffix);
|
|
9
|
+
if (ref.includes("#/properties/"))
|
|
10
|
+
return "unknown";
|
|
11
|
+
if (options.rootSchema && resolveRef(ref, options.rootSchema))
|
|
12
|
+
return refToName(ref, options.typeSuffix);
|
|
13
|
+
return "unknown";
|
|
14
|
+
};
|
|
5
15
|
const getConditionalObjectSchema = (schema) => {
|
|
6
16
|
if (!isSchemaObject(schema)) {
|
|
7
17
|
return null;
|
|
@@ -14,18 +24,26 @@ const getConditionalObjectSchema = (schema) => {
|
|
|
14
24
|
if (!isSchemaObject(ifSchema) || !isSchemaObject(thenSchema)) {
|
|
15
25
|
return null;
|
|
16
26
|
}
|
|
27
|
+
const ownProperties = schema.properties;
|
|
17
28
|
const ifProperties = ifSchema.properties;
|
|
18
29
|
const thenProperties = thenSchema.properties;
|
|
30
|
+
const hasOwnProperties = ownProperties && typeof ownProperties === "object";
|
|
19
31
|
const hasIfProperties = ifProperties && typeof ifProperties === "object";
|
|
20
32
|
const hasThenProperties = thenProperties && typeof thenProperties === "object";
|
|
21
33
|
if (!hasIfProperties && !hasThenProperties) {
|
|
22
34
|
return null;
|
|
23
35
|
}
|
|
24
36
|
const properties = {
|
|
37
|
+
...hasOwnProperties ? ownProperties : {},
|
|
25
38
|
...hasIfProperties ? ifProperties : {},
|
|
26
39
|
...hasThenProperties ? thenProperties : {}
|
|
27
40
|
};
|
|
28
41
|
const required = /* @__PURE__ */ new Set();
|
|
42
|
+
if (Array.isArray(schema.required)) {
|
|
43
|
+
for (const key of schema.required) {
|
|
44
|
+
required.add(key);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
29
47
|
if (Array.isArray(ifSchema.required)) {
|
|
30
48
|
for (const key of ifSchema.required) {
|
|
31
49
|
required.add(key);
|
|
@@ -68,7 +86,8 @@ const isObjectLikeSchema = (schema) => {
|
|
|
68
86
|
const getBooleanSubSchemaType = (schema) => {
|
|
69
87
|
return schema ? "unknown" : "never";
|
|
70
88
|
};
|
|
71
|
-
const
|
|
89
|
+
const escapeCommentText = (text) => text.replace(/\*\//g, "*\\/");
|
|
90
|
+
const renderJsDocBody = (description, indent) => escapeCommentText(description).split("\n").map((line) => line.length > 0 ? `${indent}* ${line}
|
|
72
91
|
` : `${indent}*
|
|
73
92
|
`).join("");
|
|
74
93
|
const buildJsDocBlock = (title, description, commentUrl) => {
|
|
@@ -91,7 +110,7 @@ const buildJsDocBlock = (title, description, commentUrl) => {
|
|
|
91
110
|
};
|
|
92
111
|
const buildInlinePropertyComment = (description) => {
|
|
93
112
|
if (!description.includes("\n")) {
|
|
94
|
-
return ` /** ${description} */
|
|
113
|
+
return ` /** ${escapeCommentText(description)} */
|
|
95
114
|
`;
|
|
96
115
|
}
|
|
97
116
|
return ` /**
|
|
@@ -101,7 +120,21 @@ ${renderJsDocBody(description, " ")} */
|
|
|
101
120
|
const getTypeScriptType = (schema, options = {}) => {
|
|
102
121
|
const base = getUnbrandedType(schema, options);
|
|
103
122
|
const brand = getMjstBrand(schema);
|
|
104
|
-
|
|
123
|
+
const branded = brand ? `(${base} & { readonly __brand: '${brand}' })` : base;
|
|
124
|
+
return isNullableSchema(schema) ? withNull(branded) : branded;
|
|
125
|
+
};
|
|
126
|
+
const isNullableSchema = (schema) => isSchemaObject(schema) && schema["nullable"] === true;
|
|
127
|
+
const wrapUnion = (type) => type.includes(" | ") ? `(${type})` : type;
|
|
128
|
+
const withNull = (type) => type.split(" | ").includes("null") ? type : `${type} | null`;
|
|
129
|
+
const unionOf = (members) => [...new Set(members)].join(" | ");
|
|
130
|
+
const intersectionOf = (members) => {
|
|
131
|
+
const kept = [...new Set(members)].filter((member) => member !== "unknown");
|
|
132
|
+
if (kept.length === 0)
|
|
133
|
+
return "unknown";
|
|
134
|
+
const meaningful = kept.length > 1 ? kept.filter((member) => member !== "object") : kept;
|
|
135
|
+
if (meaningful.length === 0)
|
|
136
|
+
return "object";
|
|
137
|
+
return meaningful.join(" & ");
|
|
105
138
|
};
|
|
106
139
|
const recordType = (keyType, valueType, options) => options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`;
|
|
107
140
|
const patternPropertiesRecordType = (patternProperties, options) => {
|
|
@@ -123,203 +156,235 @@ const patternPropertiesRecordType = (patternProperties, options) => {
|
|
|
123
156
|
const keyType = entries.length === 1 && entries[0]?.[0] === "^x-" ? "`x-${string}`" : "string";
|
|
124
157
|
return recordType(keyType, valueType, options);
|
|
125
158
|
};
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
159
|
+
const getTuplePositions = (schema) => {
|
|
160
|
+
const record = schema;
|
|
161
|
+
const prefixItems = record["prefixItems"];
|
|
162
|
+
if (Array.isArray(prefixItems) && prefixItems.length > 0)
|
|
163
|
+
return prefixItems;
|
|
164
|
+
if (Array.isArray(schema.items) && schema.items.length > 0)
|
|
165
|
+
return schema.items;
|
|
166
|
+
return void 0;
|
|
167
|
+
};
|
|
168
|
+
const tupleTypeToTs = (schema, positions, options) => {
|
|
169
|
+
const record = schema;
|
|
170
|
+
const rest = Array.isArray(schema.items) ? record["additionalItems"] : record["items"];
|
|
171
|
+
const minItems = typeof schema.minItems === "number" ? schema.minItems : 0;
|
|
172
|
+
const parts = [];
|
|
173
|
+
for (let i = 0; i < positions.length; i++) {
|
|
174
|
+
const positionType = wrapUnion(getTypeScriptType(positions[i], options));
|
|
175
|
+
parts.push(i < minItems ? positionType : `${positionType}?`);
|
|
140
176
|
}
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
return refToName(schema.$ref, options.typeSuffix);
|
|
177
|
+
if (rest !== false) {
|
|
178
|
+
const restType = rest === void 0 || rest === true ? "unknown" : wrapUnion(getTypeScriptType(rest, options));
|
|
179
|
+
parts.push(`...${restType}[]`);
|
|
146
180
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
181
|
+
const tuple = `[${parts.join(", ")}]`;
|
|
182
|
+
return options.readonly ? `readonly ${tuple}` : tuple;
|
|
183
|
+
};
|
|
184
|
+
const arrayTypeToTs = (schema, options) => {
|
|
185
|
+
const positions = getTuplePositions(schema);
|
|
186
|
+
if (positions)
|
|
187
|
+
return tupleTypeToTs(schema, positions, options);
|
|
188
|
+
if (schema.items !== void 0 && !Array.isArray(schema.items)) {
|
|
189
|
+
const itemType = getTypeScriptType(schema.items, options);
|
|
190
|
+
const wrappedItemType = wrapUnion(itemType);
|
|
191
|
+
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`;
|
|
152
192
|
}
|
|
153
|
-
|
|
154
|
-
|
|
193
|
+
return options.readonly ? "readonly unknown[]" : "unknown[]";
|
|
194
|
+
};
|
|
195
|
+
const getIndexSignature = (schema, options) => {
|
|
196
|
+
const additionalProperties = schema.additionalProperties;
|
|
197
|
+
if (additionalProperties !== void 0 && typeof additionalProperties !== "boolean") {
|
|
198
|
+
return { key: "string", value: getTypeScriptType(additionalProperties, options) };
|
|
155
199
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
return enumUnion;
|
|
200
|
+
const patternProperties = schema.patternProperties;
|
|
201
|
+
if (patternProperties && typeof patternProperties === "object") {
|
|
202
|
+
const entries = Object.entries(patternProperties);
|
|
203
|
+
if (entries.length === 0)
|
|
204
|
+
return void 0;
|
|
205
|
+
const valueTypes = entries.map(([, value]) => typeof value === "boolean" ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options));
|
|
206
|
+
const key = entries.length === 1 && entries[0]?.[0] === "^x-" ? "`x-${string}`" : "string";
|
|
207
|
+
return { key, value: unionOf(valueTypes) };
|
|
165
208
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
209
|
+
return void 0;
|
|
210
|
+
};
|
|
211
|
+
const buildIndexSignatureLine = (index, declaredTypes, hasOptionalProperty, options) => {
|
|
212
|
+
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
213
|
+
if (index.key !== "string")
|
|
214
|
+
return `${readonlyPrefix}[key: ${index.key}]: ${index.value}`;
|
|
215
|
+
const widened = unionOf([
|
|
216
|
+
...index.value.split(" | "),
|
|
217
|
+
...declaredTypes,
|
|
218
|
+
...hasOptionalProperty ? ["undefined"] : []
|
|
219
|
+
]);
|
|
220
|
+
return `${readonlyPrefix}[key: string]: ${widened}`;
|
|
221
|
+
};
|
|
222
|
+
const propertyDescription = (propSchema) => {
|
|
223
|
+
if (!isSchemaObject(propSchema))
|
|
224
|
+
return void 0;
|
|
225
|
+
if (typeof propSchema.description === "string")
|
|
226
|
+
return propSchema.description;
|
|
227
|
+
if (typeof propSchema.$comment === "string")
|
|
228
|
+
return propSchema.$comment;
|
|
229
|
+
return void 0;
|
|
230
|
+
};
|
|
231
|
+
const objectTypeToTs = (schema, options) => {
|
|
232
|
+
const index = getIndexSignature(schema, options);
|
|
233
|
+
if (schema.properties && Object.keys(schema.properties).length > 0) {
|
|
234
|
+
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
235
|
+
const requiredSet = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
236
|
+
const hasDescriptions = Object.values(schema.properties).some((p) => propertyDescription(p) !== void 0);
|
|
237
|
+
const declaredTypes = [];
|
|
238
|
+
let hasOptionalProperty = false;
|
|
239
|
+
const entries = [];
|
|
240
|
+
for (const key in schema.properties) {
|
|
241
|
+
const propSchema = schema.properties[key];
|
|
242
|
+
const isRequired = requiredSet.has(key);
|
|
243
|
+
const optional = isRequired ? "" : "?";
|
|
244
|
+
if (!isRequired)
|
|
245
|
+
hasOptionalProperty = true;
|
|
246
|
+
const propType = getTypeScriptType(propSchema, options);
|
|
247
|
+
declaredTypes.push(propType);
|
|
248
|
+
const declaration = readonlyPrefix + safeKey(key) + optional + ": " + propType;
|
|
249
|
+
if (!hasDescriptions) {
|
|
250
|
+
entries.push(declaration);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const inlineDescription = propertyDescription(propSchema);
|
|
254
|
+
entries.push((inlineDescription ? buildInlinePropertyComment(inlineDescription) : "") + " " + declaration + ";");
|
|
170
255
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
let anyOfUnion = getTypeScriptType(schema.anyOf[0], options);
|
|
175
|
-
for (let i = 1; i < schema.anyOf.length; i++) {
|
|
176
|
-
anyOfUnion += " | " + getTypeScriptType(schema.anyOf[i], options);
|
|
256
|
+
if (index) {
|
|
257
|
+
const line = buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options);
|
|
258
|
+
entries.push(hasDescriptions ? " " + line + ";" : line);
|
|
177
259
|
}
|
|
178
|
-
|
|
260
|
+
if (hasDescriptions)
|
|
261
|
+
return "{\n" + entries.join("\n") + "\n}";
|
|
262
|
+
return "{ " + entries.join("; ") + " }";
|
|
179
263
|
}
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
for (let i = 1; i < schema.allOf.length; i++) {
|
|
183
|
-
intersectionTypes += " & " + getTypeScriptType(schema.allOf[i], options);
|
|
184
|
-
}
|
|
185
|
-
return intersectionTypes;
|
|
264
|
+
if (index) {
|
|
265
|
+
return index.key === "string" && !options.readonly ? recordType("string", index.value, options) : recordType(index.key, index.value, options);
|
|
186
266
|
}
|
|
267
|
+
return "object";
|
|
268
|
+
};
|
|
269
|
+
const getLocalShapeType = (schema, options) => {
|
|
187
270
|
const conditionalResult = getConditionalObjectSchema(schema);
|
|
188
271
|
if (conditionalResult) {
|
|
189
|
-
const baseType =
|
|
190
|
-
|
|
191
|
-
return `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}`;
|
|
192
|
-
}
|
|
193
|
-
return baseType;
|
|
272
|
+
const baseType = objectTypeToTs(conditionalResult.schema, options);
|
|
273
|
+
return conditionalResult.thenRef ? `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}` : baseType;
|
|
194
274
|
}
|
|
195
275
|
if (!schema.type) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
return recordType("string",
|
|
201
|
-
}
|
|
202
|
-
if (schema.patternProperties && typeof schema.patternProperties === "object") {
|
|
203
|
-
const record = patternPropertiesRecordType(schema.patternProperties, options);
|
|
204
|
-
if (record !== void 0)
|
|
205
|
-
return record;
|
|
276
|
+
const index = getIndexSignature(schema, options);
|
|
277
|
+
if (index)
|
|
278
|
+
return objectTypeToTs(schema, options);
|
|
279
|
+
if (schema.additionalProperties !== void 0 && typeof schema.additionalProperties === "boolean") {
|
|
280
|
+
return recordType("string", getBooleanSubSchemaType(schema.additionalProperties), options);
|
|
206
281
|
}
|
|
282
|
+
if (schema.properties && Object.keys(schema.properties).length > 0)
|
|
283
|
+
return objectTypeToTs(schema, options);
|
|
207
284
|
if (schema.default !== void 0) {
|
|
208
|
-
if (typeof schema.default === "string")
|
|
285
|
+
if (typeof schema.default === "string")
|
|
209
286
|
return "string";
|
|
210
|
-
|
|
211
|
-
if (typeof schema.default === "number") {
|
|
287
|
+
if (typeof schema.default === "number")
|
|
212
288
|
return "number";
|
|
213
|
-
|
|
214
|
-
if (typeof schema.default === "boolean") {
|
|
289
|
+
if (typeof schema.default === "boolean")
|
|
215
290
|
return "boolean";
|
|
216
|
-
}
|
|
217
291
|
}
|
|
218
|
-
return
|
|
292
|
+
return void 0;
|
|
219
293
|
}
|
|
220
294
|
if (Array.isArray(schema.type)) {
|
|
221
|
-
|
|
222
|
-
switch (t) {
|
|
223
|
-
case "string":
|
|
224
|
-
return "string";
|
|
225
|
-
case "number":
|
|
226
|
-
case "integer":
|
|
227
|
-
return "number";
|
|
228
|
-
case "boolean":
|
|
229
|
-
return "boolean";
|
|
230
|
-
case "null":
|
|
231
|
-
return "null";
|
|
232
|
-
case "array":
|
|
233
|
-
return "unknown[]";
|
|
234
|
-
case "object":
|
|
235
|
-
return "Record<string, unknown>";
|
|
236
|
-
default:
|
|
237
|
-
return "unknown";
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
let typeUnion = mapType(schema.type[0]);
|
|
241
|
-
for (let i = 1; i < schema.type.length; i++) {
|
|
242
|
-
typeUnion += " | " + mapType(schema.type[i]);
|
|
243
|
-
}
|
|
244
|
-
return typeUnion;
|
|
295
|
+
return unionOf(schema.type.map((type) => singleTypeToTs(schema, type, options)));
|
|
245
296
|
}
|
|
246
|
-
|
|
247
|
-
|
|
297
|
+
return singleTypeToTs(schema, schema.type, options);
|
|
298
|
+
};
|
|
299
|
+
const singleTypeToTs = (schema, type, options) => {
|
|
300
|
+
switch (type) {
|
|
248
301
|
case "string":
|
|
249
302
|
return "string";
|
|
250
|
-
// Number
|
|
251
303
|
case "number":
|
|
252
304
|
case "integer":
|
|
253
305
|
return "number";
|
|
254
|
-
// Boolean
|
|
255
306
|
case "boolean":
|
|
256
307
|
return "boolean";
|
|
257
|
-
|
|
308
|
+
case "null":
|
|
309
|
+
return "null";
|
|
258
310
|
case "array":
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return options.readonly ? "readonly unknown[]" : "unknown[]";
|
|
265
|
-
// Object
|
|
266
|
-
case "object":
|
|
267
|
-
if (schema.properties) {
|
|
268
|
-
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
269
|
-
const requiredSet = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
270
|
-
const hasDescriptions = Object.values(schema.properties).some((p) => isSchemaObject(p) && (typeof p.description === "string" || typeof p.$comment === "string"));
|
|
271
|
-
if (hasDescriptions) {
|
|
272
|
-
let properties2 = "";
|
|
273
|
-
let first2 = true;
|
|
274
|
-
for (const key in schema.properties) {
|
|
275
|
-
const propSchema = schema.properties[key];
|
|
276
|
-
const isRequired = requiredSet.has(key);
|
|
277
|
-
const optional = isRequired ? "" : "?";
|
|
278
|
-
const propType = getTypeScriptType(propSchema, options);
|
|
279
|
-
const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === "string" ? propSchema.description : isSchemaObject(propSchema) && typeof propSchema.$comment === "string" ? propSchema.$comment : void 0;
|
|
280
|
-
if (!first2)
|
|
281
|
-
properties2 += "\n";
|
|
282
|
-
first2 = false;
|
|
283
|
-
if (inlineDescription) {
|
|
284
|
-
properties2 += buildInlinePropertyComment(inlineDescription) + " " + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
|
|
285
|
-
} else {
|
|
286
|
-
properties2 += " " + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return "{\n" + properties2 + "\n}";
|
|
290
|
-
}
|
|
291
|
-
let properties = "";
|
|
292
|
-
let first = true;
|
|
293
|
-
for (const key in schema.properties) {
|
|
294
|
-
const propSchema = schema.properties[key];
|
|
295
|
-
const isRequired = requiredSet.has(key);
|
|
296
|
-
const optional = isRequired ? "" : "?";
|
|
297
|
-
const propType = getTypeScriptType(propSchema, options);
|
|
298
|
-
if (!first)
|
|
299
|
-
properties += "; ";
|
|
300
|
-
properties += readonlyPrefix + safeKey(key) + optional + ": " + propType;
|
|
301
|
-
first = false;
|
|
302
|
-
}
|
|
303
|
-
return "{ " + properties + " }";
|
|
304
|
-
}
|
|
305
|
-
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
|
306
|
-
const additionalPropType = getTypeScriptType(schema.additionalProperties, options);
|
|
307
|
-
return recordType("string", additionalPropType, options);
|
|
308
|
-
}
|
|
309
|
-
if (schema.patternProperties && typeof schema.patternProperties === "object") {
|
|
310
|
-
const record = patternPropertiesRecordType(schema.patternProperties, options);
|
|
311
|
-
if (record !== void 0)
|
|
312
|
-
return record;
|
|
313
|
-
}
|
|
314
|
-
return "object";
|
|
315
|
-
// Default to unknown
|
|
311
|
+
return arrayTypeToTs(schema, options);
|
|
312
|
+
case "object": {
|
|
313
|
+
const rendered = objectTypeToTs(schema, options);
|
|
314
|
+
return rendered === "object" && Array.isArray(schema.type) ? recordType("string", "unknown", options) : rendered;
|
|
315
|
+
}
|
|
316
316
|
default:
|
|
317
317
|
return "unknown";
|
|
318
318
|
}
|
|
319
319
|
};
|
|
320
|
+
const getUnbrandedType = (schema, options = {}) => {
|
|
321
|
+
if (typeof schema === "boolean") {
|
|
322
|
+
return getBooleanSubSchemaType(schema);
|
|
323
|
+
}
|
|
324
|
+
if (typeof schema !== "object" || schema === null) {
|
|
325
|
+
return "unknown";
|
|
326
|
+
}
|
|
327
|
+
const instanceOf = getMjstInstanceOf(schema);
|
|
328
|
+
if (instanceOf) {
|
|
329
|
+
return instanceOf;
|
|
330
|
+
}
|
|
331
|
+
const primitive = getMjstPrimitive(schema);
|
|
332
|
+
if (primitive) {
|
|
333
|
+
return primitive;
|
|
334
|
+
}
|
|
335
|
+
if (schema.$ref) {
|
|
336
|
+
return refTypeName(schema.$ref, options);
|
|
337
|
+
}
|
|
338
|
+
if (schema.$dynamicRef) {
|
|
339
|
+
return "unknown";
|
|
340
|
+
}
|
|
341
|
+
if (schema.const !== void 0) {
|
|
342
|
+
return JSON.stringify(schema.const);
|
|
343
|
+
}
|
|
344
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
345
|
+
return unionOf(schema.enum.map((value) => JSON.stringify(value)));
|
|
346
|
+
}
|
|
347
|
+
const members = [];
|
|
348
|
+
const localType = getLocalShapeType(schema, options);
|
|
349
|
+
if (localType !== void 0)
|
|
350
|
+
members.push(localType);
|
|
351
|
+
if (Array.isArray(schema.allOf)) {
|
|
352
|
+
for (const entry of schema.allOf)
|
|
353
|
+
members.push(wrapUnion(getTypeScriptType(entry, options)));
|
|
354
|
+
}
|
|
355
|
+
const branches = Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : void 0;
|
|
356
|
+
const anyBranches = Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : void 0;
|
|
357
|
+
const unionBranches = branches ?? anyBranches;
|
|
358
|
+
if (unionBranches) {
|
|
359
|
+
const union = unionOf(unionBranches.map((branch) => getTypeScriptType(branch, options)));
|
|
360
|
+
if (members.length === 0 && !Array.isArray(schema.allOf))
|
|
361
|
+
return union;
|
|
362
|
+
members.push(wrapUnion(union));
|
|
363
|
+
}
|
|
364
|
+
if (members.length === 0)
|
|
365
|
+
return "unknown";
|
|
366
|
+
return intersectionOf(members);
|
|
367
|
+
};
|
|
368
|
+
const getCompositionMembers = (schema, options) => {
|
|
369
|
+
if (!isSchemaObject(schema))
|
|
370
|
+
return [];
|
|
371
|
+
const members = [];
|
|
372
|
+
if (Array.isArray(schema.allOf)) {
|
|
373
|
+
for (const entry of schema.allOf)
|
|
374
|
+
members.push(wrapUnion(getTypeScriptType(entry, options)));
|
|
375
|
+
}
|
|
376
|
+
if (typeof schema.$ref === "string") {
|
|
377
|
+
members.push(refTypeName(schema.$ref, options));
|
|
378
|
+
}
|
|
379
|
+
const unionBranches = Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : void 0;
|
|
380
|
+
if (unionBranches) {
|
|
381
|
+
members.push(wrapUnion(unionOf(unionBranches.map((branch) => getTypeScriptType(branch, options)))));
|
|
382
|
+
}
|
|
383
|
+
return [...new Set(members)].filter((member) => member !== "unknown");
|
|
384
|
+
};
|
|
320
385
|
const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
321
386
|
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
322
|
-
if (!isObjectLikeSchema(schema)) {
|
|
387
|
+
if (!isObjectLikeSchema(schema) || isSchemaObject(schema) && Array.isArray(schema.type)) {
|
|
323
388
|
const tsType = getTypeScriptType(schema, options);
|
|
324
389
|
let result = "";
|
|
325
390
|
const topLevelComment = isSchemaObject(schema) && typeof schema.description === "string" && schema.description || isSchemaObject(schema) && typeof schema.$comment === "string" && schema.$comment || void 0;
|
|
@@ -365,46 +430,45 @@ const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
365
430
|
}
|
|
366
431
|
const schemaProps = normalizedSchema.properties ?? {};
|
|
367
432
|
const requiredSet = new Set(Array.isArray(normalizedSchema.required) ? normalizedSchema.required : []);
|
|
433
|
+
const declaredTypes = [];
|
|
434
|
+
let hasOptionalProperty = false;
|
|
368
435
|
let properties = "";
|
|
369
436
|
let isFirstProp = true;
|
|
437
|
+
const appendLine = (line) => {
|
|
438
|
+
if (!isFirstProp)
|
|
439
|
+
properties += "\n";
|
|
440
|
+
isFirstProp = false;
|
|
441
|
+
properties += line;
|
|
442
|
+
};
|
|
370
443
|
for (const key in schemaProps) {
|
|
371
444
|
const propSchema = schemaProps[key];
|
|
372
445
|
const isRequired = requiredSet.has(key);
|
|
373
446
|
const optional = isRequired ? "" : "?";
|
|
447
|
+
if (!isRequired)
|
|
448
|
+
hasOptionalProperty = true;
|
|
374
449
|
const propType = getTypeScriptType(propSchema, options);
|
|
450
|
+
declaredTypes.push(propType);
|
|
375
451
|
const quotedKey = readonlyPrefix + safeKey(key);
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
isFirstProp = false;
|
|
379
|
-
const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === "string" ? propSchema.description : isSchemaObject(propSchema) && typeof propSchema.$comment === "string" ? propSchema.$comment : void 0;
|
|
380
|
-
if (inlineDescription) {
|
|
381
|
-
properties += buildInlinePropertyComment(inlineDescription) + " " + quotedKey + optional + ": " + propType + ";";
|
|
382
|
-
} else {
|
|
383
|
-
properties += " " + quotedKey + optional + ": " + propType + ";";
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
const allOfIntersections = [];
|
|
387
|
-
if (isSchemaObject(schema) && Array.isArray(schema.allOf)) {
|
|
388
|
-
for (const entry of schema.allOf) {
|
|
389
|
-
if (isSchemaObject(entry) && entry.$ref) {
|
|
390
|
-
allOfIntersections.push(refToName(entry.$ref, options.typeSuffix));
|
|
391
|
-
}
|
|
392
|
-
}
|
|
452
|
+
const inlineDescription = propertyDescription(propSchema);
|
|
453
|
+
appendLine(inlineDescription ? buildInlinePropertyComment(inlineDescription) + " " + quotedKey + optional + ": " + propType + ";" : " " + quotedKey + optional + ": " + propType + ";");
|
|
393
454
|
}
|
|
394
|
-
|
|
395
|
-
|
|
455
|
+
const index = getIndexSignature(normalizedSchema, options);
|
|
456
|
+
if (index) {
|
|
457
|
+
appendLine(" " + buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options) + ";");
|
|
396
458
|
}
|
|
397
459
|
let result = "";
|
|
398
460
|
if (jsDocTitle && jsDocDescription) {
|
|
399
461
|
result += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
400
462
|
}
|
|
401
|
-
let typeBody = "{\n" + properties + "\n}";
|
|
463
|
+
let typeBody = properties === "" ? "{}" : "{\n" + properties + "\n}";
|
|
402
464
|
if (conditionalThenRef) {
|
|
403
465
|
typeBody += " & " + refToName(conditionalThenRef, options.typeSuffix);
|
|
404
466
|
}
|
|
405
|
-
for (const intersectionType of
|
|
467
|
+
for (const intersectionType of getCompositionMembers(schema, options)) {
|
|
406
468
|
typeBody += " & " + intersectionType;
|
|
407
469
|
}
|
|
470
|
+
if (isNullableSchema(schema))
|
|
471
|
+
typeBody = withNull(typeBody);
|
|
408
472
|
result += "export type " + typeName + " = " + typeBody + ";";
|
|
409
473
|
return result;
|
|
410
474
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Folds documents the caller has already loaded into the document being
|
|
3
|
+
* generated, so a `$ref` that names another document resolves like any other.
|
|
4
|
+
*
|
|
5
|
+
* The generators are a single-document pipeline by construction: `$id` is applied
|
|
6
|
+
* once by {@link normalizeRefScopes}, every ref becomes a pointer from the
|
|
7
|
+
* document root, and the ref-graph walk, the filename and type-name derivation
|
|
8
|
+
* and the emitted import graph all work in that one currency. A ref to a
|
|
9
|
+
* *separate* resource had nowhere to land in that model — not because the URI
|
|
10
|
+
* could not be resolved, but because the bytes were somewhere else — so
|
|
11
|
+
* generation stopped.
|
|
12
|
+
*
|
|
13
|
+
* Grafting closes that gap without teaching any of the machinery a second
|
|
14
|
+
* addressing mode. Each registered document is embedded in the root's `$defs` as
|
|
15
|
+
* what it already is — a schema resource carrying its own base URI — and from
|
|
16
|
+
* that point on it *is* part of the document: the registry walk registers its
|
|
17
|
+
* `$id`, its anchors and its own embedded resources, `normalizeRefScopes`
|
|
18
|
+
* rewrites refs into it to plain pointers, and the walker gives it files and type
|
|
19
|
+
* names by the ordinary rules. Cross-document `$ref`, `$anchor` and
|
|
20
|
+
* `$dynamicAnchor` all start working at once, and nothing downstream had to
|
|
21
|
+
* change.
|
|
22
|
+
*
|
|
23
|
+
* This is the build-time counterpart of `@amritk/runtime-validators`'
|
|
24
|
+
* `ValidateOptions.schemas`, and it keeps the same promise: no I/O. The documents
|
|
25
|
+
* are handed over, never fetched — loading them is the caller's job (or
|
|
26
|
+
* `@amritk/resolve-refs`'). A URI nobody registered is still refused, loudly, at
|
|
27
|
+
* generation time.
|
|
28
|
+
*
|
|
29
|
+
* Registered documents that nothing references cost nothing in the output: the
|
|
30
|
+
* ref-graph walk only emits files for definitions it can actually reach, so a
|
|
31
|
+
* caller can register a whole directory and ship only what the schema uses.
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* A grafted document: the root to carry on with, and the `$defs` names the graft
|
|
35
|
+
* introduced.
|
|
36
|
+
*
|
|
37
|
+
* The names matter downstream because a registered document is *not* quite an
|
|
38
|
+
* authored one: a caller is meant to be able to over-register, so the ones
|
|
39
|
+
* nothing references have to be identifiable in order to be dropped again (see
|
|
40
|
+
* {@link pruneExternalSchemas}).
|
|
41
|
+
*/
|
|
42
|
+
export type GraftedSchema = {
|
|
43
|
+
readonly document: Record<string, unknown>;
|
|
44
|
+
readonly names: ReadonlySet<string>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Embeds each registered document into `root`'s `$defs`, returning a new root.
|
|
48
|
+
*
|
|
49
|
+
* The document keeps its own `$id` when it has one, and is given the retrieval
|
|
50
|
+
* URI as its `$id` when it does not — which is what makes a relative `$ref`
|
|
51
|
+
* *inside* a registered document resolve against the URI it was fetched from
|
|
52
|
+
* rather than against the root's base. When a document's `$id` disagrees with the
|
|
53
|
+
* URI it was registered under, both have to keep naming it, so a tiny alias
|
|
54
|
+
* resource (`{ $id: <retrieval URI>, $ref: <its own $id> }`) is embedded
|
|
55
|
+
* alongside it and refs to the retrieval URI land there.
|
|
56
|
+
*
|
|
57
|
+
* Every registered document is embedded, whether or not the schema mentions it —
|
|
58
|
+
* which of them are needed is not knowable until the base URIs have been applied
|
|
59
|
+
* and the refs are pointers. {@link pruneExternalSchemas} drops the rest once
|
|
60
|
+
* that is done.
|
|
61
|
+
*
|
|
62
|
+
* `root` is returned by identity when there is nothing to graft, so the
|
|
63
|
+
* overwhelmingly common no-registry call allocates nothing.
|
|
64
|
+
*
|
|
65
|
+
* @param root - The document being generated.
|
|
66
|
+
* @param schemas - Already-loaded documents, keyed by the absolute URI a `$ref`
|
|
67
|
+
* names them by.
|
|
68
|
+
* @returns The root document in which every registered URI is resolvable, and
|
|
69
|
+
* the `$defs` names the graft introduced.
|
|
70
|
+
* @throws When two registered URIs, or a registered URI and an existing `$defs`
|
|
71
|
+
* entry, want the same definition name — the name is what every emitted import
|
|
72
|
+
* is keyed on, so picking a winner would silently mistype one of them.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* graftExternalSchemas(
|
|
77
|
+
* { $ref: 'https://example.com/user.json' },
|
|
78
|
+
* { 'https://example.com/user.json': { type: 'object' } },
|
|
79
|
+
* ).document
|
|
80
|
+
* // → { $ref: 'https://example.com/user.json', $defs: { user: { $id: '…', type: 'object' } } }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare const graftExternalSchemas: (root: Record<string, unknown>, schemas: Readonly<Record<string, unknown>>) => GraftedSchema;
|