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.
@@ -42,6 +42,8 @@ import handleFetchError from './handleFetchError';
42
42
  import generateRequestHeaders from './generateRequestHeaders';
43
43
  var MAX_RETRIES = 3;
44
44
  var INITIAL_DELAY_MS = 500;
45
+ var RATE_LIMIT_RETRY_DELAY_MS = 60000;
46
+ var MS_PER_SECOND = 1000;
45
47
  function sleep(ms) {
46
48
  return new Promise(function (resolve) { return setTimeout(resolve, ms); });
47
49
  }
@@ -55,10 +57,44 @@ function getRetryDelay(policy, attempt) {
55
57
  return 0;
56
58
  }
57
59
  }
60
+ function parseDelayMs(value) {
61
+ if (!value) {
62
+ return undefined;
63
+ }
64
+ var seconds = Number(value.split(',')[0].split(';')[0].trim());
65
+ if (!Number.isFinite(seconds) || seconds < 0) {
66
+ return undefined;
67
+ }
68
+ return seconds * MS_PER_SECOND;
69
+ }
70
+ function parseRetryAfter(value) {
71
+ var delayMs = parseDelayMs(value);
72
+ if (delayMs !== undefined) {
73
+ return delayMs;
74
+ }
75
+ if (!value) {
76
+ return undefined;
77
+ }
78
+ var retryDate = Date.parse(value);
79
+ if (Number.isNaN(retryDate)) {
80
+ return undefined;
81
+ }
82
+ return Math.max(retryDate - Date.now(), 0);
83
+ }
84
+ function getRateLimitRetryDelay(response) {
85
+ var _a, _b;
86
+ return ((_b = (_a = parseRetryAfter(response.headers.get('Retry-After'))) !== null && _a !== void 0 ? _a : parseDelayMs(response.headers.get('RateLimit-Reset'))) !== null && _b !== void 0 ? _b : RATE_LIMIT_RETRY_DELAY_MS);
87
+ }
88
+ function getResponseRetryDelay(response, policy, attempt) {
89
+ if (response.status === 429) {
90
+ return getRateLimitRetryDelay(response);
91
+ }
92
+ return getRetryDelay(policy, attempt);
93
+ }
58
94
  /**
59
95
  * @internal
60
96
  *
61
- * Makes an API request with automatic retry for 5XX errors.
97
+ * Makes an API request with automatic retry for 429 and 5XX errors.
62
98
  *
63
99
  * Encapsulates URL construction, fetch with timeout, error handling,
64
100
  * response validation, and JSON parsing.
@@ -110,8 +146,9 @@ export default function apiRequest(config, endpoint, options) {
110
146
  handleFetchError(error_1, timeout);
111
147
  return [3 /*break*/, 7];
112
148
  case 7:
113
- if (!(response.status >= 500 && attempt < maxRetries)) return [3 /*break*/, 9];
114
- return [4 /*yield*/, sleep(getRetryDelay(retryPolicy, attempt))];
149
+ if (!((response.status === 429 || response.status >= 500) &&
150
+ attempt < maxRetries)) return [3 /*break*/, 9];
151
+ return [4 /*yield*/, sleep(getResponseRetryDelay(response, retryPolicy, attempt))];
115
152
  case 8:
116
153
  _d.sent();
117
154
  return [3 /*break*/, 12];
@@ -12,14 +12,9 @@ var __assign = (this && this.__assign) || function () {
12
12
  import { API_VERSION } from '../api';
13
13
  export default function generateRequestHeaders(config, excludeContentType) {
14
14
  if (excludeContentType === void 0) { excludeContentType = false; }
15
- var authHeaders = __assign(__assign({}, (!excludeContentType && { 'Content-Type': 'application/json' })), { 'x-gt-project-id': config.projectId });
15
+ var authHeaders = __assign(__assign({}, (!excludeContentType && { 'Content-Type': 'application/json' })), { 'gt-project-id': config.projectId });
16
16
  if (config.apiKey) {
17
- if (config.apiKey.startsWith('gtx-internal-')) {
18
- authHeaders['x-gt-internal-api-key'] = config.apiKey;
19
- }
20
- else {
21
- authHeaders['x-gt-api-key'] = config.apiKey;
22
- }
17
+ authHeaders['Authorization'] = "Bearer ".concat(config.apiKey);
23
18
  }
24
19
  authHeaders['gt-api-version'] = API_VERSION;
25
20
  return authHeaders;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "8.2.16",
3
+ "version": "8.2.18",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",