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