✨ 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:
1
.gitignore
vendored
1
.gitignore
vendored
@ -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*
|
||||
|
||||
26
shared/helper/backendFetch.ts
Normal file
26
shared/helper/backendFetch.ts
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user