@internxt/sdk 1.15.14 → 1.16.0

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.
Files changed (38) hide show
  1. package/dist/auth/index.js +180 -262
  2. package/dist/auth/types.js +2 -22
  3. package/dist/drive/backups/index.js +34 -32
  4. package/dist/drive/payments/index.js +96 -149
  5. package/dist/drive/payments/object-storage.js +34 -36
  6. package/dist/drive/referrals/index.js +19 -17
  7. package/dist/drive/share/index.js +168 -235
  8. package/dist/drive/storage/index.js +213 -302
  9. package/dist/drive/trash/index.js +40 -84
  10. package/dist/drive/users/index.js +95 -140
  11. package/dist/mail/index.d.ts +124 -2
  12. package/dist/mail/index.js +170 -16
  13. package/dist/mail/types.d.ts +50 -0
  14. package/dist/mail/types.js +6 -0
  15. package/dist/meet/index.js +31 -99
  16. package/dist/misc/location/index.js +10 -50
  17. package/dist/network/download.js +38 -107
  18. package/dist/network/errors/codes.js +8 -24
  19. package/dist/network/errors/context.js +9 -26
  20. package/dist/network/errors/download.js +6 -24
  21. package/dist/network/errors/upload.js +21 -48
  22. package/dist/network/index.js +120 -277
  23. package/dist/network/types.js +3 -4
  24. package/dist/network/upload.js +63 -133
  25. package/dist/payments/checkout.js +57 -69
  26. package/dist/send/send.js +17 -27
  27. package/dist/shared/headers/index.js +56 -42
  28. package/dist/shared/http/client.js +95 -207
  29. package/dist/shared/http/retryWithBackoff.js +36 -110
  30. package/dist/shared/types/errors.js +37 -51
  31. package/dist/workspaces/index.js +224 -264
  32. package/package.json +14 -15
  33. package/dist/mail/api.d.ts +0 -126
  34. package/dist/mail/api.js +0 -288
  35. package/dist/mail/crypto.d.ts +0 -66
  36. package/dist/mail/crypto.js +0 -156
  37. package/dist/mail/mail.d.ts +0 -162
  38. package/dist/mail/mail.js +0 -382
@@ -1,312 +1,202 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
6
  exports.HttpClient = void 0;
