Factor API calls

This commit is contained in:
Lucas Verney 2018-03-13 19:02:10 +01:00
parent 9bbac3b99e
commit 680f00c188

View File

@ -1,6 +1,14 @@
import * as constants from '@/constants'; import * as constants from '@/constants';
function fetchAPI(endpoint, params = {}) {
return fetch(
`${constants.API_URL}${endpoint}`,
Object.assign({}, { credentials: 'same-origin' }, params),
);
}
function loadJSON(response) { function loadJSON(response) {
return response.json(); return response.json();
} }
@ -29,59 +37,52 @@ function _postProcessRecipes(response) {
export function loadRecipes() { export function loadRecipes() {
return fetch(`${constants.API_URL}api/v1/recipes`, { credentials: 'same-origin' }) return fetchAPI('api/v1/recipes')
.then(_postProcessRecipes); .then(_postProcessRecipes);
} }
export function loadRecipe(id) { export function loadRecipe(id) {
return fetch(`${constants.API_URL}api/v1/recipe/${id}`, { credentials: 'same-origin' }) return fetchAPI(`api/v1/recipe/${id}`)
.then(_postProcessRecipes); .then(_postProcessRecipes);
} }
export function refetchRecipe(id) { export function refetchRecipe(id) {
return fetch( return fetchAPI(`api/v1/recipe/${id}/refetch`)
`${constants.API_URL}api/v1/recipe/${id}/refetch`,
{ credentials: 'same-origin' },
)
.then(_postProcessRecipes); .then(_postProcessRecipes);
} }
export function postRecipeByUrl(recipe) { export function postRecipeByUrl(recipe) {
return fetch(`${constants.API_URL}api/v1/recipes/by_url`, { return fetchAPI('api/v1/recipes/by_url', {
method: 'POST', method: 'POST',
body: JSON.stringify(recipe), body: JSON.stringify(recipe),
credentials: 'same-origin',
}) })
.then(_postProcessRecipes); .then(_postProcessRecipes);
} }
export function postRecipeManually(recipe) { export function postRecipeManually(recipe) {
return fetch(`${constants.API_URL}api/v1/recipes/manually`, { return fetchAPI('api/v1/recipes/manually', {
method: 'POST', method: 'POST',
body: JSON.stringify(recipe), body: JSON.stringify(recipe),
credentials: 'same-origin',
}) })
.then(_postProcessRecipes); .then(_postProcessRecipes);
} }
export function editRecipe(id, recipe) { export function editRecipe(id, recipe) {
return fetch(`${constants.API_URL}api/v1/recipe/${id}`, { return fetchAPI(`api/v1/recipe/${id}`, {
method: 'POST', method: 'POST',
body: JSON.stringify(recipe), body: JSON.stringify(recipe),
credentials: 'same-origin',
}) })
.then(_postProcessRecipes); .then(_postProcessRecipes);
} }
export function deleteRecipe(id) { export function deleteRecipe(id) {
return fetch(`${constants.API_URL}api/v1/recipe/${id}`, { return fetchAPI(`api/v1/recipe/${id}`, {
method: 'DELETE', method: 'DELETE',
credentials: 'same-origin',
}); });
} }