cuizin/cuizin/js_src/api.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-03-04 23:37:10 +01:00
import * as constants from '@/constants';
function loadJSON(response) {
return response.json();
}
function _postProcessRecipes(response) {
return loadJSON(response)
.then((jsonResponse) => {
const parsed = jsonResponse;
if (parsed.recipes) {
// Customize instructions
parsed.recipes = parsed.recipes.map(item => Object.assign(
item,
{
instructions: item.instructions ? item.instructions.split(/[\r\n]\n/).map(
2018-03-04 23:37:10 +01:00
line => line.trim(),
) : [],
2018-03-04 23:37:10 +01:00
},
));
}
return parsed;
});
}
export function loadRecipes() {
2018-03-12 19:49:53 +01:00
return fetch(`${constants.API_URL}api/v1/recipes`, { credentials: 'same-origin' })
2018-03-04 23:37:10 +01:00
.then(_postProcessRecipes);
}
export function loadRecipe(id) {
2018-03-12 19:49:53 +01:00
return fetch(`${constants.API_URL}api/v1/recipe/${id}`, { credentials: 'same-origin' })
2018-03-04 23:37:10 +01:00
.then(_postProcessRecipes);
}
export function refetchRecipe(id) {
2018-03-12 19:49:53 +01:00
return fetch(
`${constants.API_URL}api/v1/recipe/${id}/refetch`,
{ credentials: 'same-origin' },
)
2018-03-04 23:37:10 +01:00
.then(_postProcessRecipes);
}
2018-03-10 11:43:14 +01:00
export function postRecipeByUrl(recipe) {
return fetch(`${constants.API_URL}api/v1/recipes/by_url`, {
method: 'POST',
body: JSON.stringify(recipe),
2018-03-12 19:49:53 +01:00
credentials: 'same-origin',
2018-03-10 11:43:14 +01:00
})
.then(_postProcessRecipes);
}
export function postRecipeManually(recipe) {
return fetch(`${constants.API_URL}api/v1/recipes/manually`, {
2018-03-04 23:37:10 +01:00
method: 'POST',
body: JSON.stringify(recipe),
2018-03-12 19:49:53 +01:00
credentials: 'same-origin',
2018-03-04 23:37:10 +01:00
})
.then(_postProcessRecipes);
}
2018-03-11 16:49:05 +01:00
export function editRecipe(id, recipe) {
return fetch(`${constants.API_URL}api/v1/recipe/${id}`, {
method: 'POST',
body: JSON.stringify(recipe),
2018-03-12 19:49:53 +01:00
credentials: 'same-origin',
2018-03-11 16:49:05 +01:00
})
.then(_postProcessRecipes);
}
2018-03-04 23:37:10 +01:00
export function deleteRecipe(id) {
return fetch(`${constants.API_URL}api/v1/recipe/${id}`, {
method: 'DELETE',
2018-03-12 19:49:53 +01:00
credentials: 'same-origin',
2018-03-04 23:37:10 +01:00
});
}