Moved rl.fetch and rl.fetchJSON to boot.js so that AppData can be fetched as JSON

This commit is contained in:
the-djmaze 2023-02-21 09:04:47 +01:00
parent 91bc5931fd
commit 8b70fee072
3 changed files with 80 additions and 84 deletions

74
dev/bootstrap.js vendored
View file

@ -1,10 +1,6 @@
import { Settings } from 'Common/Globals';
import { i18n } from 'Common/Translator';
import { root } from 'Common/Links';
import { isArray } from 'Common/Utils';
export default App => {
rl.app = App;
@ -33,74 +29,4 @@ export default App => {
on: () => hasher.active = true
};
rl.fetch = (resource, init, postData) => {
init = Object.assign({
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin',
headers: {}
}, init);
if (postData) {
init.method = 'POST';
let asJSON = 1,
XToken = Settings.app('token'),
object = {};
if (postData instanceof FormData) {
postData.forEach((value, key) => {
if (value instanceof File) {
asJSON = 0;
} else if (!Reflect.has(object, key)) {
object[key] = value;
} else {
isArray(object[key]) || (object[key] = [object[key]]);
object[key].push(value);
}
});
if (asJSON) {
postData = object;
} else {
postData.set('XToken', XToken);
}
}
if (asJSON) {
init.headers['Content-Type'] = 'application/json';
postData = JSON.stringify(postData);
}
init.body = postData;
}
init.headers['X-SM-Token'] = Settings.app('token');
// init.headers = new Headers(init.headers);
return fetch(resource, init);
};
rl.fetchJSON = (resource, init, postData) => {
init = Object.assign({ headers: {} }, init);
init.headers.Accept = 'application/json';
return rl.fetch(resource, init, postData).then(response => {
if (!response.ok) {
return Promise.reject('Network response error: ' + response.status);
}
/* TODO: use this for non-developers?
response.clone()
let data = response.text();
try {
return JSON.parse(data);
} catch (e) {
console.error(e);
// console.log(data);
return Promise.reject(Notifications.JsonParse);
return {
Result: false,
ErrorCode: 952, // Notifications.JsonParse
ErrorMessage: e.message,
ErrorMessageAdditional: data
}
}
*/
return response.json();
});
};
};