@kubb/adapter-oas 5.18.8 → 5.19.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/index.cjs +112 -78
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +113 -79
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { defineExtension } from "@kubb/core";
|
|
2
2
|
import { Diagnostics, ast, childName, enumPrimitive, enumPropName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
|
|
3
|
-
import {
|
|
3
|
+
import { access, realpath } from "node:fs/promises";
|
|
4
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
4
5
|
import { fileURLToPath } from "node:url";
|
|
5
6
|
import { readFile } from "node:fs";
|
|
6
|
-
import { access } from "node:fs/promises";
|
|
7
7
|
import { upgrade } from "@scalar/openapi-upgrader";
|
|
8
8
|
import { bundle } from "api-ref-bundler";
|
|
9
9
|
import { COLLECTION_STYLE, CORE_SCHEMA, EVENT_ID, SCALAR_STYLE, constructFromEvents, load, parseEvents } from "js-yaml";
|
|
@@ -14,7 +14,7 @@ var compatibility = {
|
|
|
14
14
|
"node": ">=22.22"
|
|
15
15
|
};
|
|
16
16
|
// package.json
|
|
17
|
-
var version = "5.
|
|
17
|
+
var version = "5.19.1";
|
|
18
18
|
// src/constants.ts
|
|
19
19
|
/**
|
|
20
20
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -222,6 +222,47 @@ function patchDiscriminatorNode(node, entry) {
|
|
|
222
222
|
members: newMembers
|
|
223
223
|
};
|
|
224
224
|
}
|
|
225
|
+
// ../../internals/utils/src/promise.ts
|
|
226
|
+
/**
|
|
227
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
228
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
229
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
233
|
+
*/
|
|
234
|
+
function memo(store, key, compute) {
|
|
235
|
+
const cached = store.get(key);
|
|
236
|
+
if (cached !== void 0 || store.has(key)) return cached;
|
|
237
|
+
const value = compute();
|
|
238
|
+
store.set(key, value);
|
|
239
|
+
return value;
|
|
240
|
+
}
|
|
241
|
+
function createStore({ weak, max } = {}) {
|
|
242
|
+
if (weak) return /* @__PURE__ */ new WeakMap();
|
|
243
|
+
const entry = /* @__PURE__ */ new Map();
|
|
244
|
+
if (max === void 0) return entry;
|
|
245
|
+
const set = (key, value) => {
|
|
246
|
+
if (entry.size >= max) entry.clear();
|
|
247
|
+
entry.set(key, value);
|
|
248
|
+
};
|
|
249
|
+
if (max !== 1) return {
|
|
250
|
+
has: (key) => entry.has(key),
|
|
251
|
+
get: (key) => entry.get(key),
|
|
252
|
+
set
|
|
253
|
+
};
|
|
254
|
+
const touch = (key) => {
|
|
255
|
+
if (!entry.has(key)) return false;
|
|
256
|
+
set(key, entry.get(key));
|
|
257
|
+
return true;
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
has: touch,
|
|
261
|
+
get: (key) => touch(key) ? entry.get(key) : void 0,
|
|
262
|
+
set
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
memo.create = createStore;
|
|
225
266
|
// ../../internals/utils/src/casing.ts
|
|
226
267
|
/**
|
|
227
268
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -236,16 +277,10 @@ function convert(text, pascal) {
|
|
|
236
277
|
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
237
278
|
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
238
279
|
}
|
|
239
|
-
const cache =
|
|
240
|
-
const cacheLimit = 1e5;
|
|
280
|
+
const cache = memo.create({ max: 1e5 });
|
|
241
281
|
function toCamelOrPascal(text, pascal) {
|
|
242
282
|
const key = `${pascal ? "P" : "c"}${text}`;
|
|
243
|
-
|
|
244
|
-
if (cached !== void 0) return cached;
|
|
245
|
-
const result = convert(text, pascal);
|
|
246
|
-
if (cache.size >= cacheLimit) cache.clear();
|
|
247
|
-
cache.set(key, result);
|
|
248
|
-
return result;
|
|
283
|
+
return memo(cache, key, () => convert(text, pascal));
|
|
249
284
|
}
|
|
250
285
|
/**
|
|
251
286
|
* Converts `text` to PascalCase.
|
|
@@ -363,42 +398,27 @@ async function read(path) {
|
|
|
363
398
|
if (runtime.isBun) return Bun.file(path).text();
|
|
364
399
|
return new Promise((resolvePromise, reject) => readFile(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
365
400
|
}
|
|
366
|
-
// ../../internals/utils/src/promise.ts
|
|
367
401
|
/**
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
402
|
+
* Resolves to `true` when `path` is `parent` itself or nested inside it. Both sides are resolved
|
|
403
|
+
* to absolute paths first, so relative and `..`-containing inputs compare correctly.
|
|
404
|
+
*
|
|
405
|
+
* Guards a destructive or an out-of-tree operation: before wiping an output directory, check that
|
|
406
|
+
* it does not contain the project root, and before loading a path a caller supplied, check that it
|
|
407
|
+
* did not escape the directory it is allowed to read from.
|
|
371
408
|
*
|
|
372
409
|
* @example
|
|
373
|
-
*
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const entry = /* @__PURE__ */ new Map();
|
|
385
|
-
if (type === "map") return entry;
|
|
386
|
-
const set = (key, value) => {
|
|
387
|
-
entry.clear();
|
|
388
|
-
entry.set(key, value);
|
|
389
|
-
};
|
|
390
|
-
const touch = (key) => {
|
|
391
|
-
if (!entry.has(key)) return false;
|
|
392
|
-
set(key, entry.get(key));
|
|
393
|
-
return true;
|
|
394
|
-
};
|
|
395
|
-
return {
|
|
396
|
-
has: touch,
|
|
397
|
-
get: (key) => touch(key) ? entry.get(key) : void 0,
|
|
398
|
-
set
|
|
399
|
-
};
|
|
410
|
+
* isPathInside('./src/gen', '.') // true — nested inside the root
|
|
411
|
+
* isPathInside('.', '.') // true — the same directory counts as inside
|
|
412
|
+
* isPathInside('.', './src/gen') // false — the root is not inside its own output
|
|
413
|
+
* isPathInside('../other', '.') // false — escapes the root
|
|
414
|
+
*/
|
|
415
|
+
function isPathInside(path, parent) {
|
|
416
|
+
const resolvedPath = resolve(path);
|
|
417
|
+
const resolvedParent = resolve(parent);
|
|
418
|
+
if (resolvedPath === resolvedParent) return true;
|
|
419
|
+
const rel = relative(resolvedParent, resolvedPath);
|
|
420
|
+
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
|
|
400
421
|
}
|
|
401
|
-
memo.create = createStore;
|
|
402
422
|
// src/oas.ts
|
|
403
423
|
/**
|
|
404
424
|
* Returns `true` when a schema should be treated as nullable.
|
|
@@ -537,7 +557,7 @@ function isJsonMimeType(mimeType) {
|
|
|
537
557
|
*/
|
|
538
558
|
function createRefs(document) {
|
|
539
559
|
const pointerCache = /* @__PURE__ */ new Map();
|
|
540
|
-
const resolvedNodeCache =
|
|
560
|
+
const resolvedNodeCache = memo.create();
|
|
541
561
|
const resolvingRefs = /* @__PURE__ */ new Set();
|
|
542
562
|
function walkPointer($ref) {
|
|
543
563
|
const trimmed = $ref.trim();
|
|
@@ -587,7 +607,7 @@ function createRefs(document) {
|
|
|
587
607
|
*/
|
|
588
608
|
function resolveNode(refPath, parse) {
|
|
589
609
|
if (resolvingRefs.has(refPath)) return null;
|
|
590
|
-
|
|
610
|
+
return memo(resolvedNodeCache, refPath, () => {
|
|
591
611
|
let resolved = null;
|
|
592
612
|
try {
|
|
593
613
|
const referenced = resolve(refPath);
|
|
@@ -597,9 +617,8 @@ function createRefs(document) {
|
|
|
597
617
|
resolvingRefs.delete(refPath);
|
|
598
618
|
}
|
|
599
619
|
} catch {}
|
|
600
|
-
|
|
601
|
-
}
|
|
602
|
-
return resolvedNodeCache.get(refPath) ?? null;
|
|
620
|
+
return resolved;
|
|
621
|
+
});
|
|
603
622
|
}
|
|
604
623
|
/**
|
|
605
624
|
* Resolves a `$ref` value without mutating anything: when `value` holds a `$ref`, returns the
|
|
@@ -706,8 +725,8 @@ function pick(declarations, pointer) {
|
|
|
706
725
|
* enclosing anchor, so a relative `$id` base or a recursively extended schema is not followed, upgrade when a spec needs that.
|
|
707
726
|
*/
|
|
708
727
|
function resolveSchemaIds(document) {
|
|
709
|
-
const targets =
|
|
710
|
-
const declare = (key, pointer) => targets
|
|
728
|
+
const targets = memo.create();
|
|
729
|
+
const declare = (key, pointer) => memo(targets, key, () => []).push(pointer);
|
|
711
730
|
walk(document, "#", (node, pointer) => {
|
|
712
731
|
if (typeof node["$id"] === "string") declare(node["$id"], pointer);
|
|
713
732
|
for (const key of ["$anchor", "$dynamicAnchor"]) if (typeof node[key] === "string") declare(`#${node[key]}`, pointer);
|
|
@@ -725,7 +744,7 @@ function resolveSchemaIds(document) {
|
|
|
725
744
|
}
|
|
726
745
|
// src/sourceLocator.ts
|
|
727
746
|
const flowGap = /(?:[\s,]|#[^\n]*)*/y;
|
|
728
|
-
const indexes = memo.create({
|
|
747
|
+
const indexes = memo.create({ max: 1 });
|
|
729
748
|
function toKeyText(text) {
|
|
730
749
|
if (!/^[-+.~\d]|^(?:null|true|false)$/i.test(text)) return text;
|
|
731
750
|
try {
|
|
@@ -1091,6 +1110,16 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1091
1110
|
}
|
|
1092
1111
|
}
|
|
1093
1112
|
/**
|
|
1113
|
+
* Whether `filePath` is inside one of `folders`. Links are followed first, so a link inside a
|
|
1114
|
+
* folder cannot point outside it.
|
|
1115
|
+
*/
|
|
1116
|
+
async function isInsideFolders(filePath, folders) {
|
|
1117
|
+
const real = (item) => realpath(resolve(item)).catch(() => resolve(item));
|
|
1118
|
+
const target = await real(filePath);
|
|
1119
|
+
const realFolders = await Promise.all(folders.map(real));
|
|
1120
|
+
return [...folders, ...realFolders].some((folder) => isPathInside(target, folder));
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1094
1123
|
* Bundles a multi-file OpenAPI document into a single document via `api-ref-bundler`.
|
|
1095
1124
|
*
|
|
1096
1125
|
* External file schemas are hoisted into named `components.schemas` entries, so a property
|
|
@@ -1107,6 +1136,7 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1107
1136
|
* against the OpenAPI 3.2 `$self` when the document sets one. Every other file or URL read,
|
|
1108
1137
|
* including one that fails, lands in `sources`. A `$ref` that cannot be read reports
|
|
1109
1138
|
* `KUBB_REF_NOT_FOUND` at the key that holds it, as a warning when it sits inside an `x-` extension.
|
|
1139
|
+
* So does any local file from a URL input, and a local file outside every `fileRefs` folder.
|
|
1110
1140
|
*
|
|
1111
1141
|
* @example Local file
|
|
1112
1142
|
* `const document = await bundleDocument('./openapi.yaml')`
|
|
@@ -1114,20 +1144,24 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1114
1144
|
* @example Remote URL
|
|
1115
1145
|
* `const document = await bundleDocument('https://example.com/openapi.yaml')`
|
|
1116
1146
|
*/
|
|
1117
|
-
async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
1118
|
-
const cache =
|
|
1147
|
+
async function bundleDocument(pathOrUrl, { document, sources = [], fileRefs } = {}) {
|
|
1148
|
+
const cache = memo.create();
|
|
1119
1149
|
const toKey = (sourcePath) => urlRegExp.test(sourcePath) ? new URL(sourcePath).href : sourcePath;
|
|
1150
|
+
const folders = urlRegExp.test(pathOrUrl) ? [] : fileRefs;
|
|
1151
|
+
const refused = /* @__PURE__ */ new Set();
|
|
1120
1152
|
let entry = pathOrUrl;
|
|
1121
1153
|
let entryDocument = document;
|
|
1122
|
-
const resolver = (sourcePath) => {
|
|
1123
|
-
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return
|
|
1154
|
+
const resolver = async (sourcePath) => {
|
|
1155
|
+
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return entryDocument;
|
|
1124
1156
|
const key = toKey(sourcePath);
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
cache
|
|
1130
|
-
|
|
1157
|
+
if (key !== toKey(pathOrUrl) && !urlRegExp.test(sourcePath) && folders && !await isInsideFolders(sourcePath, folders)) {
|
|
1158
|
+
refused.add(sourcePath);
|
|
1159
|
+
throw new Error(`Refused ${sourcePath}`);
|
|
1160
|
+
}
|
|
1161
|
+
return memo(cache, key, () => {
|
|
1162
|
+
sources.push(key);
|
|
1163
|
+
return resolveSource(sourcePath, { remember: key === toKey(pathOrUrl) });
|
|
1164
|
+
});
|
|
1131
1165
|
};
|
|
1132
1166
|
const root = await resolver(pathOrUrl);
|
|
1133
1167
|
const scan = resolveTargets(root);
|
|
@@ -1139,11 +1173,12 @@ async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
|
1139
1173
|
entryDocument = root;
|
|
1140
1174
|
return await bundle(entry, resolver, { hooks: { onError: (message, { path, state }) => {
|
|
1141
1175
|
const pointer = toKey(state.baseFile) === toKey(entry) ? `#/${path.map((token) => escapePointerToken(String(token))).join("/")}` : void 0;
|
|
1176
|
+
const refusedPath = [...refused].find((sourcePath) => message.includes(sourcePath));
|
|
1142
1177
|
const diagnostic = {
|
|
1143
1178
|
code: Diagnostics.code.refNotFound,
|
|
1144
1179
|
severity: isExtensionPath(path) ? "warning" : "error",
|
|
1145
|
-
message: `Could not bundle an external \`$ref\`. ${message}.`,
|
|
1146
|
-
help: "Check that the file or URL the `$ref` points at exists and is readable, or fix the `$ref`.",
|
|
1180
|
+
message: refusedPath ? `Refused an external \`$ref\` to the local file ${refusedPath}.` : `Could not bundle an external \`$ref\`. ${message}.`,
|
|
1181
|
+
help: refusedPath ? folders?.length ? "This run only reads local files inside the folders its host allows, such as the project. Move the file there or inline the schema. On your own agent, `--allow-file-refs` lifts the limit." : "This input reads no local file. Inline the schema, or serve the file from a URL." : "Check that the file or URL the `$ref` points at exists and is readable, or fix the `$ref`.",
|
|
1147
1182
|
...pointer && { location: {
|
|
1148
1183
|
kind: "schema",
|
|
1149
1184
|
pointer
|
|
@@ -1170,7 +1205,7 @@ const inlineInputName = "<inline input>";
|
|
|
1170
1205
|
* const document = await parseFromConfig({ type: 'data', data: '{"openapi":"3.0.0",...}' })
|
|
1171
1206
|
* ```
|
|
1172
1207
|
*/
|
|
1173
|
-
async function parseFromConfig(source, sources = []) {
|
|
1208
|
+
async function parseFromConfig(source, sources = [], { fileRefs } = {}) {
|
|
1174
1209
|
const raw = await (async () => {
|
|
1175
1210
|
if (source.type === "data") {
|
|
1176
1211
|
const data = typeof source.data === "string" ? parseYaml(source.data, {
|
|
@@ -1180,14 +1215,18 @@ async function parseFromConfig(source, sources = []) {
|
|
|
1180
1215
|
const isObject = Boolean(data) && typeof data === "object";
|
|
1181
1216
|
if (source.root && isObject) return bundleDocument(join(source.root, inlineInputName), {
|
|
1182
1217
|
document: data,
|
|
1183
|
-
sources
|
|
1218
|
+
sources,
|
|
1219
|
+
fileRefs
|
|
1184
1220
|
});
|
|
1185
1221
|
const scan = resolveTargets(data);
|
|
1186
1222
|
if (isObject) scans.set(data, scan);
|
|
1187
1223
|
return data;
|
|
1188
1224
|
}
|
|
1189
1225
|
await assertInputExists(source.path);
|
|
1190
|
-
return bundleDocument(source.path, {
|
|
1226
|
+
return bundleDocument(source.path, {
|
|
1227
|
+
sources,
|
|
1228
|
+
fileRefs
|
|
1229
|
+
});
|
|
1191
1230
|
})();
|
|
1192
1231
|
if (raw && typeof raw === "object") {
|
|
1193
1232
|
const document = raw;
|
|
@@ -1503,13 +1542,8 @@ function getSchemas(document, { contentType }, refs) {
|
|
|
1503
1542
|
pointer: `${ref}/content/${escapePointerToken(extracted.contentType)}/schema`
|
|
1504
1543
|
}];
|
|
1505
1544
|
}))];
|
|
1506
|
-
const normalizedNames =
|
|
1507
|
-
for (const item of candidates)
|
|
1508
|
-
const key = pascalCase(item.originalName);
|
|
1509
|
-
const bucket = normalizedNames.get(key) ?? [];
|
|
1510
|
-
bucket.push(item);
|
|
1511
|
-
normalizedNames.set(key, bucket);
|
|
1512
|
-
}
|
|
1545
|
+
const normalizedNames = memo.create();
|
|
1546
|
+
for (const item of candidates) memo(normalizedNames, pascalCase(item.originalName), () => []).push(item);
|
|
1513
1547
|
const schemas = {};
|
|
1514
1548
|
const renames = /* @__PURE__ */ new Map();
|
|
1515
1549
|
const pointers = /* @__PURE__ */ new Map();
|
|
@@ -1907,7 +1941,7 @@ let formatTally;
|
|
|
1907
1941
|
* thousands of warnings that say the same thing. `fn` must be synchronous.
|
|
1908
1942
|
*/
|
|
1909
1943
|
function groupUnsupportedFormats(fn) {
|
|
1910
|
-
formatTally =
|
|
1944
|
+
formatTally = memo.create();
|
|
1911
1945
|
try {
|
|
1912
1946
|
return fn();
|
|
1913
1947
|
} finally {
|
|
@@ -1918,7 +1952,7 @@ function groupUnsupportedFormats(fn) {
|
|
|
1918
1952
|
}
|
|
1919
1953
|
function tallyFormat(format, pointer) {
|
|
1920
1954
|
if (!formatTally) return reportUnsupportedFormat(format, [pointer]);
|
|
1921
|
-
formatTally
|
|
1955
|
+
memo(formatTally, format, () => /* @__PURE__ */ new Set()).add(pointer);
|
|
1922
1956
|
}
|
|
1923
1957
|
function reportUnsupportedFormat(format, pointers) {
|
|
1924
1958
|
const count = pointers.length;
|
|
@@ -1975,10 +2009,10 @@ function visit(node, pointer) {
|
|
|
1975
2009
|
*/
|
|
1976
2010
|
function reportDuplicateDiscriminators(node, propertyName, pointer) {
|
|
1977
2011
|
const keyword = node.strategy === "any" ? "anyOf" : "oneOf";
|
|
1978
|
-
const membersByValue =
|
|
2012
|
+
const membersByValue = memo.create();
|
|
1979
2013
|
for (const [index, member] of (node.members ?? []).entries()) {
|
|
1980
2014
|
const label = memberName(member) ?? `${keyword}/${index}`;
|
|
1981
|
-
for (const value of memberValues(member, propertyName)) membersByValue
|
|
2015
|
+
for (const value of memberValues(member, propertyName)) memo(membersByValue, value, () => []).push(label);
|
|
1982
2016
|
}
|
|
1983
2017
|
const duplicates = [...membersByValue].filter(([, members]) => members.length > 1);
|
|
1984
2018
|
if (!duplicates.length) return;
|
|
@@ -4046,7 +4080,7 @@ const adapterOas = defineExtension({
|
|
|
4046
4080
|
async settle() {
|
|
4047
4081
|
await Promise.all(pendingValidations.splice(0));
|
|
4048
4082
|
},
|
|
4049
|
-
async parse(source, { signal } = {}) {
|
|
4083
|
+
async parse(source, { signal, fileRefs } = {}) {
|
|
4050
4084
|
signal?.throwIfAborted();
|
|
4051
4085
|
const cached = inputCache.get(source);
|
|
4052
4086
|
if (cached) {
|
|
@@ -4056,7 +4090,7 @@ const adapterOas = defineExtension({
|
|
|
4056
4090
|
const read = [];
|
|
4057
4091
|
sources = read;
|
|
4058
4092
|
const promise = (async () => {
|
|
4059
|
-
const document = await parseFromConfig(source, read);
|
|
4093
|
+
const document = await parseFromConfig(source, read, { fileRefs });
|
|
4060
4094
|
signal?.throwIfAborted();
|
|
4061
4095
|
assertDocument(document);
|
|
4062
4096
|
if (validate) pendingValidations.push(validateDocumentInWorker(document));
|