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