nopaque/app/static/js/Requests/Requests.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-03-08 10:42:53 +00:00
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) => {
2023-03-09 13:55:08 +00:00
if (response.ok) {
resolve(response.clone());
} else {
reject(response);
}
if (response.status === 204) {
return;
}
2023-03-08 10:42:53 +00:00
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');
}
);
},
(response) => {
app.flash('Something went wrong', 'error');
reject(response);
}
);
});
};