(path, { retry = true } = {})
| 40 | * retry: D1 sheds queued queries under load and the second ask usually lands, |
| 41 | * while a real failure still reaches the panel a moment later. */ |
| 42 | export async function api(path, { retry = true } = {}) { |
| 43 | let response; |
| 44 | try { |
| 45 | response = await fetch(path, { headers: { accept: 'application/json' } }); |
| 46 | } catch (err) { |
| 47 | if (!retry) throw err; |
| 48 | await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS)); |
| 49 | return api(path, { retry: false }); |
| 50 | } |
| 51 | if (response.status === 401) { |
| 52 | window.location.href = `/login?next=${encodeURIComponent(window.location.pathname)}`; |
| 53 | throw new Error('session expired'); |
| 54 | } |
| 55 | if (response.status >= 500 && retry) { |
| 56 | await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS)); |
| 57 | return api(path, { retry: false }); |
| 58 | } |
| 59 | if (!response.ok) { |
| 60 | const detail = await response.json().catch(() => null); |
| 61 | throw new Error(detail?.error ?? `responded ${response.status}`); |
| 62 | } |
| 63 | return response.json(); |
| 64 | } |
| 65 | |
| 66 | // --------------------------------------------------------------------------- |
| 67 | // Days |
no test coverage detected