import { clientFetch } from '../api-client'; /** * Shipping service - Handles shipping options * Replaces the old shippingHelper.ts */ export interface ShippingOption { _id?: string; name: string; price: number; createdAt?: string; updatedAt?: string; } export interface ShippingOptionsResponse { success: boolean; data: ShippingOption[]; } // Get all shipping options export const getShippingOptions = async (authToken?: string): Promise => { console.log('Fetching shipping options'); const options: RequestInit = { method: 'GET', headers: { 'Content-Type': 'application/json', }, }; if (authToken) { options.headers = { ...options.headers, 'Authorization': `Bearer ${authToken}`, }; } const response = await clientFetch('/api/shipping', options); return response.data || []; }; // Get a single shipping option export const getShippingOption = async (id: string, authToken?: string): Promise => { const options: RequestInit = { method: 'GET', headers: { 'Content-Type': 'application/json', }, }; if (authToken) { options.headers = { ...options.headers, 'Authorization': `Bearer ${authToken}`, }; } const response = await clientFetch<{success: boolean, data: ShippingOption}>(`/api/shipping/${id}`, options); return response.data; }; // Create a new shipping option export const createShippingOption = async (data: ShippingOption, authToken?: string) => { const options: RequestInit = { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }; if (authToken) { options.headers = { ...options.headers, 'Authorization': `Bearer ${authToken}`, }; } return clientFetch<{success: boolean, data: ShippingOption}>('/api/shipping', options); }; // Update a shipping option export const updateShippingOption = async (id: string, data: ShippingOption, authToken?: string) => { const options: RequestInit = { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }; if (authToken) { options.headers = { ...options.headers, 'Authorization': `Bearer ${authToken}`, }; } return clientFetch<{success: boolean, data: ShippingOption}>(`/api/shipping/${id}`, options); }; // Delete a shipping option export const deleteShippingOption = async (id: string, authToken?: string) => { const options: RequestInit = { method: 'DELETE', headers: { 'Content-Type': 'application/json', }, }; if (authToken) { options.headers = { ...options.headers, 'Authorization': `Bearer ${authToken}`, }; } return clientFetch<{success: boolean}>(`/api/shipping/${id}`, options); }; // Compatibility with old shippingHelper functions export const fetchShippingMethods = getShippingOptions; export const addShippingMethod = createShippingOption; export const updateShippingMethod = updateShippingOption; export const deleteShippingMethod = deleteShippingOption; // Types for backward compatibility export type ShippingMethod = ShippingOption; export type ShippingData = ShippingOption;