renaming, restructuring, adding an api util to the frontend (currently editPage only and a mock backend

This commit is contained in:
Anika Raemer 2025-09-10 20:04:26 +02:00
parent 1bd1952ecb
commit 38a5707622
16 changed files with 247 additions and 117 deletions

View file

@ -0,0 +1,39 @@
import type { Recipe } from "../types/recipe"
import { API_BASE_URL } from "../config/api"
/**
* Util for handling the recipe api
*/
const RECIPE_URL = `${API_BASE_URL}/recipe`
export async function fetchRecipe(id: string): Promise<Recipe> {
const res = await fetch(`${RECIPE_URL}/${id}`)
if (!res.ok) {
throw new Error(`Failed to fetch recipe with id ${id}`)
}
return res.json()
}
export async function createRecipe(recipe: Recipe): Promise<Recipe> {
const res = await fetch(RECIPE_URL, {
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(recipe: Recipe): Promise<Recipe> {
const res = await fetch(`${RECIPE_URL}/${recipe.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(recipe),
})
if (!res.ok) {
throw new Error(`Failed to update recipe with id ${recipe.id}`)
}
return res.json()
}