feat: fetch helper connection to main backend

create a helper to facilitate data requests to and from the main backend.
This commit is contained in:
2026-01-07 15:13:56 +07:00
parent cb0dfdda2e
commit 21a099b77f
2 changed files with 27 additions and 0 deletions

1
.gitignore vendored
View File

@ -29,6 +29,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
/app/debug
# env files (can opt-in for committing if needed)
.env*

View File

@ -0,0 +1,26 @@
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;
};