132 lines
3.1 KiB
TypeScript
132 lines
3.1 KiB
TypeScript
import { fetchData } from '@/lib/data-service';
|
|
import { normalizeApiUrl } from './api-utils';
|
|
|
|
/**
|
|
* Interface for shipping method data
|
|
*/
|
|
interface ShippingMethod {
|
|
name: string;
|
|
price: number;
|
|
_id?: string;
|
|
}
|
|
|
|
/**
|
|
* Fetches all shipping methods for the current store
|
|
*/
|
|
export const fetchShippingMethods = async (authToken: string) => {
|
|
try {
|
|
const res = await fetchData(
|
|
`/api/shipping-options`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
}
|
|
);
|
|
|
|
console.log("Shipping Methods Response:", res);
|
|
|
|
if (!res) throw new Error("Failed to fetch shipping options");
|
|
return res
|
|
} catch (error) {
|
|
console.error("Error loading shipping options:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Adds a new shipping method
|
|
*/
|
|
export const addShippingMethod = async (
|
|
authToken: string,
|
|
newShipping: Omit<ShippingMethod, "_id">
|
|
): Promise<ShippingMethod[]> => {
|
|
try {
|
|
const res = await fetchData(
|
|
`/api/shipping-options`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
body: JSON.stringify(newShipping),
|
|
}
|
|
);
|
|
|
|
// If fetchData returns directly (not a Response object), just return it
|
|
if (!res.ok && !res.status) {
|
|
return res;
|
|
}
|
|
|
|
// Handle if it's a Response object
|
|
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"
|
|
);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Deletes a shipping method by ID
|
|
*/
|
|
export const deleteShippingMethod = async (authToken: string, id: string) => {
|
|
try {
|
|
const res = await fetchData(
|
|
`/api/shipping-options/${id}`,
|
|
{
|
|
method: "DELETE",
|
|
headers: { Authorization: `Bearer ${authToken}` },
|
|
credentials: "include",
|
|
}
|
|
);
|
|
|
|
if (!res.ok) throw new Error("Failed to delete shipping method");
|
|
return { success: res.status === 204 };
|
|
} catch (error) {
|
|
console.error("Error deleting shipping method:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Updates an existing shipping method
|
|
*/
|
|
export const updateShippingMethod = async (
|
|
authToken: string,
|
|
id: string,
|
|
updatedShipping: Partial<ShippingMethod>
|
|
) => {
|
|
try {
|
|
const res = await fetchData(
|
|
`/api/shipping-options/${id}`,
|
|
{
|
|
method: "PUT",
|
|
headers: {
|
|
Authorization: `Bearer ${authToken}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
credentials: "include",
|
|
body: JSON.stringify(updatedShipping),
|
|
}
|
|
);
|
|
|
|
if (!res) throw new Error("Failed to update shipping method");
|
|
return res;
|
|
} catch (error) {
|
|
console.error("Error updating shipping method:", error);
|
|
throw error;
|
|
}
|
|
}; |