@kubb/adapter-oas 5.0.0-beta.107 → 5.0.0-beta.109

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
@@ -27,7 +27,6 @@ let node_fs_promises = require("node:fs/promises");
27
27
  let node_path = require("node:path");
28
28
  node_path = __toESM(node_path, 1);
29
29
  let yaml = require("yaml");
30
- let _readme_openapi_parser = require("@readme/openapi-parser");
31
30
  let _scalar_openapi_upgrader = require("@scalar/openapi-upgrader");
32
31
  let api_ref_bundler = require("api-ref-bundler");
33
32
  let _kubb_kit = require("@kubb/kit");
@@ -247,6 +246,20 @@ function pascalCase(text, { prefix = "", suffix = "" } = {}) {
247
246
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
248
247
  }
249
248
  //#endregion
249
+ //#region ../../internals/utils/src/errors.ts
250
+ /**
251
+ * Extracts a human-readable message from any thrown value.
252
+ *
253
+ * @example
254
+ * ```ts
255
+ * getErrorMessage(new Error('oops')) // 'oops'
256
+ * getErrorMessage('plain string') // 'plain string'
257
+ * ```
258
+ */
259
+ function getErrorMessage(value) {
260
+ return value instanceof Error ? value.message : String(value);
261
+ }
262
+ //#endregion
250
263
  //#region ../../internals/utils/src/runtime.ts
251
264
  /**
252
265
  * Detects the JavaScript runtime executing the current process and exposes its name and version.
@@ -349,11 +362,50 @@ async function read(path) {
349
362
  //#endregion
350
363
  //#region src/load/source.ts
351
364
  const urlRegExp = /^https?:\/+/i;
365
+ /**
366
+ * Node reports every connection failure as `TypeError: fetch failed` and keeps the useful part
367
+ * (`connect ECONNREFUSED 127.0.0.1:8000`) on `cause`, one level deeper again when a host resolves
368
+ * to several addresses and the attempts collect into an `AggregateError`.
369
+ */
370
+ function describeFetchFailure(error) {
371
+ if (error instanceof AggregateError && error.errors.length > 0) return describeFetchFailure(error.errors[0]);
372
+ if (error instanceof Error && error.cause instanceof Error) return describeFetchFailure(error.cause) || error.message;
373
+ return getErrorMessage(error);
374
+ }
375
+ function helpForStatus(status) {
376
+ if (status === 401 || status === 403) return "The server refused the request. Kubb sends no credentials, so serve the document without authentication or download it and set `input` to the local file.";
377
+ if (status === 404) return "Check the URL. Open it in a browser or with `curl` to confirm it serves the OpenAPI document.";
378
+ if (status >= 500) return "The server failed while serving the document. Check that it is healthy, then run Kubb again.";
379
+ return "Open the URL in a browser or with `curl` to see what the server returns, then point `input` at a URL that serves the OpenAPI document.";
380
+ }
381
+ async function fetchSource(url) {
382
+ try {
383
+ return await fetch(url);
384
+ } catch (error) {
385
+ throw new _kubb_core.Diagnostics.Error({
386
+ code: _kubb_core.Diagnostics.code.inputUnreachable,
387
+ severity: "error",
388
+ message: `Cannot reach ${url.href}: ${describeFetchFailure(error)}`,
389
+ help: "Check that the host is running and reachable from this machine. For a local server, start it and confirm the port matches the one in `input`.",
390
+ location: { kind: "config" },
391
+ cause: error instanceof Error ? error : void 0
392
+ });
393
+ }
394
+ }
352
395
  async function readSource(sourcePath) {
353
396
  if (urlRegExp.test(sourcePath)) {
354
397
  const url = new URL(sourcePath);
355
- const response = await fetch(url);
356
- if (!response.ok) throw new Error(`Cannot fetch the OAS document at ${url.href} (HTTP ${response.status})`);
398
+ const response = await fetchSource(url);
399
+ if (!response.ok) {
400
+ const status = response.statusText ? `${response.status} ${response.statusText}` : String(response.status);
401
+ throw new _kubb_core.Diagnostics.Error({
402
+ code: _kubb_core.Diagnostics.code.inputRequestFailed,
403
+ severity: "error",
404
+ message: `The server at ${url.href} answered with HTTP ${status} instead of the OpenAPI document.`,
405
+ help: helpForStatus(response.status),
406
+ location: { kind: "config" }
407
+ });
408
+ }
357
409
  return response.text();
358
410
  }
359
411
  return read(sourcePath);
@@ -369,7 +421,8 @@ async function resolveSource(sourcePath) {
369
421
  }
370
422
  /**
371
423
  * Throws a coded `KUBB_INPUT_NOT_FOUND` diagnostic when a local input path does not exist.
372
- * URLs are skipped, and a malformed but readable file is left for `parseDocument` to surface
424
+ * URLs are skipped: a remote input reports `KUBB_INPUT_REQUEST_FAILED` or `KUBB_INPUT_UNREACHABLE`
425
+ * from the request itself. A malformed but readable file is left for `parseDocument` to surface
373
426
  * its parse error instead.
374
427
  */
375
428
  async function assertInputExists(input) {
@@ -475,9 +528,10 @@ function assertDocument(document) {
475
528
  * ```
476
529
  */
477
530
  async function validateDocument(document, { throwOnError = false } = {}) {
531
+ const { compileErrors, validate } = await import("@readme/openapi-parser");
478
532
  try {
479
- const result = await (0, _readme_openapi_parser.validate)(structuredClone(document), { validate: { errors: { colorize: true } } });
480
- if (!result.valid) throw new Error((0, _readme_openapi_parser.compileErrors)(result));
533
+ const result = await validate(structuredClone(document), { validate: { errors: { colorize: true } } });
534
+ if (!result.valid) throw new Error(compileErrors(result));
481
535
  } catch (error) {
482
536
  if (throwOnError) throw error;
483
537
  }