generaltranslation 8.2.16 → 8.2.17

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.mjs CHANGED
@@ -340,6 +340,8 @@ function generateRequestHeaders(config, excludeContentType = false) {
340
340
  //#region src/translate/utils/apiRequest.ts
341
341
  const MAX_RETRIES = 3;
342
342
  const INITIAL_DELAY_MS = 500;
343
+ const RATE_LIMIT_RETRY_DELAY_MS = 6e4;
344
+ const MS_PER_SECOND = 1e3;
343
345
  function sleep(ms) {
344
346
  return new Promise((resolve) => setTimeout(resolve, ms));
345
347
  }
@@ -350,10 +352,31 @@ function getRetryDelay(policy, attempt) {
350
352
  default: return 0;
351
353
  }
352
354
  }
355
+ function parseDelayMs(value) {
356
+ if (!value) return;
357
+ const seconds = Number(value.split(",")[0].split(";")[0].trim());
358
+ if (!Number.isFinite(seconds) || seconds < 0) return;
359
+ return seconds * MS_PER_SECOND;
360
+ }
361
+ function parseRetryAfter(value) {
362
+ const delayMs = parseDelayMs(value);
363
+ if (delayMs !== void 0) return delayMs;
364
+ if (!value) return;
365
+ const retryDate = Date.parse(value);
366
+ if (Number.isNaN(retryDate)) return;
367
+ return Math.max(retryDate - Date.now(), 0);
368
+ }
369
+ function getRateLimitRetryDelay(response) {
370
+ return parseRetryAfter(response.headers.get("Retry-After")) ?? parseDelayMs(response.headers.get("RateLimit-Reset")) ?? RATE_LIMIT_RETRY_DELAY_MS;
371
+ }
372
+ function getResponseRetryDelay(response, policy, attempt) {
373
+ if (response.status === 429) return getRateLimitRetryDelay(response);
374
+ return getRetryDelay(policy, attempt);
375
+ }
353
376
  /**
354
377
  * @internal
355
378
  *
356
- * Makes an API request with automatic retry for 5XX errors.
379
+ * Makes an API request with automatic retry for 429 and 5XX errors.
357
380
  *
358
381
  * Encapsulates URL construction, fetch with timeout, error handling,
359
382
  * response validation, and JSON parsing.
@@ -385,8 +408,8 @@ async function apiRequest(config, endpoint, options) {
385
408
  }
386
409
  handleFetchError(error, timeout);
387
410
  }
388
- if (response.status >= 500 && attempt < maxRetries) {
389
- await sleep(getRetryDelay(retryPolicy, attempt));
411
+ if ((response.status === 429 || response.status >= 500) && attempt < maxRetries) {
412
+ await sleep(getResponseRetryDelay(response, retryPolicy, attempt));
390
413
  continue;
391
414
  }
392
415
  await validateResponse(response);