24 lines
552 B
TypeScript
24 lines
552 B
TypeScript
|
|
const API_BASE = '/api';
|
||
|
|
|
||
|
|
export class ApiError extends Error {
|
||
|
|
constructor(
|
||
|
|
public status: number,
|
||
|
|
public body: string,
|
||
|
|
) {
|
||
|
|
super(`API ${status}: ${body}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
||
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
||
|
|
credentials: 'include',
|
||
|
|
...init,
|
||
|
|
headers: {
|
||
|
|
'content-type': 'application/json',
|
||
|
|
...(init?.headers ?? {}),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new ApiError(res.status, await res.text());
|
||
|
|
return (await res.json()) as T;
|
||
|
|
}
|