(path: string, method: 'GET' | 'POST' | 'DELETE')
| 61 | } |
| 62 | |
| 63 | async function request<T>(path: string, method: 'GET' | 'POST' | 'DELETE'): Promise<T> { |
| 64 | const headers: Record<string, string> = { accept: 'application/json' }; |
| 65 | const token = authToken(); |
| 66 | if (token !== null && token.length > 0) { |
| 67 | headers['authorization'] = `Bearer ${token}`; |
| 68 | } |
| 69 | const res = await fetch(path, { method, headers }); |
| 70 | if (!res.ok) { |
| 71 | let err: ApiError | null = null; |
| 72 | try { |
| 73 | err = (await res.json()) as ApiError; |
| 74 | } catch { |
| 75 | /* ignore */ |
| 76 | } |
| 77 | throw new Error(err?.error ?? `HTTP ${res.status} ${res.statusText}`); |
| 78 | } |
| 79 | return (await res.json()) as T; |
| 80 | } |
| 81 | |
| 82 | function get<T>(path: string): Promise<T> { |
| 83 | return request<T>(path, 'GET'); |
no test coverage detected