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 +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +34 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +34 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1766,16 +1766,22 @@ var WebNamespace = class {
|
|
|
1766
1766
|
|
|
1767
1767
|
// src/embedded/index.ts
|
|
1768
1768
|
var CencoriEmbeddedApiError = class extends Error {
|
|
1769
|
-
constructor(message, status, code, requestId, type, param) {
|
|
1769
|
+
constructor(message, status, code, requestId, type, param, retryAfterSeconds = null) {
|
|
1770
1770
|
super(`Cencori API error: ${message}`);
|
|
1771
1771
|
this.status = status;
|
|
1772
1772
|
this.code = code;
|
|
1773
1773
|
this.requestId = requestId;
|
|
1774
1774
|
this.type = type;
|
|
1775
1775
|
this.param = param;
|
|
1776
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
1776
1777
|
this.name = "CencoriEmbeddedApiError";
|
|
1777
1778
|
}
|
|
1778
1779
|
};
|
|
1780
|
+
function parseRetryAfterSeconds(value) {
|
|
1781
|
+
if (!value) return null;
|
|
1782
|
+
const seconds = Number.parseInt(value.trim(), 10);
|
|
1783
|
+
return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
|
|
1784
|
+
}
|
|
1779
1785
|
async function request(config, method, path, body, extraHeaders, options) {
|
|
1780
1786
|
const response = await fetch(`${config.baseUrl}${path}`, {
|
|
1781
1787
|
method,
|
|
@@ -1792,7 +1798,19 @@ async function request(config, method, path, body, extraHeaders, options) {
|
|
|
1792
1798
|
const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
|
|
1793
1799
|
const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
|
|
1794
1800
|
const details = typeof errorData.error === "object" ? errorData.error : null;
|
|
1795
|
-
|
|
1801
|
+
let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
|
|
1802
|
+
if (retryAfterSeconds == null) {
|
|
1803
|
+
const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
|
|
1804
|
+
if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
|
|
1805
|
+
retryAfterSeconds = Math.ceil(secHint);
|
|
1806
|
+
} else {
|
|
1807
|
+
const msHint = details?.retry_after_ms ?? errorData.retry_after_ms;
|
|
1808
|
+
if (typeof msHint === "number" && Number.isFinite(msHint) && msHint >= 0) {
|
|
1809
|
+
retryAfterSeconds = Math.ceil(msHint / 1e3);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
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);
|
|
1796
1814
|
}
|
|
1797
1815
|
if (response.status === 204) return void 0;
|
|
1798
1816
|
return response.json();
|
|
@@ -2277,8 +2295,9 @@ var AuthenticationError = class _AuthenticationError extends CencoriError {
|
|
|
2277
2295
|
}
|
|
2278
2296
|
};
|
|
2279
2297
|
var RateLimitError = class _RateLimitError extends CencoriError {
|
|
2280
|
-
constructor(message = "Rate limit exceeded") {
|
|
2298
|
+
constructor(message = "Rate limit exceeded", retryAfterSeconds) {
|
|
2281
2299
|
super(message, 429, "RATE_LIMIT_EXCEEDED");
|
|
2300
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
2282
2301
|
this.name = "RateLimitError";
|
|
2283
2302
|
Object.setPrototypeOf(this, _RateLimitError.prototype);
|
|
2284
2303
|
}
|
|
@@ -2402,7 +2421,18 @@ var Cencori = class {
|
|
|
2402
2421
|
throw new AuthenticationError(errorData.error || "Invalid API key");
|
|
2403
2422
|
}
|
|
2404
2423
|
if (response.status === 429) {
|
|
2405
|
-
|
|
2424
|
+
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;
|
|
2427
|
+
if (retryAfterSeconds == null) {
|
|
2428
|
+
const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
|
|
2429
|
+
if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
|
|
2430
|
+
retryAfterSeconds = Math.ceil(secHint);
|
|
2431
|
+
} else if (typeof errorData.retry_after_ms === "number" && Number.isFinite(errorData.retry_after_ms) && errorData.retry_after_ms >= 0) {
|
|
2432
|
+
retryAfterSeconds = Math.ceil(errorData.retry_after_ms / 1e3);
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
throw new RateLimitError(errorData.error || "Rate limit exceeded", retryAfterSeconds);
|
|
2406
2436
|
}
|
|
2407
2437
|
if (response.status === 400 && errorData.reasons) {
|
|
2408
2438
|
throw new SafetyError(errorData.error, errorData.reasons);
|