| 6 | } from '@/types/generated' |
| 7 | |
| 8 | async function authFetch<T>(path: string, init?: RequestInit): Promise<T> { |
| 9 | const response = await fetch(`/api/v1${path}`, { |
| 10 | ...init, |
| 11 | headers: { |
| 12 | 'content-type': 'application/json', |
| 13 | ...(init?.headers as Record<string, string> | undefined), |
| 14 | }, |
| 15 | }) |
| 16 | if (!response.ok) { |
| 17 | const text = await response.text() |
| 18 | let message = response.statusText |
| 19 | try { |
| 20 | const json = JSON.parse(text) as { message?: string } |
| 21 | if (json.message) message = json.message |
| 22 | } catch { |
| 23 | if (text) message = text |
| 24 | } |
| 25 | throw new Error(message) |
| 26 | } |
| 27 | const text = await response.text() |
| 28 | if (response.status === 204 || text.length === 0) return undefined as T |
| 29 | return JSON.parse(text) as T |
| 30 | } |
| 31 | |
| 32 | export function register(body: RegisterRequest): Promise<AuthResponse> { |
| 33 | return authFetch<AuthResponse>('/auth/register', { |