Refactor
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user