public
anord
read
Ledger
Why work hard when you can work easier?
Languages
Repository composition by tracked source files.
TypeScript
86%
CSS
10%
SQL
3%
Shell
1%
HTML
0%
Trace
apps/web/src/lib/api.ts
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
const API_BASE = import.meta.env.VITE_API_URL ?? "";
3
async function request<T>(path: string, init?: RequestInit): Promise<T> {
4
const headers = new Headers(init?.headers);
5
if (!(init?.body instanceof FormData) && !headers.has("Content-Type")) {
6
headers.set("Content-Type", "application/json");
7
}
9
const response = await fetch(`${API_BASE}${path}`, {
10
...init,
11
credentials: "include",
12
headers
13
});
15
if (!response.ok) {
16
const body = await response.json().catch(() => ({ error: "Request failed" }));
17
throw new Error(body.error ?? "Request failed");
18
}
20
if (response.status === 204) {
21
return undefined as T;
22
}
24
return response.json() as Promise<T>;
25
}
27
export const api = {
28
get: <T>(path: string) => request<T>(path),
29
post: <T>(path: string, body?: unknown) =>
30
request<T>(path, {
31
method: "POST",
32
body: body instanceof FormData ? body : body ? JSON.stringify(body) : undefined
33
}),
34
put: <T>(path: string, body: unknown) =>
35
request<T>(path, {
36
method: "PUT",
37
body: JSON.stringify(body)
38
}),
39
delete: <T>(path: string) =>
40
request<T>(path, {
41
method: "DELETE"
42
})
43
};