(path: string, body?: unknown)
| 102 | * Bearer header or trigger refresh-retry — these endpoints establish the session. |
| 103 | */ |
| 104 | export async function authRequest<T>(path: string, body?: unknown): Promise<T> { |
| 105 | const res = await fetch(apiUrl(path), { |
| 106 | method: "POST", |
| 107 | credentials: "include", |
| 108 | headers: { "Content-Type": "application/json" }, |
| 109 | body: body === undefined ? undefined : JSON.stringify(body), |
| 110 | }); |
| 111 | const text = await res.text(); |
| 112 | const parsed = text ? JSON.parse(text) : undefined; |
| 113 | if (!res.ok) { |
| 114 | const message = |
| 115 | (parsed && |
| 116 | typeof parsed === "object" && |
| 117 | "error" in parsed && |
| 118 | parsed.error) || |
| 119 | `HTTP ${res.status}`; |
| 120 | throw new Error(String(message)); |
| 121 | } |
| 122 | return parsed as T; |
| 123 | } |
| 124 | |
| 125 | /** Fetch raw text (used for the docs markdown endpoint). Returns null on 404. */ |
| 126 | export async function apiGetText(path: string): Promise<string | null> { |
no test coverage detected