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

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
@@ -247,6 +247,20 @@ function pascalCase(text, { prefix = "", suffix = "" } = {}) {
247
247
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
248
248
  }
249
249
  //#endregion
250
+ //#region ../../internals/utils/src/errors.ts
251
+ /**
252
+ * Extracts a human-readable message from any thrown value.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * getErrorMessage(new Error('oops')) // 'oops'
257
+ * getErrorMessage('plain string') // 'plain string'
258
+ * ```
259
+ */
260
+ function getErrorMessage(value) {
261
+ return value instanceof Error ? value.message : String(value);
262
+ }
263
+ //#endregion
250
264
  //#region ../../internals/utils/src/runtime.ts
251
265
  /**
252
266
  * Detects the JavaScript runtime executing the current process and exposes its name and version.
@@ -349,11 +363,50 @@ async function read(path) {
349
363
  //#endregion
350
364
  //#region src/load/source.ts
351
365
  const urlRegExp = /^https?:\/+/i;
366
+ /**
367
+ * Node reports every connection failure as `TypeError: fetch failed` and keeps the useful part
368
+ * (`connect ECONNREFUSED 127.0.0.1:8000`) on `cause`, one level deeper again when a host resolves
369
+ * to several addresses and the attempts collect into an `AggregateError`.
370
+ */
371
+ function describeFetchFailure(error) {
372
+ if (error instanceof AggregateError && error.errors.length > 0) return describeFetchFailure(error.errors[0]);
373
+ if (error instanceof Error && error.cause instanceof Error) return describeFetchFailure(error.cause) || error.message;
374
+ return getErrorMessage(error);
375
+ }
376
+ function helpForStatus(status) {
377
+ 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.";
378
+ if (status === 404) return "Check the URL. Open it in a browser or with `curl` to confirm it serves the OpenAPI document.";
379
+ if (status >= 500) return "The server failed while serving the document. Check that it is healthy, then run Kubb again.";
380
+ 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.";
381
+ }
382
+ async function fetchSource(url) {
383
+ try {
384
+ return await fetch(url);
385
+ } catch (error) {
386
+ throw new _kubb_core.Diagnostics.Error({
387
+ code: _kubb_core.Diagnostics.code.inputUnreachable,
388
+ severity: "error",
389
+ message: `Cannot reach ${url.href}: ${describeFetchFailure(error)}`,
390
+ 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`.",
391
+ location: { kind: "config" },
392
+ cause: error instanceof Error ? error : void 0
393
+ });
394
+ }
395
+ }
352
396
  async function readSource(sourcePath) {
353
397
  if (urlRegExp.test(sourcePath)) {
354
398
  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})`);
399
+ const response = await fetchSource(url);
400
+ if (!response.ok) {
401
+ const status = response.statusText ? `${response.status} ${response.statusText}` : String(response.status);
402
+ throw new _kubb_core.Diagnostics.Error({
403
+ code: _kubb_core.Diagnostics.code.inputRequestFailed,
404
+ severity: "error",
405
+ message: `The server at ${url.href} answered with HTTP ${status} instead of the OpenAPI document.`,
406
+ help: helpForStatus(response.status),
407
+ location: { kind: "config" }
408
+ });
409
+ }
357
410
  return response.text();
358
411
  }
359
412
  return read(sourcePath);
@@ -369,7 +422,8 @@ async function resolveSource(sourcePath) {
369
422
  }
370
423
  /**
371
424
  * 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
425
+ * URLs are skipped: a remote input reports `KUBB_INPUT_REQUEST_FAILED` or `KUBB_INPUT_UNREACHABLE`
426
+ * from the request itself. A malformed but readable file is left for `parseDocument` to surface
373
427
  * its parse error instead.
374
428
  */
375
429
  async function assertInputExists(input) {