| 37 | } |
| 38 | |
| 39 | async function makeRequestWithRetry<T = any>( |
| 40 | url: string, |
| 41 | options: RequestConfig = {}, |
| 42 | currentAttempt = 1 |
| 43 | ): Promise<HttpResponse<T>> { |
| 44 | const retryConfig = { |
| 45 | ...DEFAULT_RETRY_CONFIG, |
| 46 | ...options.retry |
| 47 | }; |
| 48 | |
| 49 | try { |
| 50 | return await makeRequest<T>(url, options); |
| 51 | } catch (error) { |
| 52 | const shouldRetry = error instanceof Object && |
| 53 | 'status' in error && |
| 54 | retryConfig.retryStatusCodes?.includes(error.status as number) && |
| 55 | currentAttempt < retryConfig.maxRetries; |
| 56 | |
| 57 | if (!shouldRetry) { |
| 58 | throw error; |
| 59 | } |
| 60 | |
| 61 | const delayMs = retryConfig.exponentialBackoff |
| 62 | ? retryConfig.delayMs * Math.pow(2, currentAttempt - 1) |
| 63 | : retryConfig.delayMs; |
| 64 | |
| 65 | await sleep(delayMs); |
| 66 | return makeRequestWithRetry<T>(url, options, currentAttempt + 1); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function makeRequest<T = any>(url: string, options: RequestConfig = {}): Promise<HttpResponse<T>> { |
| 71 | return new Promise((resolve, reject) => { |