(path: string, options: RequestOptions = {})
| 41 | } |
| 42 | |
| 43 | export async function httpRequest<T>(path: string, options: RequestOptions = {}): Promise<T> { |
| 44 | const { method = "GET", body, headers, signal } = options; |
| 45 | const response = await fetch(buildApiUrl(path), { |
| 46 | method, |
| 47 | credentials: "include", |
| 48 | headers: { |
| 49 | ...DEFAULT_HEADERS, |
| 50 | ...headers, |
| 51 | }, |
| 52 | body: body === undefined ? undefined : JSON.stringify(body), |
| 53 | signal, |
| 54 | }); |
| 55 | |
| 56 | if (!response.ok) { |
| 57 | let payload: ApiErrorPayload | null = null; |
| 58 | try { |
| 59 | payload = await parseResponse<ApiErrorPayload>(response); |
| 60 | } catch { |
| 61 | payload = null; |
| 62 | } |
| 63 | throw new ApiHttpError(response.status, payload, `Request failed: ${method} ${path}`); |
| 64 | } |
| 65 | |
| 66 | return parseResponse<T>(response); |
| 67 | } |
no test coverage detected