Files
AnimeTV-Frontend/shared/helper/backendFetch.ts
Rafi Arrafif 21a099b77f feat: fetch helper connection to main backend
create a helper to facilitate data requests to and from the main backend.
2026-01-07 15:13:56 +07:00

27 lines
639 B
TypeScript

interface BackendResponse<T = unknown> {
success: boolean;
message: string;
data?: T;
error?: unknown;
}
export const backendFetch = async (path: string, options: RequestInit = {}) => {
const res = await fetch(`${process.env.BACKEND_ENDPOINT}/${path}`, {
...options,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.BACKEND_API_KEY}`,
...options.headers,
},
cache: "default",
});
const resJson = (await res.json()) as BackendResponse;
if (!res.ok || !resJson.success) {
throw new Error(`Elysia error: ${resJson.error}`);
}
return resJson;
};