cencori 1.7.2 → 1.7.3

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,22 @@ 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
+ function parseRetryAfterSeconds(value) {
1849
+ if (!value) return null;
1850
+ const seconds = Number.parseInt(value.trim(), 10);
1851
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1852
+ }
1847
1853
  async function request(config, method, path, body, extraHeaders, options) {
1848
1854
  const response = await fetch(`${config.baseUrl}${path}`, {
1849
1855
  method,
@@ -1860,7 +1866,19 @@ async function request(config, method, path, body, extraHeaders, options) {
1860
1866
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
1861
1867
  const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
1862
1868
  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);
1869
+ let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
1870
+ if (retryAfterSeconds == null) {
1871
+ const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
1872
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
1873
+ retryAfterSeconds = Math.ceil(secHint);
1874
+ } else {
1875
+ const msHint = details?.retry_after_ms ?? errorData.retry_after_ms;
1876
+ if (typeof msHint === "number" && Number.isFinite(msHint) && msHint >= 0) {
1877
+ retryAfterSeconds = Math.ceil(msHint / 1e3);
1878
+ }
1879
+ }
1880
+ }
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);
1864
1882
  }
1865
1883
  if (response.status === 204) return void 0;
1866
1884
  return response.json();
@@ -2345,8 +2363,9 @@ var AuthenticationError = class _AuthenticationError extends CencoriError {
2345
2363
  }
2346
2364
  };
2347
2365
  var RateLimitError = class _RateLimitError extends CencoriError {
2348
- constructor(message = "Rate limit exceeded") {
2366
+ constructor(message = "Rate limit exceeded", retryAfterSeconds) {
2349
2367
  super(message, 429, "RATE_LIMIT_EXCEEDED");
2368
+ this.retryAfterSeconds = retryAfterSeconds;
2350
2369
  this.name = "RateLimitError";
2351
2370
  Object.setPrototypeOf(this, _RateLimitError.prototype);
2352
2371
  }
@@ -2470,7 +2489,18 @@ var Cencori = class {
2470
2489
  throw new AuthenticationError(errorData.error || "Invalid API key");
2471
2490
  }
2472
2491
  if (response.status === 429) {
2473
- throw new RateLimitError(errorData.error || "Rate limit exceeded");
2492
+ 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;
2495
+ if (retryAfterSeconds == null) {
2496
+ const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2497
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
2498
+ retryAfterSeconds = Math.ceil(secHint);
2499
+ } else if (typeof errorData.retry_after_ms === "number" && Number.isFinite(errorData.retry_after_ms) && errorData.retry_after_ms >= 0) {
2500
+ retryAfterSeconds = Math.ceil(errorData.retry_after_ms / 1e3);
2501
+ }
2502
+ }
2503
+ throw new RateLimitError(errorData.error || "Rate limit exceeded", retryAfterSeconds);
2474
2504
  }
2475
2505
  if (response.status === 400 && errorData.reasons) {
2476
2506
  throw new SafetyError(errorData.error, errorData.reasons);