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.js CHANGED
@@ -1845,10 +1845,26 @@ var CencoriEmbeddedApiError = class extends Error {
1845
1845
  this.name = "CencoriEmbeddedApiError";
1846
1846
  }
1847
1847
  };
1848
- function parseRetryAfterSeconds(value) {
1848
+ var HTTP_DATE_PATTERNS = [
1849
+ // IMF-fixdate, e.g. Sun, 06 Nov 1994 08:49:37 GMT (what toUTCString emits)
1850
+ /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/,
1851
+ // RFC 850, e.g. Sunday, 06-Nov-94 08:49:37 GMT
1852
+ /^[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT$/,
1853
+ // asctime, e.g. Sun Nov 6 08:49:37 1994
1854
+ /^[A-Za-z]{3} [A-Za-z]{3} ( \d|\d{2}) \d{2}:\d{2}:\d{2} \d{4}$/
1855
+ ];
1856
+ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
1849
1857
  if (!value) return null;
1850
- const seconds = Number.parseInt(value.trim(), 10);
1851
- return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1858
+ const trimmed = value.trim();
1859
+ if (!trimmed) return null;
1860
+ if (/^[0-9]+$/.test(trimmed)) {
1861
+ const seconds = Number.parseInt(trimmed, 10);
1862
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1863
+ }
1864
+ if (!HTTP_DATE_PATTERNS.some((pattern) => pattern.test(trimmed))) return null;
1865
+ const timestamp = Date.parse(trimmed);
1866
+ if (!Number.isFinite(timestamp)) return null;
1867
+ return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
1852
1868
  }
1853
1869
  async function request(config, method, path, body, extraHeaders, options) {
1854
1870
  const response = await fetch(`${config.baseUrl}${path}`, {
@@ -1864,8 +1880,12 @@ async function request(config, method, path, body, extraHeaders, options) {
1864
1880
  });
1865
1881
  if (!response.ok) {
1866
1882
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
1867
- const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
1883
+ const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message ?? (typeof errorData.message === "string" ? errorData.message : null) ?? response.statusText;
1868
1884
  const details = typeof errorData.error === "object" ? errorData.error : null;
1885
+ const topCode = typeof errorData.code === "string" ? errorData.code : null;
1886
+ const topRequestId = typeof errorData.request_id === "string" ? errorData.request_id : typeof errorData.requestId === "string" ? errorData.requestId : null;
1887
+ const topType = typeof errorData.type === "string" ? errorData.type : null;
1888
+ const topParam = typeof errorData.param === "string" ? errorData.param : null;
1869
1889
  let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
1870
1890
  if (retryAfterSeconds == null) {
1871
1891
  const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
@@ -1878,7 +1898,7 @@ async function request(config, method, path, body, extraHeaders, options) {
1878
1898
  }
1879
1899
  }
1880
1900
  }
1881
- 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);
1901
+ 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);
1882
1902
  }
1883
1903
  if (response.status === 204) return void 0;
1884
1904
  return response.json();
@@ -2490,8 +2510,7 @@ var Cencori = class {
2490
2510
  }
2491
2511
  if (response.status === 429) {
2492
2512
  const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2493
- const parsed = rawRetryAfter ? Number.parseInt(rawRetryAfter.trim(), 10) : NaN;
2494
- let retryAfterSeconds = Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
2513
+ let retryAfterSeconds = parseRetryAfterSeconds(rawRetryAfter);
2495
2514
  if (retryAfterSeconds == null) {
2496
2515
  const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2497
2516
  if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {