mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-26 03:14:19 +00:00
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
var Requests = {};
|
|
|
|
Requests.JSONfetch = (input, init={}) => {
|
|
return new Promise((resolve, reject) => {
|
|
let fixedInit = {};
|
|
fixedInit.headers = {};
|
|
fixedInit.headers['Accept'] = 'application/json';
|
|
if (init.hasOwnProperty('body')) {
|
|
fixedInit.headers['Content-Type'] = 'application/json';
|
|
}
|
|
fetch(input, Utils.mergeObjectsDeep(init, fixedInit))
|
|
.then(
|
|
(response) => {
|
|
if (response.ok) {
|
|
resolve(response.clone());
|
|
} else {
|
|
reject(response);
|
|
}
|
|
if (response.status === 204) {
|
|
return;
|
|
}
|
|
response.json()
|
|
.then(
|
|
(json) => {
|
|
let message = json.message;
|
|
let category = json.category || 'message';
|
|
if (message) {
|
|
app.flash(message, category);
|
|
}
|
|
},
|
|
(error) => {
|
|
app.flash(`[${response.status}]: ${response.statusText}`, 'error');
|
|
}
|
|
);
|
|
},
|
|
(response) => {
|
|
app.flash('Something went wrong', 'error');
|
|
reject(response);
|
|
}
|
|
);
|
|
});
|
|
};
|