Spaces:
Build error
Build error
File size: 1,170 Bytes
0bd1a7a dae886b 0bd1a7a dae886b 0bd1a7a dae886b 8b9eb0b dae886b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import { useAuthStore } from "../store/useAuthStore";
const baseUrl =
process.env.NEXT_PUBLIC_API_BASE_URL || "http://localhost:4000/api";
async function request(input: string, init?: RequestInit) {
const token = useAuthStore.getState().token;
const res = await fetch(`${baseUrl}${input}`, {
...init,
headers: {
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}),
...(init?.headers || {})
}
});
if (!res.ok) {
const text = await res.text();
throw new Error(text || "Request failed");
}
return res.json();
}
export const apiClient = {
get: (url: string, init?: RequestInit) =>
request(url, { ...init, method: "GET" }),
post: (url: string, body?: unknown, init?: RequestInit) =>
request(url, {
...init,
method: "POST",
body: body ? JSON.stringify(body) : undefined
}),
put: (url: string, body?: unknown, init?: RequestInit) =>
request(url, {
...init,
method: "PUT",
body: body ? JSON.stringify(body) : undefined
}),
delete: (url: string, init?: RequestInit) =>
request(url, { ...init, method: "DELETE" })
};
|