@kubb/adapter-oas 5.18.7 → 5.19.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/dist/index.cjs +313 -231
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +310 -214
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,42 +1,27 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
// \0rolldown/runtime.js
|
|
3
|
-
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: ((k) => from[k]).bind(null, key),
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
20
|
-
value: mod,
|
|
21
|
-
enumerable: true
|
|
22
|
-
}) : target, mod));
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", {
|
|
5
|
+
value,
|
|
6
|
+
configurable: true
|
|
7
|
+
});
|
|
23
8
|
let _kubb_core = require("@kubb/core");
|
|
24
9
|
let _kubb_kit = require("@kubb/kit");
|
|
25
10
|
let node_path = require("node:path");
|
|
26
11
|
let node_url = require("node:url");
|
|
12
|
+
let node_fs = require("node:fs");
|
|
27
13
|
let node_fs_promises = require("node:fs/promises");
|
|
28
14
|
let _scalar_openapi_upgrader = require("@scalar/openapi-upgrader");
|
|
29
15
|
let api_ref_bundler = require("api-ref-bundler");
|
|
30
16
|
let js_yaml = require("js-yaml");
|
|
31
17
|
let yaml = require("yaml");
|
|
32
|
-
yaml = __toESM(yaml, 1);
|
|
33
18
|
// kubb-extension.json
|
|
34
19
|
var compatibility = {
|
|
35
20
|
"kubb": ">=5.8.3",
|
|
36
21
|
"node": ">=22.22"
|
|
37
22
|
};
|
|
38
23
|
// package.json
|
|
39
|
-
var version = "5.
|
|
24
|
+
var version = "5.19.0";
|
|
40
25
|
// src/constants.ts
|
|
41
26
|
/**
|
|
42
27
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -244,6 +229,47 @@ function patchDiscriminatorNode(node, entry) {
|
|
|
244
229
|
members: newMembers
|
|
245
230
|
};
|
|
246
231
|
}
|
|
232
|
+
// ../../internals/utils/src/promise.ts
|
|
233
|
+
/**
|
|
234
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
235
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
236
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
240
|
+
*/
|
|
241
|
+
function memo(store, key, compute) {
|
|
242
|
+
const cached = store.get(key);
|
|
243
|
+
if (cached !== void 0 || store.has(key)) return cached;
|
|
244
|
+
const value = compute();
|
|
245
|
+
store.set(key, value);
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
function createStore({ weak, max } = {}) {
|
|
249
|
+
if (weak) return /* @__PURE__ */ new WeakMap();
|
|
250
|
+
const entry = /* @__PURE__ */ new Map();
|
|
251
|
+
if (max === void 0) return entry;
|
|
252
|
+
const set = (key, value) => {
|
|
253
|
+
if (entry.size >= max) entry.clear();
|
|
254
|
+
entry.set(key, value);
|
|
255
|
+
};
|
|
256
|
+
if (max !== 1) return {
|
|
257
|
+
has: (key) => entry.has(key),
|
|
258
|
+
get: (key) => entry.get(key),
|
|
259
|
+
set
|
|
260
|
+
};
|
|
261
|
+
const touch = (key) => {
|
|
262
|
+
if (!entry.has(key)) return false;
|
|
263
|
+
set(key, entry.get(key));
|
|
264
|
+
return true;
|
|
265
|
+
};
|
|
266
|
+
return {
|
|
267
|
+
has: touch,
|
|
268
|
+
get: (key) => touch(key) ? entry.get(key) : void 0,
|
|
269
|
+
set
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
memo.create = createStore;
|
|
247
273
|
// ../../internals/utils/src/casing.ts
|
|
248
274
|
/**
|
|
249
275
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -258,16 +284,10 @@ function convert(text, pascal) {
|
|
|
258
284
|
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
259
285
|
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
260
286
|
}
|
|
261
|
-
const cache =
|
|
262
|
-
const cacheLimit = 1e5;
|
|
287
|
+
const cache = memo.create({ max: 1e5 });
|
|
263
288
|
function toCamelOrPascal(text, pascal) {
|
|
264
289
|
const key = `${pascal ? "P" : "c"}${text}`;
|
|
265
|
-
|
|
266
|
-
if (cached !== void 0) return cached;
|
|
267
|
-
const result = convert(text, pascal);
|
|
268
|
-
if (cache.size >= cacheLimit) cache.clear();
|
|
269
|
-
cache.set(key, result);
|
|
270
|
-
return result;
|
|
290
|
+
return memo(cache, key, () => convert(text, pascal));
|
|
271
291
|
}
|
|
272
292
|
/**
|
|
273
293
|
* Converts `text` to PascalCase.
|
|
@@ -383,7 +403,7 @@ async function exists(path) {
|
|
|
383
403
|
*/
|
|
384
404
|
async function read(path) {
|
|
385
405
|
if (runtime.isBun) return Bun.file(path).text();
|
|
386
|
-
return (0,
|
|
406
|
+
return new Promise((resolvePromise, reject) => (0, node_fs.readFile)(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
387
407
|
}
|
|
388
408
|
// src/oas.ts
|
|
389
409
|
/**
|
|
@@ -523,7 +543,7 @@ function isJsonMimeType(mimeType) {
|
|
|
523
543
|
*/
|
|
524
544
|
function createRefs(document) {
|
|
525
545
|
const pointerCache = /* @__PURE__ */ new Map();
|
|
526
|
-
const resolvedNodeCache =
|
|
546
|
+
const resolvedNodeCache = memo.create();
|
|
527
547
|
const resolvingRefs = /* @__PURE__ */ new Set();
|
|
528
548
|
function walkPointer($ref) {
|
|
529
549
|
const trimmed = $ref.trim();
|
|
@@ -573,7 +593,7 @@ function createRefs(document) {
|
|
|
573
593
|
*/
|
|
574
594
|
function resolveNode(refPath, parse) {
|
|
575
595
|
if (resolvingRefs.has(refPath)) return null;
|
|
576
|
-
|
|
596
|
+
return memo(resolvedNodeCache, refPath, () => {
|
|
577
597
|
let resolved = null;
|
|
578
598
|
try {
|
|
579
599
|
const referenced = resolve(refPath);
|
|
@@ -583,9 +603,8 @@ function createRefs(document) {
|
|
|
583
603
|
resolvingRefs.delete(refPath);
|
|
584
604
|
}
|
|
585
605
|
} catch {}
|
|
586
|
-
|
|
587
|
-
}
|
|
588
|
-
return resolvedNodeCache.get(refPath) ?? null;
|
|
606
|
+
return resolved;
|
|
607
|
+
});
|
|
589
608
|
}
|
|
590
609
|
/**
|
|
591
610
|
* Resolves a `$ref` value without mutating anything: when `value` holds a `$ref`, returns the
|
|
@@ -630,6 +649,11 @@ const NAMED_ENTRY_KEYS = /* @__PURE__ */ new Set([
|
|
|
630
649
|
"schemas",
|
|
631
650
|
"dependentSchemas"
|
|
632
651
|
]);
|
|
652
|
+
const TARGET_KEYS = [
|
|
653
|
+
"$id",
|
|
654
|
+
"$anchor",
|
|
655
|
+
"$dynamicAnchor"
|
|
656
|
+
];
|
|
633
657
|
function walk(node, pointer, visit, parentKey) {
|
|
634
658
|
if (!node || typeof node !== "object") return;
|
|
635
659
|
if (!Array.isArray(node)) visit(node, pointer);
|
|
@@ -639,6 +663,37 @@ function walk(node, pointer, visit, parentKey) {
|
|
|
639
663
|
walk(value, `${pointer}/${escapePointerToken(key)}`, visit, Array.isArray(node) ? parentKey : key);
|
|
640
664
|
}
|
|
641
665
|
}
|
|
666
|
+
/** Answers, in one walk and without building JSON pointers, what the loading steps after it need to know. */
|
|
667
|
+
function scanDocument(document) {
|
|
668
|
+
const scan = {
|
|
669
|
+
externalRef: false,
|
|
670
|
+
schemaIds: false,
|
|
671
|
+
pathRefs: false
|
|
672
|
+
};
|
|
673
|
+
const visit = ({ node, readsIds, parentKey }) => {
|
|
674
|
+
if (!node || typeof node !== "object") return;
|
|
675
|
+
const isArray = Array.isArray(node);
|
|
676
|
+
const record = node;
|
|
677
|
+
const ref = record["$ref"];
|
|
678
|
+
if (typeof ref === "string" && !ref.startsWith("#")) scan.externalRef = true;
|
|
679
|
+
if (typeof ref === "string" && ref.startsWith("#/paths/")) scan.pathRefs = true;
|
|
680
|
+
if (readsIds && !isArray && TARGET_KEYS.some((key) => typeof record[key] === "string")) scan.schemaIds = true;
|
|
681
|
+
const named = parentKey !== void 0 && NAMED_ENTRY_KEYS.has(parentKey);
|
|
682
|
+
for (const key in record) {
|
|
683
|
+
const childReadsIds = readsIds && (named || !(DATA_KEYS.has(key) || key.startsWith("x-")));
|
|
684
|
+
visit({
|
|
685
|
+
node: record[key],
|
|
686
|
+
readsIds: childReadsIds,
|
|
687
|
+
parentKey: isArray ? parentKey : key
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
visit({
|
|
692
|
+
node: document,
|
|
693
|
+
readsIds: true
|
|
694
|
+
});
|
|
695
|
+
return scan;
|
|
696
|
+
}
|
|
642
697
|
/**
|
|
643
698
|
* The declaration in the nearest enclosing schema, else the only one, else `undefined`.
|
|
644
699
|
*/
|
|
@@ -656,8 +711,8 @@ function pick(declarations, pointer) {
|
|
|
656
711
|
* enclosing anchor, so a relative `$id` base or a recursively extended schema is not followed, upgrade when a spec needs that.
|
|
657
712
|
*/
|
|
658
713
|
function resolveSchemaIds(document) {
|
|
659
|
-
const targets =
|
|
660
|
-
const declare = (key, pointer) => targets
|
|
714
|
+
const targets = memo.create();
|
|
715
|
+
const declare = (key, pointer) => memo(targets, key, () => []).push(pointer);
|
|
661
716
|
walk(document, "#", (node, pointer) => {
|
|
662
717
|
if (typeof node["$id"] === "string") declare(node["$id"], pointer);
|
|
663
718
|
for (const key of ["$anchor", "$dynamicAnchor"]) if (typeof node[key] === "string") declare(`#${node[key]}`, pointer);
|
|
@@ -673,6 +728,183 @@ function resolveSchemaIds(document) {
|
|
|
673
728
|
}
|
|
674
729
|
});
|
|
675
730
|
}
|
|
731
|
+
// src/sourceLocator.ts
|
|
732
|
+
const flowGap = /(?:[\s,]|#[^\n]*)*/y;
|
|
733
|
+
const indexes = memo.create({ max: 1 });
|
|
734
|
+
function toKeyText(text) {
|
|
735
|
+
if (!/^[-+.~\d]|^(?:null|true|false)$/i.test(text)) return text;
|
|
736
|
+
try {
|
|
737
|
+
const value = (0, js_yaml.load)(text, { schema: js_yaml.CORE_SCHEMA });
|
|
738
|
+
return value === null || typeof value === "number" || typeof value === "boolean" ? String(value) : text;
|
|
739
|
+
} catch {
|
|
740
|
+
return text;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
function indexEvents(events) {
|
|
744
|
+
const types = new Uint8Array(events.length);
|
|
745
|
+
const styles = new Uint8Array(events.length);
|
|
746
|
+
const starts = new Int32Array(events.length);
|
|
747
|
+
const ends = new Int32Array(events.length);
|
|
748
|
+
for (let at = 0, last = 0; at < events.length; last = Math.max(last, starts[at], ends[at]), at++) {
|
|
749
|
+
const event = events[at];
|
|
750
|
+
types[at] = event.type;
|
|
751
|
+
if (event.type === js_yaml.EVENT_ID.SCALAR) {
|
|
752
|
+
const quote = event.style === js_yaml.SCALAR_STYLE.SINGLE_QUOTED || event.style === js_yaml.SCALAR_STYLE.DOUBLE_QUOTED ? 1 : 0;
|
|
753
|
+
const empty = event.valueStart < 0;
|
|
754
|
+
styles[at] = event.style;
|
|
755
|
+
starts[at] = empty ? last : event.valueStart - quote;
|
|
756
|
+
ends[at] = empty ? last : event.valueEnd + quote;
|
|
757
|
+
}
|
|
758
|
+
if (event.type === js_yaml.EVENT_ID.ALIAS) {
|
|
759
|
+
starts[at] = event.anchorStart - 1;
|
|
760
|
+
ends[at] = event.anchorEnd;
|
|
761
|
+
}
|
|
762
|
+
if (event.type === js_yaml.EVENT_ID.MAPPING || event.type === js_yaml.EVENT_ID.SEQUENCE) {
|
|
763
|
+
styles[at] = event.style;
|
|
764
|
+
starts[at] = event.start;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return {
|
|
768
|
+
types,
|
|
769
|
+
styles,
|
|
770
|
+
starts,
|
|
771
|
+
ends,
|
|
772
|
+
collections: /* @__PURE__ */ new Map()
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
function createLocator({ index, source }) {
|
|
776
|
+
const { types, styles, starts, ends, collections } = index;
|
|
777
|
+
const isCollection = (at) => types[at] === js_yaml.EVENT_ID.MAPPING || types[at] === js_yaml.EVENT_ID.SEQUENCE;
|
|
778
|
+
const readText = (at) => {
|
|
779
|
+
const text = source.slice(starts[at], ends[at]);
|
|
780
|
+
if (styles[at] === js_yaml.SCALAR_STYLE.SINGLE_QUOTED) return text.slice(1, -1).replaceAll("''", "'");
|
|
781
|
+
if (styles[at] !== js_yaml.SCALAR_STYLE.DOUBLE_QUOTED) return text;
|
|
782
|
+
try {
|
|
783
|
+
return JSON.parse(text);
|
|
784
|
+
} catch {
|
|
785
|
+
return String((0, js_yaml.load)(text, { schema: js_yaml.CORE_SCHEMA }));
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
const readNode = (at) => {
|
|
789
|
+
if (!isCollection(at)) return {
|
|
790
|
+
start: starts[at],
|
|
791
|
+
end: ends[at],
|
|
792
|
+
next: at + 1
|
|
793
|
+
};
|
|
794
|
+
let next = at + 1;
|
|
795
|
+
let end = starts[at];
|
|
796
|
+
while (types[next] !== js_yaml.EVENT_ID.POP) ({end, next} = readNode(next));
|
|
797
|
+
if (styles[at] === js_yaml.COLLECTION_STYLE.FLOW) {
|
|
798
|
+
flowGap.lastIndex = end;
|
|
799
|
+
flowGap.exec(source);
|
|
800
|
+
end = flowGap.lastIndex + 1;
|
|
801
|
+
}
|
|
802
|
+
return {
|
|
803
|
+
start: starts[at],
|
|
804
|
+
end,
|
|
805
|
+
next: next + 1
|
|
806
|
+
};
|
|
807
|
+
};
|
|
808
|
+
const readChildren = (collection) => {
|
|
809
|
+
const keys = /* @__PURE__ */ new Map();
|
|
810
|
+
const items = [];
|
|
811
|
+
const isMapping = types[collection] === js_yaml.EVENT_ID.MAPPING;
|
|
812
|
+
for (let at = collection + 1; types[at] !== js_yaml.EVENT_ID.POP;) {
|
|
813
|
+
const key = isMapping ? at : void 0;
|
|
814
|
+
if (key !== void 0) at = readNode(at).next;
|
|
815
|
+
const { start, end, next } = readNode(at);
|
|
816
|
+
if (key === void 0) items.push({
|
|
817
|
+
index: at,
|
|
818
|
+
range: {
|
|
819
|
+
start,
|
|
820
|
+
end
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
const raw = key !== void 0 && types[key] === js_yaml.EVENT_ID.SCALAR ? readText(key) : void 0;
|
|
824
|
+
const text = raw !== void 0 && styles[key] === js_yaml.SCALAR_STYLE.PLAIN ? toKeyText(raw) : raw;
|
|
825
|
+
if (text !== void 0 && !keys.has(text)) keys.set(text, {
|
|
826
|
+
index: at,
|
|
827
|
+
range: {
|
|
828
|
+
start: starts[key],
|
|
829
|
+
end: ends[key]
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
at = next;
|
|
833
|
+
}
|
|
834
|
+
return {
|
|
835
|
+
keys,
|
|
836
|
+
items
|
|
837
|
+
};
|
|
838
|
+
};
|
|
839
|
+
const walk = ({ at, tokens: [token, ...rest], range }) => {
|
|
840
|
+
if (token === void 0) return range;
|
|
841
|
+
if (!isCollection(at)) return void 0;
|
|
842
|
+
const { keys, items } = memo(collections, at, () => readChildren(at));
|
|
843
|
+
const child = keys.get(token) ?? items[Number(token)];
|
|
844
|
+
return child ? walk({
|
|
845
|
+
at: child.index,
|
|
846
|
+
tokens: rest,
|
|
847
|
+
range: child.range
|
|
848
|
+
}) : void 0;
|
|
849
|
+
};
|
|
850
|
+
return {
|
|
851
|
+
find(pointer) {
|
|
852
|
+
return walk({
|
|
853
|
+
at: 1,
|
|
854
|
+
tokens: pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken),
|
|
855
|
+
range: void 0
|
|
856
|
+
});
|
|
857
|
+
},
|
|
858
|
+
findRef(ref) {
|
|
859
|
+
if (!index.refs) {
|
|
860
|
+
index.refs = /* @__PURE__ */ new Map();
|
|
861
|
+
const frames = [];
|
|
862
|
+
for (let at = 1; at + 1 < types.length; at++) {
|
|
863
|
+
if (types[at] === js_yaml.EVENT_ID.POP) {
|
|
864
|
+
frames.pop();
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
const top = frames.length - 1;
|
|
868
|
+
const isKey = frames[top] === 0;
|
|
869
|
+
if (frames[top] >= 0) frames[top] = 1 - frames[top];
|
|
870
|
+
if (isCollection(at)) frames.push(types[at] === js_yaml.EVENT_ID.MAPPING ? 0 : -1);
|
|
871
|
+
if (!isKey || types[at] !== js_yaml.EVENT_ID.SCALAR || types[at + 1] !== js_yaml.EVENT_ID.SCALAR || readText(at) !== "$ref") continue;
|
|
872
|
+
const text = readText(at + 1);
|
|
873
|
+
if (!index.refs.has(text)) index.refs.set(text, {
|
|
874
|
+
start: starts[at + 1],
|
|
875
|
+
end: ends[at + 1]
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
return index.refs.get(ref);
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
function parse$1(source) {
|
|
884
|
+
try {
|
|
885
|
+
return indexEvents((0, js_yaml.parseEvents)(source, { maxDepth: Number.POSITIVE_INFINITY }));
|
|
886
|
+
} catch {
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
__name(parse$1, "parse");
|
|
891
|
+
function rememberEvents({ source, events }) {
|
|
892
|
+
indexes.set(source, indexEvents(events));
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
|
|
896
|
+
*/
|
|
897
|
+
const locate = (source, location) => {
|
|
898
|
+
if (!("pointer" in location)) return void 0;
|
|
899
|
+
const index = memo(indexes, source, () => parse$1(source));
|
|
900
|
+
if (!index) return void 0;
|
|
901
|
+
const { find, findRef } = createLocator({
|
|
902
|
+
index,
|
|
903
|
+
source
|
|
904
|
+
});
|
|
905
|
+
const ref = "ref" in location ? location.ref : void 0;
|
|
906
|
+
return find(location.pointer) ?? (ref ? findRef(ref) : void 0);
|
|
907
|
+
};
|
|
676
908
|
// src/load/source.ts
|
|
677
909
|
const urlRegExp = /^https?:\/+/i;
|
|
678
910
|
/**
|
|
@@ -727,14 +959,15 @@ async function readSource(sourcePath) {
|
|
|
727
959
|
* Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
|
|
728
960
|
* object, Markdown is returned as-is (bundled inline rather than dereferenced).
|
|
729
961
|
*/
|
|
730
|
-
async function resolveSource(sourcePath) {
|
|
962
|
+
async function resolveSource(sourcePath, { remember } = {}) {
|
|
731
963
|
const data = await readSource(sourcePath);
|
|
732
964
|
if (sourcePath.toLowerCase().endsWith(".md")) return data;
|
|
733
965
|
const pathname = urlRegExp.test(sourcePath) ? new URL(sourcePath).pathname : sourcePath;
|
|
734
966
|
return parseYaml(data, {
|
|
735
967
|
origin: sourcePath,
|
|
736
968
|
json: !/\.ya?ml$/i.test(pathname),
|
|
737
|
-
help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
|
|
969
|
+
help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page.",
|
|
970
|
+
remember
|
|
738
971
|
});
|
|
739
972
|
}
|
|
740
973
|
/**
|
|
@@ -750,13 +983,23 @@ async function resolveSource(sourcePath) {
|
|
|
750
983
|
* keeping the last one. Its core schema keeps timestamps as strings and `<<` as a plain key, as
|
|
751
984
|
* `yaml` does for YAML 1.2.
|
|
752
985
|
*/
|
|
753
|
-
function parseYaml(data, { origin, help, json = true }) {
|
|
986
|
+
function parseYaml(data, { origin, help, json = true, remember = false }) {
|
|
754
987
|
if (json && /^\s*[[{]/.test(data)) try {
|
|
755
988
|
return JSON.parse(data);
|
|
756
989
|
} catch {}
|
|
757
990
|
try {
|
|
758
|
-
const
|
|
759
|
-
|
|
991
|
+
const events = (0, js_yaml.parseEvents)(data, {});
|
|
992
|
+
const documents = (0, js_yaml.constructFromEvents)(events, {
|
|
993
|
+
source: data,
|
|
994
|
+
schema: js_yaml.CORE_SCHEMA
|
|
995
|
+
});
|
|
996
|
+
if (documents.length === 1 && documents[0] !== void 0) {
|
|
997
|
+
if (remember) rememberEvents({
|
|
998
|
+
source: data,
|
|
999
|
+
events
|
|
1000
|
+
});
|
|
1001
|
+
return documents[0];
|
|
1002
|
+
}
|
|
760
1003
|
} catch {}
|
|
761
1004
|
try {
|
|
762
1005
|
return (0, yaml.parse)(data);
|
|
@@ -788,18 +1031,15 @@ async function assertInputExists(input) {
|
|
|
788
1031
|
});
|
|
789
1032
|
}
|
|
790
1033
|
// src/load/normalize.ts
|
|
1034
|
+
const scans = /* @__PURE__ */ new WeakMap();
|
|
791
1035
|
/**
|
|
792
|
-
*
|
|
793
|
-
* absolute path, or URL). An internal `#/...` fragment does not count.
|
|
794
|
-
*
|
|
795
|
-
* `Object.values` reads array elements and object property values alike, so the same recursion
|
|
796
|
-
* walks both without a separate array branch.
|
|
1036
|
+
* Rewrites `$id` and `$anchor` refs in place, then returns the scan of the result.
|
|
797
1037
|
*/
|
|
798
|
-
function
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
return
|
|
1038
|
+
function resolveTargets(document) {
|
|
1039
|
+
const scan = scanDocument(document);
|
|
1040
|
+
if (!scan.schemaIds) return scan;
|
|
1041
|
+
resolveSchemaIds(document);
|
|
1042
|
+
return scanDocument(document);
|
|
803
1043
|
}
|
|
804
1044
|
/**
|
|
805
1045
|
* Replaces each `$ref` into `#/paths/...` with a copy of its target, in place.
|
|
@@ -881,23 +1121,24 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
881
1121
|
* `const document = await bundleDocument('https://example.com/openapi.yaml')`
|
|
882
1122
|
*/
|
|
883
1123
|
async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
884
|
-
const cache =
|
|
1124
|
+
const cache = memo.create();
|
|
885
1125
|
const toKey = (sourcePath) => urlRegExp.test(sourcePath) ? new URL(sourcePath).href : sourcePath;
|
|
886
1126
|
let entry = pathOrUrl;
|
|
887
1127
|
let entryDocument = document;
|
|
888
1128
|
const resolver = (sourcePath) => {
|
|
889
1129
|
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return Promise.resolve(entryDocument);
|
|
890
1130
|
const key = toKey(sourcePath);
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
cache.set(key, result);
|
|
896
|
-
return result;
|
|
1131
|
+
return memo(cache, key, () => {
|
|
1132
|
+
sources.push(key);
|
|
1133
|
+
return resolveSource(sourcePath, { remember: key === toKey(pathOrUrl) });
|
|
1134
|
+
});
|
|
897
1135
|
};
|
|
898
1136
|
const root = await resolver(pathOrUrl);
|
|
899
|
-
|
|
900
|
-
if (typeof root === "object" && root !== null && !
|
|
1137
|
+
const scan = resolveTargets(root);
|
|
1138
|
+
if (typeof root === "object" && root !== null && !scan.externalRef) {
|
|
1139
|
+
scans.set(root, scan);
|
|
1140
|
+
return root;
|
|
1141
|
+
}
|
|
901
1142
|
entry = getBaseUri(pathOrUrl, root);
|
|
902
1143
|
entryDocument = root;
|
|
903
1144
|
return await (0, api_ref_bundler.bundle)(entry, resolver, { hooks: { onError: (message, { path, state }) => {
|
|
@@ -940,11 +1181,13 @@ async function parseFromConfig(source, sources = []) {
|
|
|
940
1181
|
origin: "the inline `input` string",
|
|
941
1182
|
help: "Fix the syntax at the reported line. Kubb reads `input` as an inline spec, not a file path, because it starts with `{`, `[`, `openapi:`, or `swagger:`, or spans several lines."
|
|
942
1183
|
}) : structuredClone(source.data);
|
|
943
|
-
|
|
1184
|
+
const isObject = Boolean(data) && typeof data === "object";
|
|
1185
|
+
if (source.root && isObject) return bundleDocument((0, node_path.join)(source.root, inlineInputName), {
|
|
944
1186
|
document: data,
|
|
945
1187
|
sources
|
|
946
1188
|
});
|
|
947
|
-
|
|
1189
|
+
const scan = resolveTargets(data);
|
|
1190
|
+
if (isObject) scans.set(data, scan);
|
|
948
1191
|
return data;
|
|
949
1192
|
}
|
|
950
1193
|
await assertInputExists(source.path);
|
|
@@ -957,7 +1200,7 @@ async function parseFromConfig(source, sources = []) {
|
|
|
957
1200
|
if (typeof version === "number") document[key] = Number.isInteger(version) ? version.toFixed(1) : String(version);
|
|
958
1201
|
}
|
|
959
1202
|
}
|
|
960
|
-
if (raw && typeof raw === "object" && "swagger" in raw) {
|
|
1203
|
+
if (raw && typeof raw === "object" && "swagger" in raw && (scans.get(raw)?.pathRefs ?? true)) {
|
|
961
1204
|
const refs = createRefs(raw);
|
|
962
1205
|
inlinePathRefs(raw, (ref) => refs.resolve(ref, { report: false }));
|
|
963
1206
|
}
|
|
@@ -1264,13 +1507,8 @@ function getSchemas(document, { contentType }, refs) {
|
|
|
1264
1507
|
pointer: `${ref}/content/${escapePointerToken(extracted.contentType)}/schema`
|
|
1265
1508
|
}];
|
|
1266
1509
|
}))];
|
|
1267
|
-
const normalizedNames =
|
|
1268
|
-
for (const item of candidates)
|
|
1269
|
-
const key = pascalCase(item.originalName);
|
|
1270
|
-
const bucket = normalizedNames.get(key) ?? [];
|
|
1271
|
-
bucket.push(item);
|
|
1272
|
-
normalizedNames.set(key, bucket);
|
|
1273
|
-
}
|
|
1510
|
+
const normalizedNames = memo.create();
|
|
1511
|
+
for (const item of candidates) memo(normalizedNames, pascalCase(item.originalName), () => []).push(item);
|
|
1274
1512
|
const schemas = {};
|
|
1275
1513
|
const renames = /* @__PURE__ */ new Map();
|
|
1276
1514
|
const pointers = /* @__PURE__ */ new Map();
|
|
@@ -1668,7 +1906,7 @@ let formatTally;
|
|
|
1668
1906
|
* thousands of warnings that say the same thing. `fn` must be synchronous.
|
|
1669
1907
|
*/
|
|
1670
1908
|
function groupUnsupportedFormats(fn) {
|
|
1671
|
-
formatTally =
|
|
1909
|
+
formatTally = memo.create();
|
|
1672
1910
|
try {
|
|
1673
1911
|
return fn();
|
|
1674
1912
|
} finally {
|
|
@@ -1679,7 +1917,7 @@ function groupUnsupportedFormats(fn) {
|
|
|
1679
1917
|
}
|
|
1680
1918
|
function tallyFormat(format, pointer) {
|
|
1681
1919
|
if (!formatTally) return reportUnsupportedFormat(format, [pointer]);
|
|
1682
|
-
formatTally
|
|
1920
|
+
memo(formatTally, format, () => /* @__PURE__ */ new Set()).add(pointer);
|
|
1683
1921
|
}
|
|
1684
1922
|
function reportUnsupportedFormat(format, pointers) {
|
|
1685
1923
|
const count = pointers.length;
|
|
@@ -1736,10 +1974,10 @@ function visit(node, pointer) {
|
|
|
1736
1974
|
*/
|
|
1737
1975
|
function reportDuplicateDiscriminators(node, propertyName, pointer) {
|
|
1738
1976
|
const keyword = node.strategy === "any" ? "anyOf" : "oneOf";
|
|
1739
|
-
const membersByValue =
|
|
1977
|
+
const membersByValue = memo.create();
|
|
1740
1978
|
for (const [index, member] of (node.members ?? []).entries()) {
|
|
1741
1979
|
const label = memberName(member) ?? `${keyword}/${index}`;
|
|
1742
|
-
for (const value of memberValues(member, propertyName)) membersByValue
|
|
1980
|
+
for (const value of memberValues(member, propertyName)) memo(membersByValue, value, () => []).push(label);
|
|
1743
1981
|
}
|
|
1744
1982
|
const duplicates = [...membersByValue].filter(([, members]) => members.length > 1);
|
|
1745
1983
|
if (!duplicates.length) return;
|
|
@@ -3624,162 +3862,6 @@ function refPromotedEnums(node, promoted) {
|
|
|
3624
3862
|
});
|
|
3625
3863
|
} });
|
|
3626
3864
|
}
|
|
3627
|
-
// src/sourceLocator.ts
|
|
3628
|
-
let cached;
|
|
3629
|
-
const toRange = ([start, end]) => ({
|
|
3630
|
-
start,
|
|
3631
|
-
end
|
|
3632
|
-
});
|
|
3633
|
-
/** Walks a JSON pointer through maps and sequences; the range is the last key or item it reached. */
|
|
3634
|
-
function findPointer(document, pointer) {
|
|
3635
|
-
return pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken).reduce((at, token) => {
|
|
3636
|
-
if (yaml.isMap(at?.node)) {
|
|
3637
|
-
const pair = at.node.items.find((item) => yaml.isScalar(item.key) && String(item.key.value) === token);
|
|
3638
|
-
return pair && yaml.isScalar(pair.key) && pair.key.range ? {
|
|
3639
|
-
node: pair.value,
|
|
3640
|
-
range: toRange(pair.key.range)
|
|
3641
|
-
} : void 0;
|
|
3642
|
-
}
|
|
3643
|
-
if (yaml.isSeq(at?.node)) {
|
|
3644
|
-
const item = at.node.items[Number(token)];
|
|
3645
|
-
return yaml.isNode(item) && item.range ? {
|
|
3646
|
-
node: item,
|
|
3647
|
-
range: toRange(item.range)
|
|
3648
|
-
} : void 0;
|
|
3649
|
-
}
|
|
3650
|
-
}, {
|
|
3651
|
-
node: document.contents,
|
|
3652
|
-
range: void 0
|
|
3653
|
-
})?.range;
|
|
3654
|
-
}
|
|
3655
|
-
/** The first `$ref` whose value is `ref`. */
|
|
3656
|
-
function findRefUsage(document, ref) {
|
|
3657
|
-
const found = [];
|
|
3658
|
-
yaml.visit(document, { Pair(_, { key, value }) {
|
|
3659
|
-
if (!yaml.isScalar(key) || key.value !== "$ref" || !yaml.isScalar(value) || value.value !== ref || !value.range) return;
|
|
3660
|
-
found.push(toRange(value.range));
|
|
3661
|
-
return yaml.visit.BREAK;
|
|
3662
|
-
} });
|
|
3663
|
-
return found[0];
|
|
3664
|
-
}
|
|
3665
|
-
/**
|
|
3666
|
-
* One pass over JSON text, so a located diagnostic in a large JSON spec skips `yaml.parseDocument`.
|
|
3667
|
-
* Holds the range of every key and array item by pointer, and the first usage of every `$ref`, as
|
|
3668
|
-
* `findPointer` and `findRefUsage` return them. Throws where the text breaks JSON syntax, such as a
|
|
3669
|
-
* comment, an unquoted key, or an unquoted `$ref` value. Other bare values, such as `object`, pass.
|
|
3670
|
-
*/
|
|
3671
|
-
function indexJson(source) {
|
|
3672
|
-
const pointers = /* @__PURE__ */ new Map();
|
|
3673
|
-
const refs = /* @__PURE__ */ new Map();
|
|
3674
|
-
let at = source.indexOf("{");
|
|
3675
|
-
const fail = () => {
|
|
3676
|
-
throw new SyntaxError(`Unexpected character at ${at}`);
|
|
3677
|
-
};
|
|
3678
|
-
const skipSpace = () => {
|
|
3679
|
-
for (let code = source.charCodeAt(at); code === 32 || code === 9 || code === 10 || code === 13; code = source.charCodeAt(++at));
|
|
3680
|
-
};
|
|
3681
|
-
const readString = () => {
|
|
3682
|
-
if (source[at] !== "\"") fail();
|
|
3683
|
-
const start = at;
|
|
3684
|
-
for (at++; source.charCodeAt(at) !== 34; at++) {
|
|
3685
|
-
if (at >= source.length) fail();
|
|
3686
|
-
if (source.charCodeAt(at) === 92) at++;
|
|
3687
|
-
}
|
|
3688
|
-
const raw = source.slice(start + 1, at++);
|
|
3689
|
-
return {
|
|
3690
|
-
text: raw.includes("\\") ? JSON.parse(source.slice(start, at)) : raw,
|
|
3691
|
-
start,
|
|
3692
|
-
end: at
|
|
3693
|
-
};
|
|
3694
|
-
};
|
|
3695
|
-
const expect = (char) => {
|
|
3696
|
-
skipSpace();
|
|
3697
|
-
if (source[at++] !== char) fail();
|
|
3698
|
-
skipSpace();
|
|
3699
|
-
};
|
|
3700
|
-
const walk = (pointer) => {
|
|
3701
|
-
const open = source[at];
|
|
3702
|
-
if (open === "\"") {
|
|
3703
|
-
readString();
|
|
3704
|
-
return;
|
|
3705
|
-
}
|
|
3706
|
-
if (open !== "{" && open !== "[") {
|
|
3707
|
-
const start = at;
|
|
3708
|
-
while (at < source.length && !/[\s,\]}]/.test(source[at])) at++;
|
|
3709
|
-
if (at === start) fail();
|
|
3710
|
-
return;
|
|
3711
|
-
}
|
|
3712
|
-
const close = open === "{" ? "}" : "]";
|
|
3713
|
-
at++;
|
|
3714
|
-
for (let index = 0;; index++) {
|
|
3715
|
-
skipSpace();
|
|
3716
|
-
if (source[at] === close) break;
|
|
3717
|
-
if (index > 0) expect(",");
|
|
3718
|
-
if (open === "[") {
|
|
3719
|
-
const start = at;
|
|
3720
|
-
const next = `${pointer}/${index}`;
|
|
3721
|
-
walk(next);
|
|
3722
|
-
if (!pointers.has(next)) pointers.set(next, {
|
|
3723
|
-
start,
|
|
3724
|
-
end: at
|
|
3725
|
-
});
|
|
3726
|
-
continue;
|
|
3727
|
-
}
|
|
3728
|
-
const key = readString();
|
|
3729
|
-
const next = `${pointer}/${escapePointerToken(key.text)}`;
|
|
3730
|
-
if (!pointers.has(next)) pointers.set(next, {
|
|
3731
|
-
start: key.start,
|
|
3732
|
-
end: key.end
|
|
3733
|
-
});
|
|
3734
|
-
expect(":");
|
|
3735
|
-
if (key.text !== "$ref" || source[at] === "{" || source[at] === "[") {
|
|
3736
|
-
walk(next);
|
|
3737
|
-
continue;
|
|
3738
|
-
}
|
|
3739
|
-
const value = readString();
|
|
3740
|
-
if (!refs.has(value.text)) refs.set(value.text, {
|
|
3741
|
-
start: value.start,
|
|
3742
|
-
end: value.end
|
|
3743
|
-
});
|
|
3744
|
-
}
|
|
3745
|
-
at++;
|
|
3746
|
-
};
|
|
3747
|
-
walk("#");
|
|
3748
|
-
return {
|
|
3749
|
-
pointers,
|
|
3750
|
-
refs
|
|
3751
|
-
};
|
|
3752
|
-
}
|
|
3753
|
-
/** Indexes plain JSON text; anything else goes to the YAML parser. */
|
|
3754
|
-
function parse(source) {
|
|
3755
|
-
if (/^[ \t\n\r]*\{/.test(source)) try {
|
|
3756
|
-
return {
|
|
3757
|
-
source,
|
|
3758
|
-
json: indexJson(source)
|
|
3759
|
-
};
|
|
3760
|
-
} catch {}
|
|
3761
|
-
return {
|
|
3762
|
-
source,
|
|
3763
|
-
document: yaml.parseDocument(source)
|
|
3764
|
-
};
|
|
3765
|
-
}
|
|
3766
|
-
/**
|
|
3767
|
-
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
|
|
3768
|
-
*/
|
|
3769
|
-
const locate = (source, location) => {
|
|
3770
|
-
if (!("pointer" in location)) return void 0;
|
|
3771
|
-
cached = cached?.source === source ? {
|
|
3772
|
-
...cached,
|
|
3773
|
-
source
|
|
3774
|
-
} : parse(source);
|
|
3775
|
-
const ref = "ref" in location ? location.ref : void 0;
|
|
3776
|
-
if ("json" in cached) {
|
|
3777
|
-
const { pointers, refs } = cached.json;
|
|
3778
|
-
return pointers.get(location.pointer) ?? (ref ? refs.get(ref) : void 0);
|
|
3779
|
-
}
|
|
3780
|
-
const { document } = cached;
|
|
3781
|
-
return findPointer(document, location.pointer) ?? (ref ? findRefUsage(document, ref) : void 0);
|
|
3782
|
-
};
|
|
3783
3865
|
// src/adapter.ts
|
|
3784
3866
|
/**
|
|
3785
3867
|
* The `name` of `@kubb/adapter-oas`, used to identify this adapter in a Kubb config.
|