43
- var axios_1 = __importDefault(require("axios"));
44
- var errors_1 = require("../types/errors");
45
- var retryWithBackoff_1 = require("./retryWithBackoff");
46
- var HttpClient = /** @class */ (function () {
47
- function HttpClient(baseURL, unauthorizedCallback, retryOptions) {
48
- var _this = this;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const errors_1 = require("../types/errors");
9
+ const retryWithBackoff_1 = require("./retryWithBackoff");
10
+ class HttpClient {
11
+ axios;
12
+ unauthorizedCallback;
13
+ retryOptions;
14
+ static globalInterceptors = [];
15
+ static globalRetryOptions;
16
+ static setGlobalInterceptors(interceptors) {
17
+ HttpClient.globalInterceptors = interceptors;
18
+ }
19
+ /**
20
+ * Enables global retry with backoff for rate limit errors (429) across every HttpClient instance.
21
+ * @param [options] - Optional retry configuration options
22
+ * @param [options.maxRetries] - Maximum number of retry attempts (default: 5)
23
+ * @param [options.maxRetryAfter] - Maximum wait time in ms regardless of retry-after header value (default: 70000)
24
+ * @param [options.onRetry] - Callback invoked before each retry with the attempt number and delay in ms
25
+ */
26
+ static enableGlobalRetry(options) {
27
+ HttpClient.globalRetryOptions = (options ?? {});
28
+ }
29
+ static create(baseURL, unauthorizedCallback, retryOptions) {
30
+ if (unauthorizedCallback === undefined) {
31
+ unauthorizedCallback = () => null;
32
+ }
33
+ return new HttpClient(baseURL, unauthorizedCallback, retryOptions);
34
+ }
35
+ constructor(baseURL, unauthorizedCallback, retryOptions) {
49
36
  this.axios = axios_1.default.create({
50
37
  baseURL: baseURL,
51
38
  });
52
39
  this.unauthorizedCallback = unauthorizedCallback;
53
40
  this.retryOptions = retryOptions;
54
- HttpClient.globalInterceptors.forEach(function (interceptor) {
41
+ HttpClient.globalInterceptors.forEach((interceptor) => {
55
42
  if (interceptor.request) {
56
- _this.axios.interceptors.request.use(interceptor.request.onFulfilled, interceptor.request.onRejected);
43
+ this.axios.interceptors.request.use(interceptor.request.onFulfilled, interceptor.request.onRejected);
57
44
  }
58
45
  if (interceptor.response) {
59
- _this.axios.interceptors.response.use(interceptor.response.onFulfilled, interceptor.response.onRejected);
46
+ this.axios.interceptors.response.use(interceptor.response.onFulfilled, interceptor.response.onRejected);
60
47
  }
61
48
  });
62
49
  this.initializeMiddleware();
63
50
  }
64
- HttpClient.setGlobalInterceptors = function (interceptors) {
65
- HttpClient.globalInterceptors = interceptors;
66
- };
67
- /**
68
- * Enables global retry with backoff for rate limit errors (429) across every HttpClient instance.
69
- * @param [options] - Optional retry configuration options
70
- * @param [options.maxRetries] - Maximum number of retry attempts (default: 5)
71
- * @param [options.maxRetryAfter] - Maximum wait time in ms regardless of retry-after header value (default: 70000)
72
- * @param [options.onRetry] - Callback invoked before each retry with the attempt number and delay in ms
73
- */
74
- HttpClient.enableGlobalRetry = function (options) {
75
- HttpClient.globalRetryOptions = (options !== null && options !== void 0 ? options : {});
76
- };
77
- HttpClient.create = function (baseURL, unauthorizedCallback, retryOptions) {
78
- if (unauthorizedCallback === undefined) {
79
- unauthorizedCallback = function () { return null; };
80
- }
81
- return new HttpClient(baseURL, unauthorizedCallback, retryOptions);
82
- };
83
- HttpClient.prototype.execute = function (fn) {
84
- var _a;
85
- var options = (_a = this.retryOptions) !== null && _a !== void 0 ? _a : HttpClient.globalRetryOptions;
51
+ execute(fn) {
52
+ const options = this.retryOptions ?? HttpClient.globalRetryOptions;
86
53
  if (!options) {
87
54
  return fn();
88
55
  }
89
56
  return (0, retryWithBackoff_1.retryWithBackoff)(fn, options);
90
- };
57
+ }
91
58
  /**
92
59
  * Requests a GET
93
60
  * @param url
94
61
  * @param headers
95
62
  */
96
- HttpClient.prototype.get = function (url, headers) {
97
- return __awaiter(this, void 0, void 0, function () {
98
- var _this = this;
99
- return __generator(this, function (_a) {
100
- switch (_a.label) {
101
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.get(url, { headers: headers }); })];
102
- case 1: return [2 /*return*/, _a.sent()];
103
- }
104
- });
105
- });
106
- };
63
+ async get(url, headers) {
64
+ return await this.execute(() => this.axios.get(url, { headers }));
65
+ }
107
66
  /**
108
67
  * Requests a GET
109
68
  * @param url
110
69
  * @param params
111
70
  * @param headers
112
71
  */
113
- HttpClient.prototype.getWithParams = function (url, params, headers) {
114
- return __awaiter(this, void 0, void 0, function () {
115
- var _this = this;
116
- return __generator(this, function (_a) {
117
- switch (_a.label) {
118
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.get(url, { params: params, headers: headers }); })];
119
- case 1: return [2 /*return*/, _a.sent()];
120
- }
121
- });
122
- });
123
- };
72
+ async getWithParams(url, params, headers) {
73
+ return await this.execute(() => this.axios.get(url, { params, headers }));
74
+ }
124
75
  /**
125
76
  * Requests a GET with option to cancel
126
77
  * @param url
127
78
  * @param headers
128
79
  */
129
- HttpClient.prototype.getCancellable = function (url, headers) {
130
- var _this = this;
131
- var currentCancel = function () { };
132
- var requestCanceler = { cancel: function (message) { return currentCancel(message); } };
133
- var promise = this.execute(function () {
134
- var source = axios_1.default.CancelToken.source();
80
+ getCancellable(url, headers) {
81
+ let currentCancel = () => { };
82
+ const requestCanceler = { cancel: (message) => currentCancel(message) };
83
+ const promise = this.execute(() => {
84
+ const source = axios_1.default.CancelToken.source();
135
85
  currentCancel = source.cancel;
136
- return _this.axios.get(url, { headers: headers, cancelToken: source.token });
86
+ return this.axios.get(url, { headers, cancelToken: source.token });
137
87
  });
138
- return { promise: promise, requestCanceler: requestCanceler };
139
- };
88
+ return { promise, requestCanceler };
89
+ }
140
90
  /**
141
91
  * Requests a POST
142
92
  * @param url
143
93
  * @param params
144
94
  * @param headers
145
95
  */
146
- HttpClient.prototype.post = function (url, params, headers, signal) {
147
- return __awaiter(this, void 0, void 0, function () {
148
- var _this = this;
149
- return __generator(this, function (_a) {
150
- switch (_a.label) {
151
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.post(url, params, { headers: headers, signal: signal }); })];
152
- case 1: return [2 /*return*/, _a.sent()];
153
- }
154
- });
155
- });
156
- };
96
+ async post(url, params, headers, signal) {
97
+ return await this.execute(() => this.axios.post(url, params, { headers, signal }));
98
+ }
157
99
  /**
158
100
  * Requests a POST with fetch adapter and keep-alive option
159
101
  * @param url
160
102
  * @param params
161
103
  * @param headers
162
104
  */
