@kubb/adapter-oas 5.0.0-beta.52 → 5.0.0-beta.54
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 +72 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +72 -13
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
- package/src/bundler.ts +71 -0
- package/src/factory.ts +6 -5
- package/src/stream.ts +13 -3
- package/extension.yaml +0 -431
package/dist/index.cjs
CHANGED
|
@@ -29,6 +29,9 @@ node_path = __toESM(node_path, 1);
|
|
|
29
29
|
let node_fs_promises = require("node:fs/promises");
|
|
30
30
|
let oas_normalize = require("oas-normalize");
|
|
31
31
|
oas_normalize = __toESM(oas_normalize, 1);
|
|
32
|
+
let api_ref_bundler = require("api-ref-bundler");
|
|
33
|
+
let js_yaml = require("js-yaml");
|
|
34
|
+
js_yaml = __toESM(js_yaml, 1);
|
|
32
35
|
let _kubb_ast_utils = require("@kubb/ast/utils");
|
|
33
36
|
let oas_types = require("oas/types");
|
|
34
37
|
let oas_utils = require("oas/utils");
|
|
@@ -263,6 +266,19 @@ async function exists(path) {
|
|
|
263
266
|
if (isBun()) return Bun.file(path).exists();
|
|
264
267
|
return (0, node_fs_promises.access)(path).then(() => true, () => false);
|
|
265
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Reads the file at `path` as a UTF-8 string.
|
|
271
|
+
* Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* const source = await read('./src/Pet.ts')
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
async function read(path) {
|
|
279
|
+
if (isBun()) return Bun.file(path).text();
|
|
280
|
+
return (0, node_fs_promises.readFile)(path, { encoding: "utf8" });
|
|
281
|
+
}
|
|
266
282
|
//#endregion
|
|
267
283
|
//#region ../../internals/utils/src/object.ts
|
|
268
284
|
/**
|
|
@@ -564,6 +580,50 @@ var URLPath = class {
|
|
|
564
580
|
}
|
|
565
581
|
};
|
|
566
582
|
//#endregion
|
|
583
|
+
//#region src/bundler.ts
|
|
584
|
+
const urlRegExp = /^https?:\/+/i;
|
|
585
|
+
async function readSource(sourcePath) {
|
|
586
|
+
if (urlRegExp.test(sourcePath)) {
|
|
587
|
+
const url = new URL(sourcePath);
|
|
588
|
+
const response = await fetch(url);
|
|
589
|
+
if (!response.ok) throw new Error(`Cannot fetch the OAS document at ${url.href} (HTTP ${response.status})`);
|
|
590
|
+
return response.text();
|
|
591
|
+
}
|
|
592
|
+
return read(sourcePath);
|
|
593
|
+
}
|
|
594
|
+
async function resolveSource(sourcePath) {
|
|
595
|
+
const data = await readSource(sourcePath);
|
|
596
|
+
if (sourcePath.toLowerCase().endsWith(".md")) return data;
|
|
597
|
+
return js_yaml.default.load(data);
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Bundles a multi-file OpenAPI document into a single document via `api-ref-bundler`.
|
|
601
|
+
*
|
|
602
|
+
* External file schemas are hoisted into named `components.schemas` entries, so a property
|
|
603
|
+
* pointing at `./schemas/User.yaml` ends up referencing `#/components/schemas/User`. Generators
|
|
604
|
+
* can then emit a named type with an import instead of inlining the shape. Sources are read with
|
|
605
|
+
* the Bun-aware `read` util for local YAML and JSON files, and with `fetch` for HTTP(S) URLs.
|
|
606
|
+
*
|
|
607
|
+
* @example Local file
|
|
608
|
+
* `const document = await bundleDocument('./openapi.yaml')`
|
|
609
|
+
*
|
|
610
|
+
* @example Remote URL
|
|
611
|
+
* `const document = await bundleDocument('https://example.com/openapi.yaml')`
|
|
612
|
+
*/
|
|
613
|
+
async function bundleDocument(pathOrUrl) {
|
|
614
|
+
const cache = /* @__PURE__ */ new Map();
|
|
615
|
+
const resolver = (sourcePath) => {
|
|
616
|
+
const key = urlRegExp.test(sourcePath) ? new URL(sourcePath).href : sourcePath;
|
|
617
|
+
const cached = cache.get(key);
|
|
618
|
+
if (cached) return cached;
|
|
619
|
+
const result = resolveSource(sourcePath);
|
|
620
|
+
cache.set(key, result);
|
|
621
|
+
return result;
|
|
622
|
+
};
|
|
623
|
+
await resolver(pathOrUrl);
|
|
624
|
+
return await (0, api_ref_bundler.bundle)(pathOrUrl, resolver);
|
|
625
|
+
}
|
|
626
|
+
//#endregion
|
|
567
627
|
//#region src/guards.ts
|
|
568
628
|
/**
|
|
569
629
|
* Returns `true` when `doc` is a Swagger 2.0 document (no `openapi` key).
|
|
@@ -628,8 +688,9 @@ function isDiscriminator(obj) {
|
|
|
628
688
|
/**
|
|
629
689
|
* Loads and dereferences an OpenAPI document, returning the raw `Document`.
|
|
630
690
|
*
|
|
631
|
-
* Accepts a file path string or an already-parsed document object. File paths
|
|
632
|
-
*
|
|
691
|
+
* Accepts a file path string or an already-parsed document object. File paths and URLs are
|
|
692
|
+
* bundled via `api-ref-bundler`, hoisting external file schemas into named `components.schemas`
|
|
693
|
+
* entries so generators can emit named types and imports. Swagger 2.0 documents are
|
|
633
694
|
* automatically up-converted to OpenAPI 3.0 via `swagger2openapi`.
|
|
634
695
|
*
|
|
635
696
|
* @example
|
|
@@ -639,13 +700,10 @@ function isDiscriminator(obj) {
|
|
|
639
700
|
* ```
|
|
640
701
|
*/
|
|
641
702
|
async function parseDocument(pathOrApi, { canBundle = true, enablePaths = true } = {}) {
|
|
642
|
-
if (typeof pathOrApi === "string" && canBundle) {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
enablePaths
|
|
647
|
-
});
|
|
648
|
-
}
|
|
703
|
+
if (typeof pathOrApi === "string" && canBundle) return parseDocument(await bundleDocument(pathOrApi), {
|
|
704
|
+
canBundle: false,
|
|
705
|
+
enablePaths
|
|
706
|
+
});
|
|
649
707
|
const document = await new oas_normalize.default(pathOrApi, {
|
|
650
708
|
enablePaths,
|
|
651
709
|
colorizeErrors: true
|
|
@@ -2351,9 +2409,10 @@ function preScan({ schemas, parseSchema, parseOperation, baseOas, parserOptions,
|
|
|
2351
2409
|
});
|
|
2352
2410
|
for (const definition of dedupePlan.hoisted) if (definition.type === "enum" && definition.name) enumNames.push(definition.name);
|
|
2353
2411
|
}
|
|
2412
|
+
const aliasNames = dedupePlan?.aliasNames;
|
|
2354
2413
|
return {
|
|
2355
2414
|
refAliasMap,
|
|
2356
|
-
enumNames,
|
|
2415
|
+
enumNames: aliasNames && aliasNames.size > 0 ? enumNames.filter((name) => !aliasNames.has(name)) : enumNames,
|
|
2357
2416
|
circularNames,
|
|
2358
2417
|
discriminatorChildMap,
|
|
2359
2418
|
dedupePlan
|
|
@@ -2388,12 +2447,13 @@ function createInputStream({ schemas, parseSchema, parseOperation, baseOas, pars
|
|
|
2388
2447
|
description: node.description,
|
|
2389
2448
|
deprecated: node.deprecated
|
|
2390
2449
|
});
|
|
2391
|
-
return _kubb_core.ast.applyDedupe(node, dedupePlan
|
|
2450
|
+
return _kubb_core.ast.applyDedupe(node, dedupePlan, true);
|
|
2392
2451
|
};
|
|
2393
2452
|
const schemasIterable = { [Symbol.asyncIterator]() {
|
|
2394
2453
|
return (async function* () {
|
|
2395
2454
|
if (dedupePlan) for (const definition of dedupePlan.hoisted) yield definition;
|
|
2396
2455
|
for (const [name, schema] of Object.entries(schemas)) {
|
|
2456
|
+
if (dedupePlan?.aliasNames.has(name)) continue;
|
|
2397
2457
|
const alias = refAliasMap.get(name);
|
|
2398
2458
|
if (alias?.name && schemas[alias.name]) {
|
|
2399
2459
|
yield rewriteTopLevelSchema({
|
|
@@ -2418,7 +2478,7 @@ function createInputStream({ schemas, parseSchema, parseOperation, baseOas, pars
|
|
|
2418
2478
|
for (const methods of Object.values(baseOas.getPaths())) for (const operation of Object.values(methods)) {
|
|
2419
2479
|
if (!operation) continue;
|
|
2420
2480
|
const node = parseOperation(parserOptions, operation);
|
|
2421
|
-
if (node) yield dedupePlan ? _kubb_core.ast.applyDedupe(node, dedupePlan
|
|
2481
|
+
if (node) yield dedupePlan ? _kubb_core.ast.applyDedupe(node, dedupePlan) : node;
|
|
2422
2482
|
}
|
|
2423
2483
|
})();
|
|
2424
2484
|
} };
|