cencori 1.7.2 → 1.7.4

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.d.mts CHANGED
@@ -326,8 +326,9 @@ declare class CencoriEmbeddedApiError extends Error {
326
326
  readonly requestId: string | null;
327
327
  readonly type: string | null;
328
328
  readonly param: string | null;
329
+ readonly retryAfterSeconds: number | null;
329
330
  readonly name = "CencoriEmbeddedApiError";
330
- constructor(message: string, status: number, code: string | null, requestId: string | null, type: string | null, param: string | null);
331
+ constructor(message: string, status: number, code: string | null, requestId: string | null, type: string | null, param: string | null, retryAfterSeconds?: number | null);
331
332
  }
332
333
  interface Tenant {
333
334
  id: string;
@@ -1117,7 +1118,8 @@ declare class AuthenticationError extends CencoriError {
1117
1118
  constructor(message?: string);
1118
1119
  }
1119
1120
  declare class RateLimitError extends CencoriError {
1120
- constructor(message?: string);
1121
+ retryAfterSeconds?: number | null | undefined;
1122
+ constructor(message?: string, retryAfterSeconds?: number | null | undefined);
1121
1123
  }
1122
1124
  declare class SafetyError extends CencoriError {
1123
1125
  reasons?: string[] | undefined;
package/dist/index.d.ts CHANGED
@@ -326,8 +326,9 @@ declare class CencoriEmbeddedApiError extends Error {
326
326
  readonly requestId: string | null;
327
327
  readonly type: string | null;
328
328
  readonly param: string | null;
329
+ readonly retryAfterSeconds: number | null;
329
330
  readonly name = "CencoriEmbeddedApiError";
330
- constructor(message: string, status: number, code: string | null, requestId: string | null, type: string | null, param: string | null);
331
+ constructor(message: string, status: number, code: string | null, requestId: string | null, type: string | null, param: string | null, retryAfterSeconds?: number | null);
331
332
  }
332
333
  interface Tenant {
333
334
  id: string;
@@ -1117,7 +1118,8 @@ declare class AuthenticationError extends CencoriError {
1117
1118
  constructor(message?: string);
1118
1119
  }
1119
1120
  declare class RateLimitError extends CencoriError {
1120
- constructor(message?: string);
1121
+ retryAfterSeconds?: number | null | undefined;
1122
+ constructor(message?: string, retryAfterSeconds?: number | null | undefined);
1121
1123
  }
1122
1124
  declare class SafetyError extends CencoriError {
1123
1125
  reasons?: string[] | undefined;
package/dist/index.js CHANGED
@@ -1834,16 +1834,38 @@ var WebNamespace = class {
1834
1834
 
1835
1835
  // src/embedded/index.ts
1836
1836
  var CencoriEmbeddedApiError = class extends Error {
1837
- constructor(message, status, code, requestId, type, param) {
1837
+ constructor(message, status, code, requestId, type, param, retryAfterSeconds = null) {
1838
1838
  super(`Cencori API error: ${message}`);
1839
1839
  this.status = status;
1840
1840
  this.code = code;
1841
1841
  this.requestId = requestId;
1842
1842
  this.type = type;
1843
1843
  this.param = param;
1844
+ this.retryAfterSeconds = retryAfterSeconds;
1844
1845
  this.name = "CencoriEmbeddedApiError";
1845
1846
  }
1846
1847
  };
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()) {
1857
+ if (!value) return 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));
1868
+ }
1847
1869
  async function request(config, method, path, body, extraHeaders, options) {
1848
1870
  const response = await fetch(`${config.baseUrl}${path}`, {
1849
1871
  method,
@@ -1860,7 +1882,19 @@ async function request(config, method, path, body, extraHeaders, options) {
1860
1882
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
1861
1883
  const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
1862
1884
  const details = typeof errorData.error === "object" ? errorData.error : null;
1863
- throw new CencoriEmbeddedApiError(message, response.status, details?.code ?? null, details?.request_id ?? response.headers.get("X-Request-Id"), details?.type ?? null, details?.param ?? null);
1885
+ let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
1886
+ if (retryAfterSeconds == null) {
1887
+ const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
1888
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
1889
+ retryAfterSeconds = Math.ceil(secHint);
1890
+ } else {
1891
+ const msHint = details?.retry_after_ms ?? errorData.retry_after_ms;
1892
+ if (typeof msHint === "number" && Number.isFinite(msHint) && msHint >= 0) {
1893
+ retryAfterSeconds = Math.ceil(msHint / 1e3);
1894
+ }
1895
+ }
1896
+ }
1897
+ 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);
1864
1898
  }
1865
1899
  if (response.status === 204) return void 0;
1866
1900
  return response.json();
@@ -2345,8 +2379,9 @@ var AuthenticationError = class _AuthenticationError extends CencoriError {
2345
2379
  }
2346
2380
  };
2347
2381
  var RateLimitError = class _RateLimitError extends CencoriError {
2348
- constructor(message = "Rate limit exceeded") {
2382
+ constructor(message = "Rate limit exceeded", retryAfterSeconds) {
2349
2383
  super(message, 429, "RATE_LIMIT_EXCEEDED");
2384
+ this.retryAfterSeconds = retryAfterSeconds;
2350
2385
  this.name = "RateLimitError";
2351
2386
  Object.setPrototypeOf(this, _RateLimitError.prototype);
2352
2387
  }
@@ -2470,7 +2505,17 @@ var Cencori = class {
2470
2505
  throw new AuthenticationError(errorData.error || "Invalid API key");
2471
2506
  }
2472
2507
  if (response.status === 429) {
2473
- throw new RateLimitError(errorData.error || "Rate limit exceeded");
2508
+ const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2509
+ let retryAfterSeconds = parseRetryAfterSeconds(rawRetryAfter);
2510
+ if (retryAfterSeconds == null) {
2511
+ const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2512
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
2513
+ retryAfterSeconds = Math.ceil(secHint);
2514
+ } else if (typeof errorData.retry_after_ms === "number" && Number.isFinite(errorData.retry_after_ms) && errorData.retry_after_ms >= 0) {
2515
+ retryAfterSeconds = Math.ceil(errorData.retry_after_ms / 1e3);
2516
+ }
2517
+ }
2518
+ throw new RateLimitError(errorData.error || "Rate limit exceeded", retryAfterSeconds);
2474
2519
  }
2475
2520
  if (response.status === 400 && errorData.reasons) {
2476
2521
  throw new SafetyError(errorData.error, errorData.reasons);