Load and save recipes from backend
This commit is contained in:
parent
38a5707622
commit
568606213d
5 changed files with 102 additions and 16 deletions
|
|
@ -4,8 +4,16 @@ import { API_BASE_URL } from "../config/api"
|
|||
/**
|
||||
* Util for handling the recipe api
|
||||
*/
|
||||
/**
|
||||
* URL for handling recipes
|
||||
*/
|
||||
const RECIPE_URL = `${API_BASE_URL}/recipe`
|
||||
|
||||
/**
|
||||
* Load a single recipe
|
||||
* @param id ID of the recipe to load
|
||||
* @returns A single recipe
|
||||
*/
|
||||
export async function fetchRecipe(id: string): Promise<Recipe> {
|
||||
const res = await fetch(`${RECIPE_URL}/${id}`)
|
||||
if (!res.ok) {
|
||||
|
|
@ -14,6 +22,23 @@ export async function fetchRecipe(id: string): Promise<Recipe> {
|
|||
return res.json()
|
||||
}
|
||||
|
||||
/**
|
||||
* Load list of all recipes
|
||||
* @returns Array of recipe
|
||||
*/
|
||||
export async function fetchRecipeList(): Promise<Recipe[]> {
|
||||
const res = await fetch(`${RECIPE_URL}/`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch recipe list`)
|
||||
}
|
||||
return res.json()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new Recipe
|
||||
* @param recipe Recipe to create
|
||||
* @returns Saved recipe
|
||||
*/
|
||||
export async function createRecipe(recipe: Recipe): Promise<Recipe> {
|
||||
const res = await fetch(RECIPE_URL, {
|
||||
method: "POST",
|
||||
|
|
@ -26,6 +51,11 @@ export async function createRecipe(recipe: Recipe): Promise<Recipe> {
|
|||
return res.json()
|
||||
}
|
||||
|
||||
/**
|
||||
* Save an existing recipe
|
||||
* @param recipe Recipe to save. This recipe must have an ID!
|
||||
* @returns Saved recipe
|
||||
*/
|
||||
export async function updateRecipe(recipe: Recipe): Promise<Recipe> {
|
||||
const res = await fetch(`${RECIPE_URL}/${recipe.id}`, {
|
||||
method: "PUT",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue