mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 17:25:44 +00:00
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
|
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) => {
|
||
|
response.json()
|
||
|
.then(
|
||
|
(json) => {
|
||
|
let message = json.message || json;
|
||
|
let category = json.category || 'message';
|
||
|
app.flash(message, category);
|
||
|
},
|
||
|
(error) => {
|
||
|
app.flash(`[${response.status}]: ${response.statusText}`, 'error');
|
||
|
}
|
||
|
);
|
||
|
if (response.ok) {
|
||
|
resolve(response);
|
||
|
} else {
|
||
|
reject(response);
|
||
|
}
|
||
|
},
|
||
|
(response) => {
|
||
|
app.flash('Something went wrong', 'error');
|
||
|
reject(response);
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
};
|