@kubb/ast 5.0.0-beta.30 → 5.0.0-beta.31
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/index.cjs +275 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +219 -100
- package/dist/index.js +272 -55
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dedupe.ts +202 -0
- package/src/index.ts +2 -0
- package/src/signature.ts +135 -0
- package/src/types.ts +1 -0
package/dist/index.cjs
CHANGED
|
@@ -224,60 +224,6 @@ const mediaTypes = {
|
|
|
224
224
|
videoMp4: "video/mp4"
|
|
225
225
|
};
|
|
226
226
|
//#endregion
|
|
227
|
-
//#region src/dialect.ts
|
|
228
|
-
/**
|
|
229
|
-
* Identity helper that types a {@link SchemaDialect} for an adapter. Like
|
|
230
|
-
* `defineParser`, it adds no runtime behavior — it pins the dialect's type for
|
|
231
|
-
* inference and gives adapter authors a discoverable anchor.
|
|
232
|
-
*
|
|
233
|
-
* @example
|
|
234
|
-
* ```ts
|
|
235
|
-
* export const oasDialect = defineSchemaDialect({
|
|
236
|
-
* name: 'oas',
|
|
237
|
-
* isNullable,
|
|
238
|
-
* isReference,
|
|
239
|
-
* isDiscriminator,
|
|
240
|
-
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
241
|
-
* resolveRef,
|
|
242
|
-
* })
|
|
243
|
-
* ```
|
|
244
|
-
*/
|
|
245
|
-
function defineSchemaDialect(dialect) {
|
|
246
|
-
return dialect;
|
|
247
|
-
}
|
|
248
|
-
//#endregion
|
|
249
|
-
//#region src/dispatch.ts
|
|
250
|
-
/**
|
|
251
|
-
* Walks an ordered list of {@link DispatchRule}s and returns the first node produced.
|
|
252
|
-
*
|
|
253
|
-
* This is the shared backbone for spec adapters (OpenAPI today, AsyncAPI and others later).
|
|
254
|
-
* The contract an adapter follows is intentionally minimal:
|
|
255
|
-
*
|
|
256
|
-
* context → [rule.match → rule.convert] → node
|
|
257
|
-
*
|
|
258
|
-
* An adapter derives a context from a source spec node, then declares an ordered table of
|
|
259
|
-
* rules mapping spec shapes onto Kubb AST nodes. To add support for a new spec, write a new
|
|
260
|
-
* context type and a new rules table — the traversal here is reused unchanged.
|
|
261
|
-
*
|
|
262
|
-
* Order is significant: earlier rules win, so list higher-precedence or more specific shapes
|
|
263
|
-
* first (e.g. composition keywords before plain `type`). A rule whose `match` returns `true`
|
|
264
|
-
* may still `convert` to `null` to defer to later rules. When no rule produces a node this
|
|
265
|
-
* returns `null`, leaving the caller to apply its own fallback.
|
|
266
|
-
*
|
|
267
|
-
* @example
|
|
268
|
-
* ```ts
|
|
269
|
-
* const node = dispatch(schemaRules, schemaContext) ?? createSchema({ type: fallbackType })
|
|
270
|
-
* ```
|
|
271
|
-
*/
|
|
272
|
-
function dispatch(rules, context) {
|
|
273
|
-
for (const rule of rules) {
|
|
274
|
-
if (!rule.match(context)) continue;
|
|
275
|
-
const node = rule.convert(context);
|
|
276
|
-
if (node !== null && node !== void 0) return node;
|
|
277
|
-
}
|
|
278
|
-
return null;
|
|
279
|
-
}
|
|
280
|
-
//#endregion
|
|
281
227
|
//#region ../../internals/utils/src/casing.ts
|
|
282
228
|
/**
|
|
283
229
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -2138,6 +2084,277 @@ function createJsx(value) {
|
|
|
2138
2084
|
};
|
|
2139
2085
|
}
|
|
2140
2086
|
//#endregion
|
|
2087
|
+
//#region src/signature.ts
|
|
2088
|
+
/**
|
|
2089
|
+
* The shape-affecting flags shared by every node kind: base primitive, format, and `nullable`.
|
|
2090
|
+
* Documentation and usage-slot flags (`optional`/`nullish`/`readOnly`/`writeOnly`) are
|
|
2091
|
+
* intentionally excluded — they describe the property slot, not the type.
|
|
2092
|
+
*/
|
|
2093
|
+
function flagsDescriptor(node) {
|
|
2094
|
+
return `${node.primitive ?? ""};${node.format ?? ""};${node.nullable ? 1 : 0}`;
|
|
2095
|
+
}
|
|
2096
|
+
function refTargetName(node) {
|
|
2097
|
+
if (node.ref) return extractRefName(node.ref);
|
|
2098
|
+
return node.name ?? "";
|
|
2099
|
+
}
|
|
2100
|
+
/**
|
|
2101
|
+
* Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
|
|
2102
|
+
* children's signatures. {@link signatureOf} hashes this string; children contribute their
|
|
2103
|
+
* fixed-length signature rather than their own full descriptor, which keeps the result bounded.
|
|
2104
|
+
*/
|
|
2105
|
+
function describeShape(node, signatures) {
|
|
2106
|
+
const flags = flagsDescriptor(node);
|
|
2107
|
+
switch (node.type) {
|
|
2108
|
+
case "object": {
|
|
2109
|
+
const props = (node.properties ?? []).map((prop) => `${prop.name}${prop.required ? "!" : "?"}${signatureOf(prop.schema, signatures)}`).join(",");
|
|
2110
|
+
let additional = "";
|
|
2111
|
+
if (typeof node.additionalProperties === "boolean") additional = `ab:${node.additionalProperties}`;
|
|
2112
|
+
else if (node.additionalProperties) additional = `as:${signatureOf(node.additionalProperties, signatures)}`;
|
|
2113
|
+
const pattern = node.patternProperties ? Object.keys(node.patternProperties).sort().map((key) => `${key}=${signatureOf(node.patternProperties[key], signatures)}`).join(",") : "";
|
|
2114
|
+
return `object|${flags}|p[${props}]|${additional}|pp[${pattern}]|mn:${node.minProperties ?? ""}|mx:${node.maxProperties ?? ""}`;
|
|
2115
|
+
}
|
|
2116
|
+
case "array":
|
|
2117
|
+
case "tuple": {
|
|
2118
|
+
const items = (node.items ?? []).map((item) => signatureOf(item, signatures)).join(",");
|
|
2119
|
+
const rest = node.rest ? signatureOf(node.rest, signatures) : "";
|
|
2120
|
+
return `${node.type}|${flags}|i[${items}]|r:${rest}|mn:${node.min ?? ""}|mx:${node.max ?? ""}|u:${node.unique ? 1 : 0}`;
|
|
2121
|
+
}
|
|
2122
|
+
case "union": {
|
|
2123
|
+
const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(",");
|
|
2124
|
+
return `union|${flags}|s:${node.strategy ?? ""}|d:${node.discriminatorPropertyName ?? ""}|m[${members}]`;
|
|
2125
|
+
}
|
|
2126
|
+
case "intersection": return `intersection|${flags}|m[${(node.members ?? []).map((member) => signatureOf(member, signatures)).join(",")}]`;
|
|
2127
|
+
case "enum": {
|
|
2128
|
+
let values = "";
|
|
2129
|
+
if (node.namedEnumValues?.length) values = node.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(",");
|
|
2130
|
+
else if (node.enumValues?.length) values = node.enumValues.map((value) => `${value === null ? "null" : typeof value}:${String(value)}`).join(",");
|
|
2131
|
+
return `enum|${flags}|v[${values}]`;
|
|
2132
|
+
}
|
|
2133
|
+
case "ref": return `ref|${flags}|->${refTargetName(node)}`;
|
|
2134
|
+
case "string": return `string|${flags}|mn:${node.min ?? ""}|mx:${node.max ?? ""}|pt:${node.pattern ?? ""}`;
|
|
2135
|
+
case "number":
|
|
2136
|
+
case "integer":
|
|
2137
|
+
case "bigint": return `${node.type}|${flags}|mn:${node.min ?? ""}|mx:${node.max ?? ""}|emn:${node.exclusiveMinimum ?? ""}|emx:${node.exclusiveMaximum ?? ""}|mo:${node.multipleOf ?? ""}`;
|
|
2138
|
+
case "url": return `url|${flags}|path:${node.path ?? ""}|mn:${node.min ?? ""}|mx:${node.max ?? ""}`;
|
|
2139
|
+
case "uuid":
|
|
2140
|
+
case "email": return `${node.type}|${flags}|mn:${node.min ?? ""}|mx:${node.max ?? ""}`;
|
|
2141
|
+
case "datetime": return `datetime|${flags}|o:${node.offset ? 1 : 0}|l:${node.local ? 1 : 0}`;
|
|
2142
|
+
case "date":
|
|
2143
|
+
case "time": return `${node.type}|${flags}|rep:${node.representation}`;
|
|
2144
|
+
default: return `${node.type}|${flags}`;
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
/**
|
|
2148
|
+
* Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
|
|
2149
|
+
* children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
|
|
2150
|
+
* full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
|
|
2151
|
+
* digest is identical across calls because it depends only on content — never on traversal
|
|
2152
|
+
* order. This keeps the keys built during planning consistent with the ones recomputed later
|
|
2153
|
+
* during streaming. `signatures` memoizes node → digest within a single computation.
|
|
2154
|
+
*/
|
|
2155
|
+
function signatureOf(node, signatures) {
|
|
2156
|
+
const cached = signatures.get(node);
|
|
2157
|
+
if (cached !== void 0) return cached;
|
|
2158
|
+
const signature = (0, node_crypto.createHash)("sha256").update(describeShape(node, signatures)).digest("hex");
|
|
2159
|
+
signatures.set(node, signature);
|
|
2160
|
+
return signature;
|
|
2161
|
+
}
|
|
2162
|
+
/**
|
|
2163
|
+
* Computes a deterministic, shape-only signature (a fixed-length content hash) for a schema node.
|
|
2164
|
+
*
|
|
2165
|
+
* Two schemas share a signature when they are structurally identical, ignoring
|
|
2166
|
+
* documentation (`name`, `title`, `description`, `example`, `default`, `deprecated`)
|
|
2167
|
+
* and usage-slot flags (`optional`, `nullish`, `readOnly`, `writeOnly`). `nullable`
|
|
2168
|
+
* is kept because it changes the produced type. `ref` nodes compare by target name,
|
|
2169
|
+
* which also keeps the algorithm terminating on circular shapes.
|
|
2170
|
+
*
|
|
2171
|
+
* @example Two enums with different descriptions share a signature
|
|
2172
|
+
* ```ts
|
|
2173
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
|
|
2174
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
|
|
2175
|
+
* ```
|
|
2176
|
+
*/
|
|
2177
|
+
function schemaSignature(node) {
|
|
2178
|
+
return signatureOf(node, /* @__PURE__ */ new Map());
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Returns `true` when two schema nodes are structurally identical under shape-only equality.
|
|
2182
|
+
*
|
|
2183
|
+
* @example
|
|
2184
|
+
* ```ts
|
|
2185
|
+
* isSchemaEqual(a, b) // a and b produce the same TypeScript type
|
|
2186
|
+
* ```
|
|
2187
|
+
*/
|
|
2188
|
+
function isSchemaEqual(a, b) {
|
|
2189
|
+
return schemaSignature(a) === schemaSignature(b);
|
|
2190
|
+
}
|
|
2191
|
+
//#endregion
|
|
2192
|
+
//#region src/dedupe.ts
|
|
2193
|
+
/**
|
|
2194
|
+
* Builds the shared `ref` replacement for a duplicate occurrence, carrying the
|
|
2195
|
+
* usage-slot and documentation fields that are not part of the canonical type.
|
|
2196
|
+
*/
|
|
2197
|
+
function createRefNode(node, canonical) {
|
|
2198
|
+
return createSchema({
|
|
2199
|
+
type: "ref",
|
|
2200
|
+
name: canonical.name,
|
|
2201
|
+
ref: canonical.ref,
|
|
2202
|
+
optional: node.optional,
|
|
2203
|
+
nullish: node.nullish,
|
|
2204
|
+
readOnly: node.readOnly,
|
|
2205
|
+
writeOnly: node.writeOnly,
|
|
2206
|
+
deprecated: node.deprecated,
|
|
2207
|
+
description: node.description,
|
|
2208
|
+
default: node.default,
|
|
2209
|
+
example: node.example
|
|
2210
|
+
});
|
|
2211
|
+
}
|
|
2212
|
+
function applyDedupe(node, canonicalBySignature, skipRootMatch = false) {
|
|
2213
|
+
if (canonicalBySignature.size === 0) return node;
|
|
2214
|
+
const signatures = /* @__PURE__ */ new Map();
|
|
2215
|
+
const root = node;
|
|
2216
|
+
return transform(node, { schema(schemaNode) {
|
|
2217
|
+
const signature = signatureOf(schemaNode, signatures);
|
|
2218
|
+
if (skipRootMatch && schemaNode === root) return void 0;
|
|
2219
|
+
const canonical = canonicalBySignature.get(signature);
|
|
2220
|
+
if (!canonical) return void 0;
|
|
2221
|
+
return createRefNode(schemaNode, canonical);
|
|
2222
|
+
} });
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
2225
|
+
* Strips usage-slot flags from a hoisted definition and applies its canonical name.
|
|
2226
|
+
* A standalone definition is never optional, so `optional`/`nullish` are cleared.
|
|
2227
|
+
*/
|
|
2228
|
+
function cleanDefinition(node, name) {
|
|
2229
|
+
return {
|
|
2230
|
+
...node,
|
|
2231
|
+
name,
|
|
2232
|
+
optional: void 0,
|
|
2233
|
+
nullish: void 0
|
|
2234
|
+
};
|
|
2235
|
+
}
|
|
2236
|
+
/**
|
|
2237
|
+
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
|
|
2238
|
+
*
|
|
2239
|
+
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
|
|
2240
|
+
* is a named top-level schema, that name becomes the canonical (so other top-level duplicates
|
|
2241
|
+
* and inline copies turn into references to it); otherwise a new definition is hoisted using
|
|
2242
|
+
* `nameFor`. The plan is then applied per node with {@link applyDedupe}.
|
|
2243
|
+
*
|
|
2244
|
+
* @example
|
|
2245
|
+
* ```ts
|
|
2246
|
+
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
|
|
2247
|
+
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
|
|
2248
|
+
* nameFor: (node) => node.name ?? null,
|
|
2249
|
+
* refFor: (name) => `#/components/schemas/${name}`,
|
|
2250
|
+
* })
|
|
2251
|
+
* ```
|
|
2252
|
+
*/
|
|
2253
|
+
function buildDedupePlan(roots, options) {
|
|
2254
|
+
const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options;
|
|
2255
|
+
const signatures = /* @__PURE__ */ new Map();
|
|
2256
|
+
const topLevelNodes = /* @__PURE__ */ new Set();
|
|
2257
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2258
|
+
function record(schemaNode) {
|
|
2259
|
+
const signature = signatureOf(schemaNode, signatures);
|
|
2260
|
+
if (!isCandidate(schemaNode)) return;
|
|
2261
|
+
const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name;
|
|
2262
|
+
const group = groups.get(signature);
|
|
2263
|
+
if (group) {
|
|
2264
|
+
group.count++;
|
|
2265
|
+
if (isTopLevel && !group.topLevelName) group.topLevelName = schemaNode.name;
|
|
2266
|
+
} else groups.set(signature, {
|
|
2267
|
+
count: 1,
|
|
2268
|
+
representative: schemaNode,
|
|
2269
|
+
topLevelName: isTopLevel ? schemaNode.name : void 0
|
|
2270
|
+
});
|
|
2271
|
+
}
|
|
2272
|
+
for (const root of roots) {
|
|
2273
|
+
if (root.kind === "Schema") topLevelNodes.add(root);
|
|
2274
|
+
for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode);
|
|
2275
|
+
}
|
|
2276
|
+
const canonicalBySignature = /* @__PURE__ */ new Map();
|
|
2277
|
+
const pendingHoists = [];
|
|
2278
|
+
for (const [signature, group] of groups) {
|
|
2279
|
+
if (group.count < minOccurrences) continue;
|
|
2280
|
+
if (group.topLevelName) {
|
|
2281
|
+
canonicalBySignature.set(signature, {
|
|
2282
|
+
name: group.topLevelName,
|
|
2283
|
+
ref: refFor(group.topLevelName)
|
|
2284
|
+
});
|
|
2285
|
+
continue;
|
|
2286
|
+
}
|
|
2287
|
+
const name = nameFor(group.representative, signature);
|
|
2288
|
+
if (!name) continue;
|
|
2289
|
+
canonicalBySignature.set(signature, {
|
|
2290
|
+
name,
|
|
2291
|
+
ref: refFor(name)
|
|
2292
|
+
});
|
|
2293
|
+
pendingHoists.push({
|
|
2294
|
+
name,
|
|
2295
|
+
representative: group.representative
|
|
2296
|
+
});
|
|
2297
|
+
}
|
|
2298
|
+
return {
|
|
2299
|
+
canonicalBySignature,
|
|
2300
|
+
hoisted: pendingHoists.map(({ name, representative }) => cleanDefinition(applyDedupe(representative, canonicalBySignature, true), name))
|
|
2301
|
+
};
|
|
2302
|
+
}
|
|
2303
|
+
//#endregion
|
|
2304
|
+
//#region src/dialect.ts
|
|
2305
|
+
/**
|
|
2306
|
+
* Identity helper that types a {@link SchemaDialect} for an adapter. Like
|
|
2307
|
+
* `defineParser`, it adds no runtime behavior — it pins the dialect's type for
|
|
2308
|
+
* inference and gives adapter authors a discoverable anchor.
|
|
2309
|
+
*
|
|
2310
|
+
* @example
|
|
2311
|
+
* ```ts
|
|
2312
|
+
* export const oasDialect = defineSchemaDialect({
|
|
2313
|
+
* name: 'oas',
|
|
2314
|
+
* isNullable,
|
|
2315
|
+
* isReference,
|
|
2316
|
+
* isDiscriminator,
|
|
2317
|
+
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
2318
|
+
* resolveRef,
|
|
2319
|
+
* })
|
|
2320
|
+
* ```
|
|
2321
|
+
*/
|
|
2322
|
+
function defineSchemaDialect(dialect) {
|
|
2323
|
+
return dialect;
|
|
2324
|
+
}
|
|
2325
|
+
//#endregion
|
|
2326
|
+
//#region src/dispatch.ts
|
|
2327
|
+
/**
|
|
2328
|
+
* Walks an ordered list of {@link DispatchRule}s and returns the first node produced.
|
|
2329
|
+
*
|
|
2330
|
+
* This is the shared backbone for spec adapters (OpenAPI today, AsyncAPI and others later).
|
|
2331
|
+
* The contract an adapter follows is intentionally minimal:
|
|
2332
|
+
*
|
|
2333
|
+
* context → [rule.match → rule.convert] → node
|
|
2334
|
+
*
|
|
2335
|
+
* An adapter derives a context from a source spec node, then declares an ordered table of
|
|
2336
|
+
* rules mapping spec shapes onto Kubb AST nodes. To add support for a new spec, write a new
|
|
2337
|
+
* context type and a new rules table — the traversal here is reused unchanged.
|
|
2338
|
+
*
|
|
2339
|
+
* Order is significant: earlier rules win, so list higher-precedence or more specific shapes
|
|
2340
|
+
* first (e.g. composition keywords before plain `type`). A rule whose `match` returns `true`
|
|
2341
|
+
* may still `convert` to `null` to defer to later rules. When no rule produces a node this
|
|
2342
|
+
* returns `null`, leaving the caller to apply its own fallback.
|
|
2343
|
+
*
|
|
2344
|
+
* @example
|
|
2345
|
+
* ```ts
|
|
2346
|
+
* const node = dispatch(schemaRules, schemaContext) ?? createSchema({ type: fallbackType })
|
|
2347
|
+
* ```
|
|
2348
|
+
*/
|
|
2349
|
+
function dispatch(rules, context) {
|
|
2350
|
+
for (const rule of rules) {
|
|
2351
|
+
if (!rule.match(context)) continue;
|
|
2352
|
+
const node = rule.convert(context);
|
|
2353
|
+
if (node !== null && node !== void 0) return node;
|
|
2354
|
+
}
|
|
2355
|
+
return null;
|
|
2356
|
+
}
|
|
2357
|
+
//#endregion
|
|
2141
2358
|
//#region src/printer.ts
|
|
2142
2359
|
/**
|
|
2143
2360
|
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
@@ -2354,6 +2571,8 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
2354
2571
|
return propNode;
|
|
2355
2572
|
}
|
|
2356
2573
|
//#endregion
|
|
2574
|
+
exports.applyDedupe = applyDedupe;
|
|
2575
|
+
exports.buildDedupePlan = buildDedupePlan;
|
|
2357
2576
|
exports.caseParams = caseParams;
|
|
2358
2577
|
exports.childName = childName;
|
|
2359
2578
|
exports.collect = collect;
|
|
@@ -2404,6 +2623,7 @@ exports.isInputNode = isInputNode;
|
|
|
2404
2623
|
exports.isOperationNode = isOperationNode;
|
|
2405
2624
|
exports.isOutputNode = isOutputNode;
|
|
2406
2625
|
exports.isScalarPrimitive = isScalarPrimitive;
|
|
2626
|
+
exports.isSchemaEqual = isSchemaEqual;
|
|
2407
2627
|
exports.isSchemaNode = isSchemaNode;
|
|
2408
2628
|
exports.isStringType = isStringType;
|
|
2409
2629
|
exports.mediaTypes = mediaTypes;
|
|
@@ -2412,6 +2632,7 @@ exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
|
|
|
2412
2632
|
exports.narrowSchema = narrowSchema;
|
|
2413
2633
|
exports.nodeKinds = nodeKinds;
|
|
2414
2634
|
exports.resolveRefName = resolveRefName;
|
|
2635
|
+
exports.schemaSignature = schemaSignature;
|
|
2415
2636
|
exports.schemaTypes = schemaTypes;
|
|
2416
2637
|
exports.setDiscriminatorEnum = setDiscriminatorEnum;
|
|
2417
2638
|
exports.setEnumName = setEnumName;
|