2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Request Utils
|
|
|
|
*/
|
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
function fetchJson(verb, endpoint, body) {
|
2019-10-05 02:09:52 +02:00
|
|
|
const data = {
|
|
|
|
method: verb,
|
|
|
|
credentials: 'same-origin',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2020-09-28 05:53:14 +02:00
|
|
|
'x-csrf-token': window.booru.csrfToken,
|
|
|
|
'x-requested-with': 'xmlhttprequest'
|
2019-10-05 02:09:52 +02: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-28 05:53:14 +02:00
|
|
|
'x-csrf-token': window.booru.csrfToken,
|
|
|
|
'x-requested-with': 'xmlhttprequest'
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Received error from server');
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { fetchJson, fetchHtml, handleError };
|