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

43
lib/client-utils.ts Normal file
View File

@@ -0,0 +1,43 @@
// ✅ Client-Safe API Helper
export const fetchWithAuthClient = async <T = any>(
endpoint: string,
method: string = "GET",
body?: unknown
): Promise<T> => {
try {
// ✅ Get auth token from cookies (Client-Safe)
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 fetch: ${endpoint} (${res.status})`);
}
return await res.json();
} catch (error) {
console.error("API request error:", error);
throw error;
}
};

18
lib/fetchData.ts Normal file
View File

@@ -0,0 +1,18 @@
export const fetchData = async (url: string, authToken: string) => {
try {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
credentials: "include",
});
if (!response.ok) throw new Error("Failed to fetch data");
return await response.json();
} catch (error) {
console.error("Error fetching data:", error);
throw error;
}
};

62
lib/productData.ts Normal file
View File

@@ -0,0 +1,62 @@
export const fetchProductData = async (url: string, authToken: string) => {
try {
const response = await fetch(url, {
headers: { Authorization: `Bearer ${authToken}` },
credentials: "include",
});
if (!response.ok) {
throw new Error("Failed to fetch product data");
}
return await response.json();
} catch (error) {
console.error("Error fetching product data:", error);
throw error;
}
};
export const saveProductData = async (
url: string,
data: any,
authToken: string,
method: "POST" | "PUT" = "POST"
) => {
try {
const response = await fetch(url, {
method,
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error("Failed to save product data");
}
return await response.json();
} catch (error) {
console.error("Error saving product data:", error);
throw error;
}
};
export const deleteProductData = async (url: string, authToken: string) => {
try {
const response = await fetch(url, {
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
});
if (!response.ok) {
throw new Error("Failed to delete product data");
}
return await response.json();
} catch (error) {
console.error("Error deleting product data:", error);
throw error;
}
};

47
lib/server-utils.ts Normal file
View File

@@ -0,0 +1,47 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
/**
* Fetch API with Authorization (for server-side usage only).
* @param endpoint - The API endpoint.
* @param options - Fetch options (optional).
* @returns Parsed JSON response.
*/
export async function fetchWithAuthorization<T = unknown>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
// Access the Authorization cookie securely
const cookieStore = await cookies();
const authToken = cookieStore.get("Authorization")?.value;
console.log("authToken", authToken);
if (!authToken) {
redirect("/login");
}
const config: RequestInit = {
...options,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
...(options.headers || {}),
},
cache: "no-store",
};
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`, config);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
throw new Error(`Failed to fetch ${endpoint}: ${res.statusText}`);
}
const data = await res.json() as T;
return data as Promise<T>;
}

107
lib/shippingHelper.ts Normal file
View File

@@ -0,0 +1,107 @@
export const fetchShippingMethods = async (authToken: string) => {
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/shipping-options`,
{
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
}
);
if (!res.ok) throw new Error("Failed to fetch shipping options");
return await res.json();
} catch (error) {
console.error("Error loading shipping options:", error);
throw error;
}
};
interface ShippingMethod {
name: string;
price: number;
_id?: string;
}
export const addShippingMethod = async (
authToken: string,
newShipping: Omit<ShippingMethod, "_id">
): Promise<ShippingMethod[]> => {
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/shipping-options`,
{
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify(newShipping),
}
);
if (!res.ok) {
const errorData = await res.json();
throw new Error(errorData.message || "Failed to add shipping method");
}
return await res.json();
} catch (error) {
console.error("Error adding shipping method:", error);
throw new Error(
error instanceof Error
? error.message
: "An unexpected error occurred while adding shipping method"
);
}
};
export const deleteShippingMethod = async (authToken: string, id: string) => {
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/shipping-options/${id}`,
{
method: "DELETE",
headers: { Authorization: `Bearer ${authToken}` },
credentials: "include",
}
);
if (!res.ok) throw new Error("Failed to delete shipping method");
// Since there is no content, just return success status.
return { success: res.status === 204 };
} catch (error) {
console.error("Error deleting shipping method:", error);
throw error;
}
};
export const updateShippingMethod = async (
authToken: string,
id: string,
updatedShipping: any
) => {
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/shipping-options/${id}`,
{
method: "PUT",
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify(updatedShipping),
}
);
if (!res.ok) throw new Error("Failed to update shipping method");
return await res.json();
} catch (error) {
console.error("Error updating shipping method:", error);
throw error;
}
};

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");
}
};

14
lib/utils.ts Normal file
View File

@@ -0,0 +1,14 @@
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
/**
* Utility function for merging Tailwind CSS class names with conditional logic.
* @param inputs - Class values to merge.
* @returns A properly merged class name string.
*/
export function cn(...inputs: ClassValue[]): string {
return twMerge(clsx(inputs));
}