| 41 | } |
| 42 | |
| 43 | private async post<ReqBody, ResBody>( |
| 44 | url: string, |
| 45 | data: ReqBody, |
| 46 | options: FetchOptions, |
| 47 | attempt: number, |
| 48 | ): Promise<ResBody | null> { |
| 49 | try { |
| 50 | const response = await fetch(url, { |
| 51 | method: 'POST', |
| 52 | headers: await this.resolveHeaders(), |
| 53 | body: data ? JSON.stringify(data ?? {}) : undefined, |
| 54 | keepalive: true, |
| 55 | ...options, |
| 56 | }); |
| 57 | |
| 58 | if (response.status === 401) return null; |
| 59 | |
| 60 | if (response.status !== 200 && response.status !== 202) { |
| 61 | throw new Error(`HTTP error! status: ${response.status}`); |
| 62 | } |
| 63 | |
| 64 | const responseText = await response.text(); |
| 65 | return responseText ? JSON.parse(responseText) : null; |
| 66 | } catch (error) { |
| 67 | if (attempt < this.maxRetries) { |
| 68 | const delay = this.initialRetryDelay * 2 ** attempt; |
| 69 | await new Promise((resolve) => setTimeout(resolve, delay)); |
| 70 | return this.post<ReqBody, ResBody>(url, data, options, attempt + 1); |
| 71 | } |
| 72 | console.error('Max retries reached:', error); |
| 73 | return null; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | async fetch<ReqBody, ResBody>( |
| 78 | path: string, |