| 9 | const jsonHeaders = { "Content-Type": "application/json" }; |
| 10 | |
| 11 | async function request<T>(path: string, init?: RequestInit): Promise<T> { |
| 12 | const res = await fetch(path, { |
| 13 | credentials: "include", |
| 14 | ...init, |
| 15 | headers: { |
| 16 | ...(init?.body ? jsonHeaders : {}), |
| 17 | ...(init?.headers || {}), |
| 18 | }, |
| 19 | }); |
| 20 | if (!res.ok) { |
| 21 | const text = await res.text(); |
| 22 | throw new Error(text || `Request failed: ${res.status}`); |
| 23 | } |
| 24 | return (await res.json()) as T; |
| 25 | } |
| 26 | |
| 27 | export const api = { |
| 28 | config: () => request<AppConfig>("/api/config"), |