@kubb/ast 5.21.0 → 5.21.2
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 +76 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -8
- package/dist/index.d.ts +26 -8
- package/dist/index.js +71 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2100,6 +2100,75 @@ var nodes_exports = /* @__PURE__ */ __exportAll({
|
|
|
2100
2100
|
createText: () => createText,
|
|
2101
2101
|
createType: () => createType
|
|
2102
2102
|
});
|
|
2103
|
+
// src/json/index.ts
|
|
2104
|
+
var json_exports = /* @__PURE__ */ __exportAll({
|
|
2105
|
+
parse: () => parse,
|
|
2106
|
+
stringify: () => stringify
|
|
2107
|
+
});
|
|
2108
|
+
const version = 1;
|
|
2109
|
+
function isRef(value) {
|
|
2110
|
+
const node = value;
|
|
2111
|
+
return node?.kind === nodeKinds$1.schema && node.type === schemaTypes$1.ref;
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* Turns an `InputNode` into JSON text that `parse` reads back. Every schema a ref points at is written once,
|
|
2115
|
+
* where a plain `JSON.stringify(input)` writes it again at every ref.
|
|
2116
|
+
*
|
|
2117
|
+
* @example
|
|
2118
|
+
* ```ts
|
|
2119
|
+
* await writeFile('input.json', ast.json.stringify(input))
|
|
2120
|
+
* ```
|
|
2121
|
+
*/
|
|
2122
|
+
function stringify(input) {
|
|
2123
|
+
const ids = /* @__PURE__ */ new Map();
|
|
2124
|
+
const targets = [];
|
|
2125
|
+
function replacer(key, value) {
|
|
2126
|
+
if (key !== "schema" || !isRef(this)) return value;
|
|
2127
|
+
if (typeof value === "number") return [value];
|
|
2128
|
+
if (typeof value !== "object" || value === null) return value;
|
|
2129
|
+
if (!ids.has(value)) {
|
|
2130
|
+
ids.set(value, targets.length);
|
|
2131
|
+
targets.push(value);
|
|
2132
|
+
}
|
|
2133
|
+
return ids.get(value);
|
|
2134
|
+
}
|
|
2135
|
+
const root = JSON.stringify(input, replacer);
|
|
2136
|
+
const encoded = [];
|
|
2137
|
+
for (const target of targets) encoded.push(JSON.stringify(target, replacer));
|
|
2138
|
+
return `{"version":${version},"input":${root},"targets":[${encoded.join(",")}]}`;
|
|
2139
|
+
}
|
|
2140
|
+
/**
|
|
2141
|
+
* Reads the text `stringify` wrote back into an `InputNode`. Refs to one schema share one object again.
|
|
2142
|
+
* Throws on another format version, or on a ref whose target is missing.
|
|
2143
|
+
*/
|
|
2144
|
+
function parse(text) {
|
|
2145
|
+
const document = JSON.parse(text);
|
|
2146
|
+
if (document?.version !== version) throw new Error(`Cannot read this AST JSON: expected format version ${version}, got ${JSON.stringify(document?.version)}.`);
|
|
2147
|
+
if (!document.input || !Array.isArray(document.targets)) throw new Error("Cannot read this AST JSON: `input` or `targets` is missing.");
|
|
2148
|
+
const targets = document.targets;
|
|
2149
|
+
const stack = [document.input, ...targets];
|
|
2150
|
+
while (stack.length) {
|
|
2151
|
+
const value = stack.pop();
|
|
2152
|
+
const ref = isRef(value);
|
|
2153
|
+
for (const key in value) {
|
|
2154
|
+
const child = value[key];
|
|
2155
|
+
if (ref && key === "schema") {
|
|
2156
|
+
if (Array.isArray(child)) {
|
|
2157
|
+
value.schema = child[0];
|
|
2158
|
+
continue;
|
|
2159
|
+
}
|
|
2160
|
+
if (typeof child === "number") {
|
|
2161
|
+
const target = targets[child];
|
|
2162
|
+
if (!target) throw new Error(`Cannot read this AST JSON: a ref points at target ${child}, which is missing.`);
|
|
2163
|
+
value.schema = target;
|
|
2164
|
+
continue;
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
if (typeof child === "object" && child !== null) stack.push(child);
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
return document.input;
|
|
2171
|
+
}
|
|
2103
2172
|
// src/server/index.ts
|
|
2104
2173
|
var server_exports = /* @__PURE__ */ __exportAll({
|
|
2105
2174
|
get: () => get,
|
|
@@ -2140,6 +2209,7 @@ var src_exports = /* @__PURE__ */ __exportAll({
|
|
|
2140
2209
|
inlineFiles: () => inlineFiles,
|
|
2141
2210
|
isBareRef: () => isBareRef,
|
|
2142
2211
|
isHttpOperationNode: () => isHttpOperationNode,
|
|
2212
|
+
json: () => json_exports,
|
|
2143
2213
|
macro: () => macro_exports,
|
|
2144
2214
|
mediaType: () => mediaType_exports,
|
|
2145
2215
|
mergeRefWithSchema: () => mergeRefWithSchema,
|
|
@@ -2210,6 +2280,12 @@ exports.inferDiscriminatorPropertyName = inferDiscriminatorPropertyName;
|
|
|
2210
2280
|
exports.inlineFiles = inlineFiles;
|
|
2211
2281
|
exports.isBareRef = isBareRef;
|
|
2212
2282
|
exports.isHttpOperationNode = isHttpOperationNode;
|
|
2283
|
+
Object.defineProperty(exports, "json", {
|
|
2284
|
+
enumerable: true,
|
|
2285
|
+
get: function() {
|
|
2286
|
+
return json_exports;
|
|
2287
|
+
}
|
|
2288
|
+
});
|
|
2213
2289
|
Object.defineProperty(exports, "macro", {
|
|
2214
2290
|
enumerable: true,
|
|
2215
2291
|
get: function() {
|