163
- HttpClient.prototype.postWithKeepAlive = function (url, params, headers) {
164
- return __awaiter(this, void 0, void 0, function () {
165
- var _this = this;
166
- return __generator(this, function (_a) {
167
- switch (_a.label) {
168
- case 0: return [4 /*yield*/, this.execute(function () {
169
- return _this.axios.post(url, params, {
170
- headers: headers,
171
- adapter: 'fetch',
172
- fetchOptions: {
173
- keepalive: true,
174
- },
175
- });
176
- })];
177
- case 1: return [2 /*return*/, _a.sent()];
178
- }
179
- });
180
- });
181
- };
105
+ async postWithKeepAlive(url, params, headers) {
106
+ return await this.execute(() => this.axios.post(url, params, {
107
+ headers,
108
+ adapter: 'fetch',
109
+ fetchOptions: {
110
+ keepalive: true,
111
+ },
112
+ }));
113
+ }
182
114
  /**
183
115
  * Requests a POST FORM
184
116
  * @param url
185
117
  * @param params
186
118
  * @param headers
187
119
  */
188
- HttpClient.prototype.postForm = function (url, params, headers) {
189
- return __awaiter(this, void 0, void 0, function () {
190
- var _this = this;
191
- return __generator(this, function (_a) {
192
- switch (_a.label) {
193
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.postForm(url, params, { headers: headers }); })];
194
- case 1: return [2 /*return*/, _a.sent()];
195
- }
196
- });
197
- });
198
- };
120
+ async postForm(url, params, headers) {
121
+ return await this.execute(() => this.axios.postForm(url, params, { headers }));
122
+ }
199
123
  /**
200
124
  * Requests a POST with option to cancel
201
125
  * @param url
202
126
  * @param params
203
127
  * @param headers
204
128
  */
205
- HttpClient.prototype.postCancellable = function (url, params, headers) {
206
- var _this = this;
207
- var currentCancel = function () { };
208
- var requestCanceler = { cancel: function (message) { return currentCancel(message); } };
209
- var promise = this.execute(function () {
210
- var source = axios_1.default.CancelToken.source();
129
+ postCancellable(url, params, headers) {
130
+ let currentCancel = () => { };
131
+ const requestCanceler = { cancel: (message) => currentCancel(message) };
132
+ const promise = this.execute(() => {
133
+ const source = axios_1.default.CancelToken.source();
211
134
  currentCancel = source.cancel;
212
- return _this.axios.post(url, params, { headers: headers, cancelToken: source.token });
135
+ return this.axios.post(url, params, { headers, cancelToken: source.token });
213
136
  });
214
- return { promise: promise, requestCanceler: requestCanceler };
215
- };
137
+ return { promise, requestCanceler };
138
+ }
216
139
  /**
217
140
  * Requests PATCH
218
141
  * @param url
219
142
  * @param params
220
143
  * @param headers
221
144
  */
222
- HttpClient.prototype.patch = function (url, params, headers) {
223
- return __awaiter(this, void 0, void 0, function () {
224
- var _this = this;
225
- return __generator(this, function (_a) {
226
- switch (_a.label) {
227
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.patch(url, params, { headers: headers }); })];
228
- case 1: return [2 /*return*/, _a.sent()];
229
- }
230
- });
231
- });
232
- };
145
+ async patch(url, params, headers) {
146
+ return await this.execute(() => this.axios.patch(url, params, { headers }));
147
+ }
233
148
  /**
234
149
  * Requests a PUT
235
150
  * @param url
236
151
  * @param params
237
152
  * @param headers
238
153
  */
