cencori 1.7.3 → 1.7.5

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.mjs CHANGED
@@ -1777,10 +1777,26 @@ var CencoriEmbeddedApiError = class extends Error {
1777
1777
  this.name = "CencoriEmbeddedApiError";
1778
1778
  }
1779
1779
  };
1780
- function parseRetryAfterSeconds(value) {
1780
+ var HTTP_DATE_PATTERNS = [
1781
+ // IMF-fixdate, e.g. Sun, 06 Nov 1994 08:49:37 GMT (what toUTCString emits)
1782
+ /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/,
1783
+ // RFC 850, e.g. Sunday, 06-Nov-94 08:49:37 GMT
1784
+ /^[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT$/,
1785
+ // asctime, e.g. Sun Nov 6 08:49:37 1994
1786
+ /^[A-Za-z]{3} [A-Za-z]{3} ( \d|\d{2}) \d{2}:\d{2}:\d{2} \d{4}$/
1787
+ ];
1788
+ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
1781
1789
  if (!value) return null;
1782
- const seconds = Number.parseInt(value.trim(), 10);
1783
- return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1790
+ const trimmed = value.trim();
1791
+ if (!trimmed) return null;
1792
+ if (/^[0-9]+$/.test(trimmed)) {
1793
+ const seconds = Number.parseInt(trimmed, 10);
1794
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1795
+ }
1796
+ if (!HTTP_DATE_PATTERNS.some((pattern) => pattern.test(trimmed))) return null;
1797
+ const timestamp = Date.parse(trimmed);
1798
+ if (!Number.isFinite(timestamp)) return null;
1799
+ return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
1784
1800
  }
1785
1801
  async function request(config, method, path, body, extraHeaders, options) {
1786
1802
  const response = await fetch(`${config.baseUrl}${path}`, {
@@ -1796,8 +1812,12 @@ async function request(config, method, path, body, extraHeaders, options) {
1796
1812
  });
1797
1813
  if (!response.ok) {
1798
1814
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
1799
- const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
1815
+ const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message ?? (typeof errorData.message === "string" ? errorData.message : null) ?? response.statusText;
1800
1816
  const details = typeof errorData.error === "object" ? errorData.error : null;
1817
+ const topCode = typeof errorData.code === "string" ? errorData.code : null;
1818
+ const topRequestId = typeof errorData.request_id === "string" ? errorData.request_id : typeof errorData.requestId === "string" ? errorData.requestId : null;
1819
+ const topType = typeof errorData.type === "string" ? errorData.type : null;
1820
+ const topParam = typeof errorData.param === "string" ? errorData.param : null;
1801
1821
  let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
1802
1822
  if (retryAfterSeconds == null) {
1803
1823
  const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
@@ -1810,7 +1830,7 @@ async function request(config, method, path, body, extraHeaders, options) {
1810
1830
  }
1811
1831
  }
1812
1832
  }
1813
- throw new CencoriEmbeddedApiError(message, response.status, details?.code ?? null, details?.request_id ?? response.headers.get("X-Request-Id"), details?.type ?? null, details?.param ?? null, retryAfterSeconds);
1833
+ throw new CencoriEmbeddedApiError(message, response.status, details?.code ?? topCode, details?.request_id ?? topRequestId ?? response.headers.get("X-Request-Id"), details?.type ?? topType, details?.param ?? topParam, retryAfterSeconds);
1814
1834
  }
1815
1835
  if (response.status === 204) return void 0;
1816
1836
  return response.json();
@@ -2422,8 +2442,7 @@ var Cencori = class {
2422
2442
  }
2423
2443
  if (response.status === 429) {
2424
2444
  const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2425
- const parsed = rawRetryAfter ? Number.parseInt(rawRetryAfter.trim(), 10) : NaN;
2426
- let retryAfterSeconds = Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
2445
+ let retryAfterSeconds = parseRetryAfterSeconds(rawRetryAfter);
2427
2446
  if (retryAfterSeconds == null) {
2428
2447
  const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2429
2448
  if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {