@amritk/helpers 0.13.4 → 0.14.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/README.md +2 -1
- package/dist/fold-nullable.d.ts +19 -0
- package/dist/fold-nullable.js +42 -0
- package/dist/generate-type-definition.d.ts +8 -0
- package/dist/generate-type-definition.js +251 -184
- package/dist/walk-ref-graph.js +9 -1
- package/package.json +5 -1
- package/src/fold-nullable.ts +60 -0
- package/src/generate-type-definition.ts +416 -231
- package/src/walk-ref-graph.ts +18 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
**Shared schema-traversal and runtime helpers for the mjst code generation ecosystem.**
|
|
6
6
|
|
|
7
7
|

|
|
8
|
-

|
|
9
9
|

|
|
10
10
|

|
|
11
11
|

|
|
@@ -55,6 +55,7 @@ bun add @amritk/helpers
|
|
|
55
55
|
| Subpath | Exports | Purpose |
|
|
56
56
|
|:---|:---|:---|
|
|
57
57
|
| `@amritk/helpers/generate-type-definition` | `generateTypeDefinition` | Render a TypeScript type from a schema node. |
|
|
58
|
+
| `@amritk/helpers/mjst-extension` | `MJST_EXTENSION_KEY`, `getMjstInstanceOf`, `getMjstPrimitive`, `getMjstBrand` | Read the `x-mjst` vendor hints (`instanceOf`, `primitive`, `brand`) a schema carries. |
|
|
58
59
|
| `@amritk/helpers/parse-documentation` | `parseDocumentation`, `ObjectDocumentation` | Parse a markdown doc file into per-property descriptions. |
|
|
59
60
|
|
|
60
61
|
### Runtime helpers (also copied into generated output)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rewrites OpenAPI 3.0's `nullable: true` into the JSON Schema form the
|
|
3
|
+
* generators already enforce: a `null` member of the node's `type`.
|
|
4
|
+
*
|
|
5
|
+
* OpenAPI 3.0 has no `type: [...]` — it spells "or null" as a sibling boolean.
|
|
6
|
+
* Every generator here reads `type`, so an unfolded document produced parsers
|
|
7
|
+
* that rejected `null` outright (strict) or coerced it to a default (non-strict)
|
|
8
|
+
* while the schema declared it valid. Folding once, at the document level, means
|
|
9
|
+
* no downstream path needs to know the keyword exists.
|
|
10
|
+
*
|
|
11
|
+
* A node with `nullable: true` but no `type` is left alone: there is no type
|
|
12
|
+
* list to extend, and OpenAPI 3.0 ignores keywords alongside a `$ref` anyway.
|
|
13
|
+
* The `nullable` key itself is kept so the type generator can still widen the
|
|
14
|
+
* emitted TypeScript with `| null`.
|
|
15
|
+
*
|
|
16
|
+
* Returns the input unchanged (same reference) when the document contains no
|
|
17
|
+
* `nullable: true`, so the common case allocates nothing.
|
|
18
|
+
*/
|
|
19
|
+
export declare const foldNullable: <T>(schema: T) => T;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const foldNullable = (schema) => {
|
|
2
|
+
const folded = fold(schema);
|
|
3
|
+
return folded;
|
|
4
|
+
};
|
|
5
|
+
const fold = (node) => {
|
|
6
|
+
if (Array.isArray(node)) {
|
|
7
|
+
let changed2 = false;
|
|
8
|
+
const next2 = node.map((item) => {
|
|
9
|
+
const folded = fold(item);
|
|
10
|
+
if (folded !== item)
|
|
11
|
+
changed2 = true;
|
|
12
|
+
return folded;
|
|
13
|
+
});
|
|
14
|
+
return changed2 ? next2 : node;
|
|
15
|
+
}
|
|
16
|
+
if (typeof node !== "object" || node === null)
|
|
17
|
+
return node;
|
|
18
|
+
const record = node;
|
|
19
|
+
let changed = false;
|
|
20
|
+
const next = {};
|
|
21
|
+
for (const key in record) {
|
|
22
|
+
const value = record[key];
|
|
23
|
+
const folded = fold(value);
|
|
24
|
+
if (folded !== value)
|
|
25
|
+
changed = true;
|
|
26
|
+
next[key] = folded;
|
|
27
|
+
}
|
|
28
|
+
const type = record["type"];
|
|
29
|
+
if (record["nullable"] === true) {
|
|
30
|
+
if (typeof type === "string" && type !== "null") {
|
|
31
|
+
next["type"] = [type, "null"];
|
|
32
|
+
changed = true;
|
|
33
|
+
} else if (Array.isArray(type) && !type.includes("null")) {
|
|
34
|
+
next["type"] = [...type, "null"];
|
|
35
|
+
changed = true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return changed ? next : node;
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
foldNullable
|
|
42
|
+
};
|
|
@@ -8,6 +8,14 @@ export type TypeOptions = {
|
|
|
8
8
|
* Defaults to `''` (no suffix). Set to e.g. `'Object'` to emit `ContactObject`.
|
|
9
9
|
*/
|
|
10
10
|
readonly typeSuffix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The root document, used to decide whether a URI `$ref` names a type the
|
|
13
|
+
* generator also emits a file for. Without it such refs stay `unknown` — the
|
|
14
|
+
* conservative reading, since an unresolvable ref has no generated file to
|
|
15
|
+
* name. Pass it whenever the caller also emits imports for those refs, so the
|
|
16
|
+
* type and the import agree.
|
|
17
|
+
*/
|
|
18
|
+
readonly rootSchema?: Record<string, unknown>;
|
|
11
19
|
};
|
|
12
20
|
/**
|
|
13
21
|
* Generates a TypeScript type definition from a JSON Schema.
|
|
@@ -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,6 +156,167 @@ 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
|
};
|
|
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}?`);
|
|
176
|
+
}
|
|
177
|
+
if (rest !== false) {
|
|
178
|
+
const restType = rest === void 0 || rest === true ? "unknown" : wrapUnion(getTypeScriptType(rest, options));
|
|
179
|
+
parts.push(`...${restType}[]`);
|
|
180
|
+
}
|
|
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}[]`;
|
|
192
|
+
}
|
|
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) };
|
|
199
|
+
}
|
|
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) };
|
|
208
|
+
}
|
|
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 + ";");
|
|
255
|
+
}
|
|
256
|
+
if (index) {
|
|
257
|
+
const line = buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options);
|
|
258
|
+
entries.push(hasDescriptions ? " " + line + ";" : line);
|
|
259
|
+
}
|
|
260
|
+
if (hasDescriptions)
|
|
261
|
+
return "{\n" + entries.join("\n") + "\n}";
|
|
262
|
+
return "{ " + entries.join("; ") + " }";
|
|
263
|
+
}
|
|
264
|
+
if (index) {
|
|
265
|
+
return index.key === "string" && !options.readonly ? recordType("string", index.value, options) : recordType(index.key, index.value, options);
|
|
266
|
+
}
|
|
267
|
+
return "object";
|
|
268
|
+
};
|
|
269
|
+
const getLocalShapeType = (schema, options) => {
|
|
270
|
+
const conditionalResult = getConditionalObjectSchema(schema);
|
|
271
|
+
if (conditionalResult) {
|
|
272
|
+
const baseType = objectTypeToTs(conditionalResult.schema, options);
|
|
273
|
+
return conditionalResult.thenRef ? `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}` : baseType;
|
|
274
|
+
}
|
|
275
|
+
if (!schema.type) {
|
|
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);
|
|
281
|
+
}
|
|
282
|
+
if (schema.properties && Object.keys(schema.properties).length > 0)
|
|
283
|
+
return objectTypeToTs(schema, options);
|
|
284
|
+
if (schema.default !== void 0) {
|
|
285
|
+
if (typeof schema.default === "string")
|
|
286
|
+
return "string";
|
|
287
|
+
if (typeof schema.default === "number")
|
|
288
|
+
return "number";
|
|
289
|
+
if (typeof schema.default === "boolean")
|
|
290
|
+
return "boolean";
|
|
291
|
+
}
|
|
292
|
+
return void 0;
|
|
293
|
+
}
|
|
294
|
+
if (Array.isArray(schema.type)) {
|
|
295
|
+
return unionOf(schema.type.map((type) => singleTypeToTs(schema, type, options)));
|
|
296
|
+
}
|
|
297
|
+
return singleTypeToTs(schema, schema.type, options);
|
|
298
|
+
};
|
|
299
|
+
const singleTypeToTs = (schema, type, options) => {
|
|
300
|
+
switch (type) {
|
|
301
|
+
case "string":
|
|
302
|
+
return "string";
|
|
303
|
+
case "number":
|
|
304
|
+
case "integer":
|
|
305
|
+
return "number";
|
|
306
|
+
case "boolean":
|
|
307
|
+
return "boolean";
|
|
308
|
+
case "null":
|
|
309
|
+
return "null";
|
|
310
|
+
case "array":
|
|
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
|
+
default:
|
|
317
|
+
return "unknown";
|
|
318
|
+
}
|
|
319
|
+
};
|
|
126
320
|
const getUnbrandedType = (schema, options = {}) => {
|
|
127
321
|
if (typeof schema === "boolean") {
|
|
128
322
|
return getBooleanSubSchemaType(schema);
|
|
@@ -139,10 +333,7 @@ const getUnbrandedType = (schema, options = {}) => {
|
|
|
139
333
|
return primitive;
|
|
140
334
|
}
|
|
141
335
|
if (schema.$ref) {
|
|
142
|
-
|
|
143
|
-
return "unknown";
|
|
144
|
-
}
|
|
145
|
-
return refToName(schema.$ref, options.typeSuffix);
|
|
336
|
+
return refTypeName(schema.$ref, options);
|
|
146
337
|
}
|
|
147
338
|
if (schema.$dynamicRef) {
|
|
148
339
|
if (schema.$dynamicRef === "#meta") {
|
|
@@ -154,172 +345,49 @@ const getUnbrandedType = (schema, options = {}) => {
|
|
|
154
345
|
return JSON.stringify(schema.const);
|
|
155
346
|
}
|
|
156
347
|
if (schema.enum && schema.enum.length > 0) {
|
|
157
|
-
|
|
158
|
-
return JSON.stringify(schema.enum[0]);
|
|
159
|
-
}
|
|
160
|
-
let enumUnion = JSON.stringify(schema.enum[0]);
|
|
161
|
-
for (let i = 1; i < schema.enum.length; i++) {
|
|
162
|
-
enumUnion += " | " + JSON.stringify(schema.enum[i]);
|
|
163
|
-
}
|
|
164
|
-
return enumUnion;
|
|
165
|
-
}
|
|
166
|
-
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
167
|
-
let oneOfUnion = getTypeScriptType(schema.oneOf[0], options);
|
|
168
|
-
for (let i = 1; i < schema.oneOf.length; i++) {
|
|
169
|
-
oneOfUnion += " | " + getTypeScriptType(schema.oneOf[i], options);
|
|
170
|
-
}
|
|
171
|
-
return oneOfUnion;
|
|
172
|
-
}
|
|
173
|
-
if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
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);
|
|
177
|
-
}
|
|
178
|
-
return anyOfUnion;
|
|
348
|
+
return unionOf(schema.enum.map((value) => JSON.stringify(value)));
|
|
179
349
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
350
|
+
const members = [];
|
|
351
|
+
const localType = getLocalShapeType(schema, options);
|
|
352
|
+
if (localType !== void 0)
|
|
353
|
+
members.push(localType);
|
|
354
|
+
if (Array.isArray(schema.allOf)) {
|
|
355
|
+
for (const entry of schema.allOf)
|
|
356
|
+
members.push(wrapUnion(getTypeScriptType(entry, options)));
|
|
186
357
|
}
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
358
|
+
const branches = Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : void 0;
|
|
359
|
+
const anyBranches = Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : void 0;
|
|
360
|
+
const unionBranches = branches ?? anyBranches;
|
|
361
|
+
if (unionBranches) {
|
|
362
|
+
const union = unionOf(unionBranches.map((branch) => getTypeScriptType(branch, options)));
|
|
363
|
+
if (members.length === 0 && !Array.isArray(schema.allOf))
|
|
364
|
+
return union;
|
|
365
|
+
members.push(wrapUnion(union));
|
|
194
366
|
}
|
|
195
|
-
if (
|
|
196
|
-
if (schema.additionalProperties !== void 0) {
|
|
197
|
-
if (typeof schema.additionalProperties === "boolean") {
|
|
198
|
-
return recordType("string", getBooleanSubSchemaType(schema.additionalProperties), options);
|
|
199
|
-
}
|
|
200
|
-
return recordType("string", getTypeScriptType(schema.additionalProperties, options), options);
|
|
201
|
-
}
|
|
202
|
-
if (schema.patternProperties && typeof schema.patternProperties === "object") {
|
|
203
|
-
const record = patternPropertiesRecordType(schema.patternProperties, options);
|
|
204
|
-
if (record !== void 0)
|
|
205
|
-
return record;
|
|
206
|
-
}
|
|
207
|
-
if (schema.default !== void 0) {
|
|
208
|
-
if (typeof schema.default === "string") {
|
|
209
|
-
return "string";
|
|
210
|
-
}
|
|
211
|
-
if (typeof schema.default === "number") {
|
|
212
|
-
return "number";
|
|
213
|
-
}
|
|
214
|
-
if (typeof schema.default === "boolean") {
|
|
215
|
-
return "boolean";
|
|
216
|
-
}
|
|
217
|
-
}
|
|
367
|
+
if (members.length === 0)
|
|
218
368
|
return "unknown";
|
|
369
|
+
return intersectionOf(members);
|
|
370
|
+
};
|
|
371
|
+
const getCompositionMembers = (schema, options) => {
|
|
372
|
+
if (!isSchemaObject(schema))
|
|
373
|
+
return [];
|
|
374
|
+
const members = [];
|
|
375
|
+
if (Array.isArray(schema.allOf)) {
|
|
376
|
+
for (const entry of schema.allOf)
|
|
377
|
+
members.push(wrapUnion(getTypeScriptType(entry, options)));
|
|
219
378
|
}
|
|
220
|
-
if (
|
|
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;
|
|
379
|
+
if (typeof schema.$ref === "string") {
|
|
380
|
+
members.push(refTypeName(schema.$ref, options));
|
|
245
381
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return "string";
|
|
250
|
-
// Number
|
|
251
|
-
case "number":
|
|
252
|
-
case "integer":
|
|
253
|
-
return "number";
|
|
254
|
-
// Boolean
|
|
255
|
-
case "boolean":
|
|
256
|
-
return "boolean";
|
|
257
|
-
// Array
|
|
258
|
-
case "array":
|
|
259
|
-
if (schema.items) {
|
|
260
|
-
const itemType = getTypeScriptType(schema.items, options);
|
|
261
|
-
const wrappedItemType = itemType.includes(" | ") ? `(${itemType})` : itemType;
|
|
262
|
-
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`;
|
|
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
|
|
316
|
-
default:
|
|
317
|
-
return "unknown";
|
|
382
|
+
const unionBranches = Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : void 0;
|
|
383
|
+
if (unionBranches) {
|
|
384
|
+
members.push(wrapUnion(unionOf(unionBranches.map((branch) => getTypeScriptType(branch, options)))));
|
|
318
385
|
}
|
|
386
|
+
return [...new Set(members)].filter((member) => member !== "unknown");
|
|
319
387
|
};
|
|
320
388
|
const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
321
389
|
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
322
|
-
if (!isObjectLikeSchema(schema)) {
|
|
390
|
+
if (!isObjectLikeSchema(schema) || isSchemaObject(schema) && Array.isArray(schema.type)) {
|
|
323
391
|
const tsType = getTypeScriptType(schema, options);
|
|
324
392
|
let result = "";
|
|
325
393
|
const topLevelComment = isSchemaObject(schema) && typeof schema.description === "string" && schema.description || isSchemaObject(schema) && typeof schema.$comment === "string" && schema.$comment || void 0;
|
|
@@ -365,46 +433,45 @@ const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
365
433
|
}
|
|
366
434
|
const schemaProps = normalizedSchema.properties ?? {};
|
|
367
435
|
const requiredSet = new Set(Array.isArray(normalizedSchema.required) ? normalizedSchema.required : []);
|
|
436
|
+
const declaredTypes = [];
|
|
437
|
+
let hasOptionalProperty = false;
|
|
368
438
|
let properties = "";
|
|
369
439
|
let isFirstProp = true;
|
|
440
|
+
const appendLine = (line) => {
|
|
441
|
+
if (!isFirstProp)
|
|
442
|
+
properties += "\n";
|
|
443
|
+
isFirstProp = false;
|
|
444
|
+
properties += line;
|
|
445
|
+
};
|
|
370
446
|
for (const key in schemaProps) {
|
|
371
447
|
const propSchema = schemaProps[key];
|
|
372
448
|
const isRequired = requiredSet.has(key);
|
|
373
449
|
const optional = isRequired ? "" : "?";
|
|
450
|
+
if (!isRequired)
|
|
451
|
+
hasOptionalProperty = true;
|
|
374
452
|
const propType = getTypeScriptType(propSchema, options);
|
|
453
|
+
declaredTypes.push(propType);
|
|
375
454
|
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
|
-
}
|
|
455
|
+
const inlineDescription = propertyDescription(propSchema);
|
|
456
|
+
appendLine(inlineDescription ? buildInlinePropertyComment(inlineDescription) + " " + quotedKey + optional + ": " + propType + ";" : " " + quotedKey + optional + ": " + propType + ";");
|
|
393
457
|
}
|
|
394
|
-
|
|
395
|
-
|
|
458
|
+
const index = getIndexSignature(normalizedSchema, options);
|
|
459
|
+
if (index) {
|
|
460
|
+
appendLine(" " + buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options) + ";");
|
|
396
461
|
}
|
|
397
462
|
let result = "";
|
|
398
463
|
if (jsDocTitle && jsDocDescription) {
|
|
399
464
|
result += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
400
465
|
}
|
|
401
|
-
let typeBody = "{\n" + properties + "\n}";
|
|
466
|
+
let typeBody = properties === "" ? "{}" : "{\n" + properties + "\n}";
|
|
402
467
|
if (conditionalThenRef) {
|
|
403
468
|
typeBody += " & " + refToName(conditionalThenRef, options.typeSuffix);
|
|
404
469
|
}
|
|
405
|
-
for (const intersectionType of
|
|
470
|
+
for (const intersectionType of getCompositionMembers(schema, options)) {
|
|
406
471
|
typeBody += " & " + intersectionType;
|
|
407
472
|
}
|
|
473
|
+
if (isNullableSchema(schema))
|
|
474
|
+
typeBody = withNull(typeBody);
|
|
408
475
|
result += "export type " + typeName + " = " + typeBody + ";";
|
|
409
476
|
return result;
|
|
410
477
|
}
|
package/dist/walk-ref-graph.js
CHANGED
|
@@ -73,8 +73,10 @@ const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
|
73
73
|
const { upgraded, dynamicRefMap } = cache;
|
|
74
74
|
const processedRefs = /* @__PURE__ */ new Set();
|
|
75
75
|
const processedFilenames = /* @__PURE__ */ new Set();
|
|
76
|
+
const filenameOwners = /* @__PURE__ */ new Map();
|
|
76
77
|
const rootFilename = rootTypeName.toLowerCase();
|
|
77
78
|
processedFilenames.add(rootFilename);
|
|
79
|
+
filenameOwners.set(rootFilename, `the root type ${rootTypeName}`);
|
|
78
80
|
let rootNodeSchema = upgraded;
|
|
79
81
|
let rootNodeRef;
|
|
80
82
|
const aliasRef = getAliasRootRef(upgraded);
|
|
@@ -105,8 +107,14 @@ const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
|
105
107
|
continue;
|
|
106
108
|
}
|
|
107
109
|
const filename = refToFilename(ref);
|
|
108
|
-
if (
|
|
110
|
+
if (processedFilenames.has(filename)) {
|
|
111
|
+
const owner = filenameOwners.get(filename);
|
|
112
|
+
if (owner !== void 0 && owner !== ref) {
|
|
113
|
+
console.warn(`Warning: ${ref} and ${owner} both map to "${filename}.ts" \u2014 only ${owner} was generated, so every reference to the other resolves to the wrong type. Rename one so each gets its own file.`);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
109
116
|
processedFilenames.add(filename);
|
|
117
|
+
filenameOwners.set(filename, ref);
|
|
110
118
|
visit({
|
|
111
119
|
ref,
|
|
112
120
|
typeName: refToName(ref, typeSuffix),
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Shared utilities for the mjst code generation ecosystem.",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20"
|
|
9
|
+
},
|
|
6
10
|
"license": "MIT",
|
|
7
11
|
"author": "amritk",
|
|
8
12
|
"keywords": [
|