239
- HttpClient.prototype.put = function (url, params, headers) {
240
- return __awaiter(this, void 0, void 0, function () {
241
- var _this = this;
242
- return __generator(this, function (_a) {
243
- switch (_a.label) {
244
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.put(url, params, { headers: headers }); })];
245
- case 1: return [2 /*return*/, _a.sent()];
246
- }
247
- });
248
- });
249
- };
154
+ async put(url, params, headers) {
155
+ return await this.execute(() => this.axios.put(url, params, { headers }));
156
+ }
250
157
  /**
251
158
  * Requests a PUT FORM
252
159
  * @param url
253
160
  * @param params
254
161
  * @param headers
255
162
  */
256
- HttpClient.prototype.putForm = function (url, params, headers) {
257
- return __awaiter(this, void 0, void 0, function () {
258
- var _this = this;
259
- return __generator(this, function (_a) {
260
- switch (_a.label) {
261
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.putForm(url, params, { headers: headers }); })];
262
- case 1: return [2 /*return*/, _a.sent()];
263
- }
264
- });
265
- });
266
- };
163
+ async putForm(url, params, headers) {
164
+ return await this.execute(() => this.axios.putForm(url, params, { headers }));
165
+ }
267
166
  /**
268
167
  * Requests a DELETE
269
168
  * @param url
270
169
  * @param headers
271
170
  * @param params
272
171
  */
273
- HttpClient.prototype.delete = function (url, headers, params) {
274
- return __awaiter(this, void 0, void 0, function () {
275
- var _this = this;
276
- return __generator(this, function (_a) {
277
- switch (_a.label) {
278
- case 0: return [4 /*yield*/, this.execute(function () { return _this.axios.delete(url, { headers: headers, data: params }); })];
279
- case 1: return [2 /*return*/, _a.sent()];
280
- }
281
- });
282
- });
283
- };
172
+ async delete(url, headers, params) {
173
+ return await this.execute(() => this.axios.delete(url, { headers, data: params }));
174
+ }
284
175
  /**
285
176
  * Sets middlewares into the client for common behaviour managing
286
177
  * @private
287
178
  */
288
- HttpClient.prototype.initializeMiddleware = function () {
179
+ initializeMiddleware() {
289
180
  this.axios.interceptors.response.use(HttpClient.extractData, this.normalizeError.bind(this));
290
- };
181
+ }
291
182
  /**
292
183
  * Extracts the valuable data from the server's response
293
184
  * @param response
294
185
  * @private
295
186
  */
296
- HttpClient.extractData = function (response) {
187
+ static extractData(response) {
297
188
  return response.data;
298
- };
189
+ }
299
190
  /**
300
191
  * Converts the specific client error into a proprietary error for our apps
301
192
  * @param error
302
193
  * @private
303
194
  */
304
- HttpClient.prototype.normalizeError = function (error) {
305
- var _a, _b, _c, _d, _e, _f;
306
- var baseUrl = (_b = (_a = error.config) === null || _a === void 0 ? void 0 : _a.baseURL) !== null && _b !== void 0 ? _b : '';
307
- var url = (_d = (_c = error.config) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : '';
308
- var method = (_f = (_e = error.config) === null || _e === void 0 ? void 0 : _e.method) !== null && _f !== void 0 ? _f : '';
309
- var request = "".concat(method, " ").concat(baseUrl).concat(url);
195
+ normalizeError(error) {
196
+ const baseUrl = error.config?.baseURL ?? '';
197
+ const url = error.config?.url ?? '';
198
+ const method = error.config?.method ?? '';
199
+ const request = `${method} ${baseUrl}${url}`;
310
200
  if (error.response) {
311
201
  if (error.response.status === 401) {
312
202
  this.unauthorizedCallback();
@@ -316,8 +206,6 @@ var HttpClient = /** @class */ (function () {
316
206
  else {
317
207
  throw new errors_1.AxiosUnknownError(error.message, request, error);
318
208
  }
319
- };
320
- HttpClient.globalInterceptors = [];
321
- return HttpClient;
322
- }());
209
+ }
210
+ }
323
211
  exports.HttpClient = HttpClient;
@@ -1,80 +1,24 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
- if (ar || !(i in from)) {
52
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
- ar[i] = from[i];
54
- }
55
- }
56
- return to.concat(ar || Array.prototype.slice.call(from));
57
- };
58
2
  Object.defineProperty(exports, "__esModule", { value: true });
59
3
  exports.retryWithBackoff = void 0;
