@kubb/adapter-oas 5.0.0-beta.51 → 5.0.0-beta.53

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 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 are bundled via
632
- * `@apidevtools/json-schema-ref-parser` to resolve external `$ref`s. Swagger 2.0 documents are
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
- const { $RefParser } = await import("@apidevtools/json-schema-ref-parser");
644
- return parseDocument(await $RefParser.bundle(pathOrApi), {
645
- canBundle: false,
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