munch-ease-frontend/src/data.js

77 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-01-14 01:46:45 +00:00
const BASE_URL = "http://192.168.68.177:8000"
2024-01-13 02:00:15 +00:00
export default {
2024-01-17 09:17:22 +00:00
async getMeals(from, to) {
2024-01-17 11:31:45 +00:00
const response = await fetch(BASE_URL + "/meals?from=" + from.toISOString() + "&to=" + to.toISOString());
const meals = await response.json();
for (const meal of meals) {
meal.date = new Date(meal.date);
}
return meals;
2024-01-13 02:00:15 +00:00
},
2024-01-17 09:17:22 +00:00
async getMeal(id) {
var response = await fetch(BASE_URL + `/meals/${encodeURIComponent(id)}`);
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-13 02:00:15 +00:00
},
2024-01-17 11:57:44 +00:00
async deleteMeal(id) {
var response = await fetch(BASE_URL + `/meals/${encodeURIComponent(id)}`, {
method: "DELETE",
});
return await response.json();
},
2024-01-17 09:17:22 +00:00
async searchRecipes(query) {
const response = await fetch(BASE_URL + "/recipes?q=" + encodeURIComponent(query));
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-13 02:00:15 +00:00
},
2024-01-17 09:17:22 +00:00
async parseRecipe(url) {
const response = await fetch(BASE_URL + `/recipes/parse?url=${encodeURIComponent(url)}`);
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-13 02:00:15 +00:00
},
2024-01-17 09:17:22 +00:00
async getRecipe(id) {
const response = await fetch(BASE_URL + `/recipes/${encodeURIComponent(id)}`);
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-13 07:00:55 +00:00
},
2024-01-17 09:17:22 +00:00
async parseProduct(ingredient, url) {
2024-01-13 02:00:15 +00:00
const body = {
url, tags: [ingredient.name, ingredient.line],
2024-01-17 09:17:22 +00:00
};
2024-01-13 03:22:10 +00:00
2024-01-17 09:17:22 +00:00
const response = await fetch(BASE_URL + "/products", {
2024-01-13 02:00:15 +00:00
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body),
2024-01-17 09:17:22 +00:00
});
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-13 02:00:15 +00:00
},
2024-01-17 09:17:22 +00:00
async parseIngredients(lines) {
2024-01-13 02:00:15 +00:00
const params = lines.map(line => "ingredients=" + encodeURIComponent(line)).join("&");
2024-01-17 09:17:22 +00:00
const response = await fetch(BASE_URL + "/recipes/ingredients/parse?" + params);
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-13 03:22:10 +00:00
},
2024-01-17 09:17:22 +00:00
async saveRecipe(recipe) {
const response = await fetch(BASE_URL + "/recipes", {
2024-01-13 03:22:10 +00:00
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(recipe),
2024-01-17 09:17:22 +00:00
});
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-17 09:17:22 +00:00
},
async saveMeal(meal) {
const response = await fetch(BASE_URL + "/meals", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(meal),
});
2024-01-17 11:31:45 +00:00
return await response.json();
2024-01-17 09:17:22 +00:00
},
2024-01-13 02:00:15 +00:00
}