60
- var HTTP_STATUS_TOO_MANY_REQUESTS = 429;
61
- var wait = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); };
62
- var isErrorWithStatus = function (error) {
4
+ const HTTP_STATUS_TOO_MANY_REQUESTS = 429;
5
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
6
+ const isErrorWithStatus = (error) => {
63
7
  return typeof error === 'object' && error !== null;
64
8
  };
65
- var isRateLimitError = function (error) {
9
+ const isRateLimitError = (error) => {
66
10
  if (!isErrorWithStatus(error)) {
67
11
  return false;
68
12
  }
69
13
  return error.status === HTTP_STATUS_TOO_MANY_REQUESTS;
70
14
  };
71
- var extractRetryAfter = function (error) {
72
- var headers = error.headers;
73
- var resetHeader = headers === null || headers === void 0 ? void 0 : headers['retry-after'];
15
+ const extractRetryAfter = (error) => {
16
+ const headers = error.headers;
17
+ const resetHeader = headers?.['retry-after'];
74
18
  if (!resetHeader) {
75
19
  return undefined;
76
20
  }
77
- var resetValueInSeconds = Number.parseInt(resetHeader, 10);
21
+ const resetValueInSeconds = Number.parseInt(resetHeader, 10);
78
22
  if (Number.isNaN(resetValueInSeconds)) {
79
23
  return undefined;
80
24
  }
@@ -92,52 +36,34 @@ var extractRetryAfter = function (error) {
92
36
  * @returns The result of the function if successful
93
37
  * @throws The original error if it's not a rate limit error, if max retries exceeded, or if retry-after header is missing
94
38
  */
95
- var retryWithBackoff = function (fn_1) {
96
- var args_1 = [];
97
- for (var _i = 1; _i < arguments.length; _i++) {
98
- args_1[_i - 1] = arguments[_i];
99
- }
100
- return __awaiter(void 0, __spreadArray([fn_1], args_1, true), void 0, function (fn, options) {
101
- var opts, lastError, attempt, error_1, retryAfter, delay, err;
102
- if (options === void 0) { options = {}; }
103
- return __generator(this, function (_a) {
104
- switch (_a.label) {
105
- case 0:
106
- opts = __assign({ maxRetries: 5, maxRetryAfter: 70000, onRetry: function () { } }, options);
107
- attempt = 1;
108
- _a.label = 1;
109
- case 1:
110
- if (!(attempt <= opts.maxRetries)) return [3 /*break*/, 7];
111
- _a.label = 2;
112
- case 2:
113
- _a.trys.push([2, 4, , 6]);
114
- return [4 /*yield*/, fn()];
115
- case 3: return [2 /*return*/, _a.sent()];
116
- case 4:
117
- error_1 = _a.sent();
118
- if (!isRateLimitError(error_1)) {
119
- throw error_1;
120
- }
121
- retryAfter = extractRetryAfter(error_1);
122
- if (!retryAfter) {
123
- throw error_1;
124
- }
125
- delay = Math.min(retryAfter, opts.maxRetryAfter);
126
- opts.onRetry(attempt, delay);
127
- lastError = error_1;
128
- return [4 /*yield*/, wait(delay)];
129
- case 5:
130
- _a.sent();
131
- return [3 /*break*/, 6];
132
- case 6:
133
- attempt++;
134
- return [3 /*break*/, 1];
135
- case 7:
136
- err = lastError;
137
- err.message = "Max retries exceeded: ".concat(err.message);
138
- throw err;
39
+ const retryWithBackoff = async (fn, options = {}) => {
40
+ const opts = {
41
+ maxRetries: 5,
42
+ maxRetryAfter: 70_000,
43
+ onRetry: () => { },
44
+ ...options,
45
+ };
46
+ let lastError;
47
+ for (let attempt = 1; attempt <= opts.maxRetries; attempt++) {
48
+ try {
49
+ return await fn();
50
+ }
51
+ catch (error) {
52
+ if (!isRateLimitError(error)) {
53
+ throw error;
54
+ }
55
+ const retryAfter = extractRetryAfter(error);
56
+ if (!retryAfter) {
57
+ throw error;
139
58
  }
140
- });
141
- });
59
+ const delay = Math.min(retryAfter, opts.maxRetryAfter);
60
+ opts.onRetry(attempt, delay);
61
+ lastError = error;
62
+ await wait(delay);
63
+ }
64
+ }
65
+ const err = lastError;
66
+ err.message = `Max retries exceeded: ${err.message}`;
67
+ throw err;
142
68
  };
143
69
  exports.retryWithBackoff = retryWithBackoff;