This commit is contained in:
g
2025-02-07 21:33:13 +00:00
parent 8900bbcc76
commit 891f57d729
50 changed files with 165 additions and 444 deletions

View File

@@ -1,31 +1,13 @@
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' })
};
/**
* Client-side fetch function for API requests.
*/
export async function fetchData(url: string, options: RequestInit = {}): Promise<any> {
try {
const res = await fetch(url, options);
if (!res.ok) throw new Error(`Request failed: ${res.statusText}`);
return res.json();
} catch (error) {
console.error(`Fetch error at ${url}:`, error);
throw error;
}
}