Centralize window.fetch to rl.fetchJSON()

This commit is contained in:
djmaze 2020-09-14 12:39:15 +02:00
parent f300ec94ee
commit ab3ce67376
4 changed files with 40 additions and 66 deletions

View file

@ -116,42 +116,14 @@ class AbstractAjaxRemote {
*/
ajaxRequest(fResultCallback, params, iTimeOut = 20000, sGetAdd = '', abortActions = []) {
params = params || {};
const isPost = !sGetAdd,
start = Date.now(),
const start = Date.now(),
action = params.Action || '';
if (action && abortActions) {
abortActions.forEach(actionToAbort => this.abort(actionToAbort));
}
let init = {
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin'
};
if (isPost) {
init.method = 'POST';
init.headers = {
// 'Content-Type': 'application/json'
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
};
params.XToken = rl.settings.app('token');
// init.body = JSON.stringify(params);
const formData = new FormData(),
buildFormData = (formData, data, parentKey) => {
if (data && typeof data === 'object' && !(data instanceof Date || data instanceof File)) {
Object.keys(data).forEach(key =>
buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key)
);
} else {
formData.set(parentKey, data == null ? '' : data);
}
};
buildFormData(formData, params);
init.body = new URLSearchParams(formData);
}
let init = {};
if (window.AbortController) {
this.abort(action);
@ -163,8 +135,7 @@ class AbstractAjaxRemote {
this.oRequests[action] = controller;
}
fetch(ajax(sGetAdd), init)
.then(response => response.json())
return rl.fetchJSON(ajax(sGetAdd), init, iTimeOut, sGetAdd ? null : params)
.then(oData => {
let cached = false;
if (oData && oData.Time) {