slight cleanup
This commit is contained in:
@@ -1,43 +1,46 @@
|
||||
// ✅ Client-Safe API Helper
|
||||
export const fetchWithAuthClient = async <T = any>(
|
||||
/**
|
||||
* Client-side API utilities with authentication handling
|
||||
*/
|
||||
|
||||
const getAuthToken = (): string | null => {
|
||||
const token = document.cookie
|
||||
.split('; ')
|
||||
.find(row => row.startsWith('Authorization='))
|
||||
?.split('=')[1];
|
||||
|
||||
return token || null; // Return null instead of throwing an error
|
||||
};
|
||||
|
||||
export const fetchClient = async <T = unknown>(
|
||||
endpoint: string,
|
||||
method: string = "GET",
|
||||
body?: unknown
|
||||
options: RequestInit = {}
|
||||
): 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 authToken = getAuthToken();
|
||||
if (!authToken) {
|
||||
console.warn("No authentication token found. Redirecting to login...");
|
||||
window.location.href = "/login";
|
||||
return Promise.reject("Unauthorized");
|
||||
}
|
||||
|
||||
const res = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`,
|
||||
options
|
||||
);
|
||||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
...options.headers,
|
||||
},
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch: ${endpoint} (${res.status})`);
|
||||
const errorData = await res.json();
|
||||
throw new Error(errorData.message || `Request failed: ${res.statusText}`);
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
return res.json();
|
||||
} catch (error) {
|
||||
console.error("API request error:", error);
|
||||
console.error(`API request to ${endpoint} failed:`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
};
|
||||
31
lib/data-service.ts
Normal file
31
lib/data-service.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { fetchClient } from './client-utils';
|
||||
import type { Product, ShippingMethod, ApiResponse } from './types';
|
||||
|
||||
export const ProductService = {
|
||||
getAll: async (): Promise<ApiResponse<Product[]>> =>
|
||||
fetchClient('/products'),
|
||||
|
||||
create: async (product: Omit<Product, '_id'>): Promise<ApiResponse<Product>> =>
|
||||
fetchClient('/products', { method: 'POST', body: JSON.stringify(product) }),
|
||||
|
||||
update: async (id: string, product: Partial<Product>): Promise<ApiResponse<Product>> =>
|
||||
fetchClient(`/products/${id}`, { method: 'PUT', body: JSON.stringify(product) }),
|
||||
|
||||
delete: async (id: string): Promise<ApiResponse<void>> =>
|
||||
fetchClient(`/products/${id}`, { method: 'DELETE' })
|
||||
};
|
||||
|
||||
// Shipping Operations
|
||||
export const ShippingService = {
|
||||
getAll: async (): Promise<ApiResponse<ShippingMethod[]>> =>
|
||||
fetchClient('/shipping-options'),
|
||||
|
||||
create: async (method: Omit<ShippingMethod, '_id'>): Promise<ApiResponse<ShippingMethod>> =>
|
||||
fetchClient('/shipping-options', { method: 'POST', body: JSON.stringify(method) }),
|
||||
|
||||
update: async (id: string, method: Partial<ShippingMethod>): Promise<ApiResponse<ShippingMethod>> =>
|
||||
fetchClient(`/shipping-options/${id}`, { method: 'PUT', body: JSON.stringify(method) }),
|
||||
|
||||
delete: async (id: string): Promise<ApiResponse<void>> =>
|
||||
fetchClient(`/shipping-options/${id}`, { method: 'DELETE' })
|
||||
};
|
||||
@@ -1,47 +1,35 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
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.
|
||||
* Server-side fetch wrapper with auth handling
|
||||
*/
|
||||
export async function fetchWithAuthorization<T = unknown>(
|
||||
export const fetchServer = async <T = unknown>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> {
|
||||
// Access the Authorization cookie securely
|
||||
const cookieStore = await cookies();
|
||||
const authToken = cookieStore.get("Authorization")?.value;
|
||||
): Promise<T> => {
|
||||
const cookieStore = cookies();
|
||||
const authToken = await cookieStore.get('Authorization')?.value;
|
||||
|
||||
console.log("authToken", authToken);
|
||||
if (!authToken) redirect('/login');
|
||||
|
||||
if (!authToken) {
|
||||
redirect("/login");
|
||||
try {
|
||||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
...options.headers,
|
||||
},
|
||||
cache: 'no-store',
|
||||
});
|
||||
|
||||
if (res.status === 401) redirect('/login');
|
||||
if (!res.ok) throw new Error(`Request failed: ${res.statusText}`);
|
||||
|
||||
return res.json();
|
||||
} catch (error) {
|
||||
console.error(`Server request to ${endpoint} failed:`, error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
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>;
|
||||
}
|
||||
};
|
||||
17
lib/types.ts
17
lib/types.ts
@@ -10,3 +10,20 @@ export interface ShippingData {
|
||||
name: string;
|
||||
price: number;
|
||||
}
|
||||
|
||||
export interface Product {
|
||||
_id?: string;
|
||||
name: string;
|
||||
price: number;
|
||||
description?: string;
|
||||
sku?: string;
|
||||
stock: number;
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
|
||||
export type ApiResponse<T> = {
|
||||
data?: T;
|
||||
error?: string;
|
||||
total?: number;
|
||||
};
|
||||
Reference in New Issue
Block a user