mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-15 10:30:37 +09:00
23 lines
752 B
TypeScript
23 lines
752 B
TypeScript
|
|
const BASE = "/api";
|
||
|
|
|
||
|
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||
|
|
const res = await fetch(`${BASE}${path}`, {
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
...init,
|
||
|
|
});
|
||
|
|
if (!res.ok) {
|
||
|
|
const body = await res.json().catch(() => null);
|
||
|
|
throw new Error(body?.error ?? `Request failed: ${res.status}`);
|
||
|
|
}
|
||
|
|
return res.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
export const api = {
|
||
|
|
get: <T>(path: string) => request<T>(path),
|
||
|
|
post: <T>(path: string, body: unknown) =>
|
||
|
|
request<T>(path, { method: "POST", body: JSON.stringify(body) }),
|
||
|
|
patch: <T>(path: string, body: unknown) =>
|
||
|
|
request<T>(path, { method: "PATCH", body: JSON.stringify(body) }),
|
||
|
|
delete: <T>(path: string) => request<T>(path, { method: "DELETE" }),
|
||
|
|
};
|