philomena/assets/js/utils/requests.js

42 lines
800 B
JavaScript
Raw Normal View History

2019-10-04 20:09:52 -04:00
/**
* Request Utils
*/
function fetchJson(verb, endpoint, body) {
const data = {
method: verb,
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
2020-09-27 23:53:14 -04:00
'x-csrf-token': window.booru.csrfToken,
'x-requested-with': 'xmlhttprequest'
2019-10-04 20:09:52 -04:00
},
};
if (body) {
body._method = verb;
data.body = JSON.stringify(body);
}
return fetch(endpoint, data);
}
function fetchHtml(endpoint) {
return fetch(endpoint, {
credentials: 'same-origin',
headers: {
2020-09-27 23:53:14 -04:00
'x-csrf-token': window.booru.csrfToken,
'x-requested-with': 'xmlhttprequest'
2019-10-04 20:09:52 -04:00
},
});
}
function handleError(response) {
if (!response.ok) {
throw new Error('Received error from server');
}
return response;
}
export { fetchJson, fetchHtml, handleError };