From 21a099b77f24468c19b39e5958ef9bdc26b25242 Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Wed, 7 Jan 2026 15:13:56 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20fetch=20helper=20connection?= =?UTF-8?q?=20to=20main=20backend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit create a helper to facilitate data requests to and from the main backend. --- .gitignore | 1 + shared/helper/backendFetch.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 shared/helper/backendFetch.ts diff --git a/.gitignore b/.gitignore index 5ef6a52..6548165 100644 --- a/.gitignore +++ b/.gitignore @@ -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* diff --git a/shared/helper/backendFetch.ts b/shared/helper/backendFetch.ts new file mode 100644 index 0000000..6de7066 --- /dev/null +++ b/shared/helper/backendFetch.ts @@ -0,0 +1,26 @@ +interface BackendResponse { + 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; +};