(response: Response, originalRequest?: { endpoint: string; options: RequestOptions })
| 58 | } |
| 59 | |
| 60 | private async handleResponse<T>(response: Response, originalRequest?: { endpoint: string; options: RequestOptions }): Promise<T> { |
| 61 | if (!response.ok) { |
| 62 | if (response.status === 401 && originalRequest && !originalRequest.options.skipAuth) { |
| 63 | const retryCount = originalRequest.options._retryCount || 0; |
| 64 | |
| 65 | // Prevent infinite loops - only retry once |
| 66 | if (retryCount >= 1) { |
| 67 | throw new Error('Session expired. Please log in again.'); |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | // Attempt to refresh the token |
| 72 | await this.refreshToken(); |
| 73 | |
| 74 | // Retry the original request with incremented retry count |
| 75 | const retryOptions = { |
| 76 | ...originalRequest.options, |
| 77 | _retryCount: retryCount + 1 |
| 78 | }; |
| 79 | return this.request<T>(originalRequest.endpoint, retryOptions); |
| 80 | } catch (refreshError) { |
| 81 | throw new Error('Session expired. Please log in again.'); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | let errorMessage = `Request failed with status ${response.status}`; |
| 86 | try { |
| 87 | const errorData = await response.json(); |
| 88 | errorMessage = errorData.message || errorMessage; |
| 89 | } catch { |
| 90 | // Ignore JSON parse error |
| 91 | } |
| 92 | |
| 93 | throw new Error(errorMessage); |
| 94 | } |
| 95 | |
| 96 | // Handle empty responses |
| 97 | if (response.status === 204 || response.headers.get('content-length') === '0') { |
| 98 | return {} as T; |
| 99 | } |
| 100 | |
| 101 | return response.json(); |
| 102 | } |
| 103 | |
| 104 | async request<T>( |
| 105 | endpoint: string, |
nothing calls this directly
no test coverage detected