Spaces:
Build error

primestake / frontend /lib /api /client.ts
PrimeStake Deployer
Fix betting features: add auth headers, Redis checks, and decimal handling
0bd1a7a
Raw History Blame Contribute Delete
1.17 kB
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" })
};