Files
frangipane-client/src/api/client.ts

27 lines
559 B
TypeScript

import { initAuth } from '../stores/auth.ts'
import { API } from '../main.ts'
export async function apiFetch<T>(
path: string,
options: RequestInit = {}
): Promise<T> {
const auth = await initAuth()
const res = await fetch(`${API}${path}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(auth.token ? { Authorization: `Bearer ${auth.token}` } : {}),
...options.headers,
},
})
if (!res.ok) {
const text = await res.text()
throw new Error(text || res.statusText)
}
return res.json()
}