generaltranslation 8.2.16 → 8.2.18
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 +12 -0
- package/dist/index.cjs +28 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +28 -6
- package/dist/index.mjs.map +1 -1
- package/dist/translate/utils/apiRequest.js +40 -3
- package/dist/translate/utils/generateRequestHeaders.js +2 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# generaltranslation
|
|
2
2
|
|
|
3
|
+
## 8.2.18
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1648](https://github.com/generaltranslation/gt/pull/1648) [`9709a2f`](https://github.com/generaltranslation/gt/commit/9709a2f2b97b9d8239298e39bb31e57692bbffd8) Thanks [@brian-lou](https://github.com/brian-lou)! - Use `gt-project-id` instead of `x-gt-project-id` for API request headers. Update the API key header to use standard `Authorization: Bearer <api-key>` prefix.
|
|
8
|
+
|
|
9
|
+
## 8.2.17
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#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
|
|
14
|
+
|
|
3
15
|
## 8.2.16
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -330,10 +330,9 @@ const API_VERSION$1 = "2026-03-06.v1";
|
|
|
330
330
|
function generateRequestHeaders(config, excludeContentType = false) {
|
|
331
331
|
const authHeaders = {
|
|
332
332
|
...!excludeContentType && { "Content-Type": "application/json" },
|
|
333
|
-
"
|
|
333
|
+
"gt-project-id": config.projectId
|
|
334
334
|
};
|
|
335
|
-
if (config.apiKey)
|
|
336
|
-
else authHeaders["x-gt-api-key"] = config.apiKey;
|
|
335
|
+
if (config.apiKey) authHeaders["Authorization"] = `Bearer ${config.apiKey}`;
|
|
337
336
|
authHeaders["gt-api-version"] = API_VERSION$1;
|
|
338
337
|
return authHeaders;
|
|
339
338
|
}
|
|
@@ -341,6 +340,8 @@ function generateRequestHeaders(config, excludeContentType = false) {
|
|
|
341
340
|
//#region src/translate/utils/apiRequest.ts
|
|
342
341
|
const MAX_RETRIES = 3;
|
|
343
342
|
const INITIAL_DELAY_MS = 500;
|
|
343
|
+
const RATE_LIMIT_RETRY_DELAY_MS = 6e4;
|
|
344
|
+
const MS_PER_SECOND = 1e3;
|
|
344
345
|
function sleep(ms) {
|
|
345
346
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
346
347
|
}
|
|
@@ -351,10 +352,31 @@ function getRetryDelay(policy, attempt) {
|
|
|
351
352
|
default: return 0;
|
|
352
353
|
}
|
|
353
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
|
+
}
|
|
354
376
|
/**
|
|
355
377
|
* @internal
|
|
356
378
|
*
|
|
357
|
-
* Makes an API request with automatic retry for 5XX errors.
|
|
379
|
+
* Makes an API request with automatic retry for 429 and 5XX errors.
|
|
358
380
|
*
|
|
359
381
|
* Encapsulates URL construction, fetch with timeout, error handling,
|
|
360
382
|
* response validation, and JSON parsing.
|
|
@@ -386,8 +408,8 @@ async function apiRequest(config, endpoint, options) {
|
|
|
386
408
|
}
|
|
387
409
|
handleFetchError(error, timeout);
|
|
388
410
|
}
|
|
389
|
-
if (response.status >= 500 && attempt < maxRetries) {
|
|
390
|
-
await sleep(
|
|
411
|
+
if ((response.status === 429 || response.status >= 500) && attempt < maxRetries) {
|
|
412
|
+
await sleep(getResponseRetryDelay(response, retryPolicy, attempt));
|
|
391
413
|
continue;
|
|
392
414
|
}
|
|
393
415
|
await validateResponse(response);
|