Remove axios and use fetch

This commit is contained in:
dvirlabs 2025-12-05 11:38:26 +02:00
parent 7eeaf2fad9
commit 56d00c2ed8

View File

@ -1,31 +1,43 @@
import axios from "axios";
const API_BASE = "http://localhost:8000"; const API_BASE = "http://localhost:8000";
export async function getRecipes() { export async function getRecipes() {
const res = await axios.get(`${API_BASE}/recipes`); const res = await fetch(`${API_BASE}/recipes`);
return res.data; if (!res.ok) {
throw new Error("Failed to fetch recipes");
}
return res.json();
} }
export async function getRandomRecipe(filters) { export async function getRandomRecipe(filters) {
const params = {}; const params = new URLSearchParams();
if (filters.mealType) params.meal_type = filters.mealType; if (filters.mealType) params.append("meal_type", filters.mealType);
if (filters.maxTime) params.max_time = filters.maxTime; if (filters.maxTime) params.append("max_time", filters.maxTime);
if (filters.ingredients && filters.ingredients.length > 0) { if (filters.ingredients && filters.ingredients.length > 0) {
filters.ingredients.forEach((ing) => { filters.ingredients.forEach((ing) => {
params.ingredients = params.ingredients || []; params.append("ingredients", ing);
params.ingredients.push(ing);
}); });
} }
const res = await axios.get(`${API_BASE}/recipes/random`, { params }); const queryString = params.toString();
return res.data; const url = queryString ? `${API_BASE}/recipes/random?${queryString}` : `${API_BASE}/recipes/random`;
const res = await fetch(url);
if (!res.ok) {
throw new Error("Failed to fetch random recipe");
}
return res.json();
} }
export async function createRecipe(recipe) { export async function createRecipe(recipe) {
const res = await axios.post(`${API_BASE}/recipes`, recipe); const res = await fetch(`${API_BASE}/recipes`, {
return res.data; method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(recipe),
});
if (!res.ok) {
throw new Error("Failed to create recipe");
}
return res.json();
} }
export async function updateRecipe(id, payload) { export async function updateRecipe(id, payload) {