philomena/assets/js/utils/requests.js

42 lines
820 B
JavaScript

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