This commit is contained in:
g
2025-02-07 04:43:47 +00:00
parent d3e19c7e09
commit 6c4f3f0866
94 changed files with 11777 additions and 0 deletions

36
lib/storeHelper.ts Normal file
View File

@@ -0,0 +1,36 @@
export const apiRequest = async <T = any>(endpoint: string, method: string = "GET", body?: T | null) => {
try {
const authToken = document.cookie.split("; ").find(row => row.startsWith("Authorization="))?.split("=")[1];
if (!authToken) throw new Error("No authentication token found");
const options: RequestInit = {
method,
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
};
if (body) {
options.body = JSON.stringify(body);
}
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`, options);
if (!res.ok) {
throw new Error(`Failed to ${method} ${endpoint}: ${res.statusText}`);
}
return await res.json();
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`Error in API request: ${error.message}`);
throw new Error(error.message);
}
console.error("An unknown error occurred", error);
throw new Error("An unknown error occurred");
}
};