kommo-aiatende-api 1.2.3 → 1.2.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.js CHANGED
@@ -1495,6 +1495,18 @@ function isRetryableRequest(config) {
1495
1495
  if (IDEMPOTENT_METHODS.has(method)) return true;
1496
1496
  return method === "post" && config._idempotentRetry === true;
1497
1497
  }
1498
+ function isRateLimited(error) {
1499
+ return error.response?.status === 429;
1500
+ }
1501
+ function retryAfterMs(error) {
1502
+ const header = error.response?.headers?.["retry-after"];
1503
+ if (!header) return null;
1504
+ const seconds = Number(header);
1505
+ if (!Number.isNaN(seconds)) return seconds * 1e3;
1506
+ const dateMs = Date.parse(header);
1507
+ if (!Number.isNaN(dateMs)) return Math.max(0, dateMs - Date.now());
1508
+ return null;
1509
+ }
1498
1510
  function wait(ms) {
1499
1511
  return new Promise((resolve) => setTimeout(resolve, ms));
1500
1512
  }
@@ -1502,7 +1514,8 @@ function attachRetryInterceptor(client, retryConfig = {}) {
1502
1514
  const { retries = 2, baseDelayMs = 300, maxDelayMs = 3e3 } = retryConfig;
1503
1515
  client.interceptors.response.use(void 0, async (error) => {
1504
1516
  const config = error.config;
1505
- if (!config || retries <= 0 || !isTransientNetworkError(error) || !isRetryableRequest(config)) {
1517
+ const rateLimited = isRateLimited(error);
1518
+ if (!config || retries <= 0 || !rateLimited && (!isTransientNetworkError(error) || !isRetryableRequest(config))) {
1506
1519
  throw error;
1507
1520
  }
1508
1521
  const attempt = config._retryCount ?? 0;
@@ -1510,7 +1523,8 @@ function attachRetryInterceptor(client, retryConfig = {}) {
1510
1523
  throw error;
1511
1524
  }
1512
1525
  config._retryCount = attempt + 1;
1513
- const backoff = Math.min(maxDelayMs, baseDelayMs * 2 ** attempt);
1526
+ const exponential = Math.min(maxDelayMs, baseDelayMs * 2 ** attempt);
1527
+ const backoff = rateLimited ? retryAfterMs(error) ?? exponential : exponential;
1514
1528
  const jitter = backoff * 0.3 * Math.random();
1515
1529
  await wait(backoff + jitter);
1516
1530
  return client.request(config);