change fetch to axios

This commit is contained in:
hamid zarghami
2026-01-24 10:05:57 +03:30
parent 82fddde315
commit 999c64f3dc
+11 -4
View File
@@ -1,23 +1,28 @@
import { API_BASE_URL } from "@/config/const"; import { API_BASE_URL } from "@/config/const";
import { AboutResponse, Restaurant } from "../(Main)/about/types/Types"; import { AboutResponse, Restaurant } from "../(Main)/about/types/Types";
import axios from "axios";
export async function getRestaurant(slug: string): Promise<Restaurant> { export async function getRestaurant(slug: string): Promise<Restaurant> {
try { try {
const response = await fetch(`${API_BASE_URL}/public/restaurants/${slug}`, { const response = await axios(`${API_BASE_URL}/public/restaurants/${slug}`, {
cache: "no-store", // cache: "no-store",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"X-SLUG": slug,
}, },
}); });
if (!response.ok) { console.log("response", response);
if (!response.data) {
if (response.status === 404) { if (response.status === 404) {
throw new Error("RESTAURANT_NOT_FOUND"); throw new Error("RESTAURANT_NOT_FOUND");
} }
throw new Error(`Failed to fetch restaurant: ${response.status}`); throw new Error(`Failed to fetch restaurant: ${response.status}`);
} }
const data: AboutResponse = await response.json(); const data: AboutResponse = await response.data;
if (!data.success || !data.data) { if (!data.success || !data.data) {
throw new Error("RESTAURANT_NOT_FOUND"); throw new Error("RESTAURANT_NOT_FOUND");
@@ -25,6 +30,8 @@ export async function getRestaurant(slug: string): Promise<Restaurant> {
return data.data; return data.data;
} catch (error) { } catch (error) {
console.log("error", error);
if (error instanceof Error && error.message === "RESTAURANT_NOT_FOUND") { if (error instanceof Error && error.message === "RESTAURANT_NOT_FOUND") {
throw error; throw error;
} }