From ab3ce6737611e83a247f0f9704c62e9a85bb9447 Mon Sep 17 00:00:00 2001 From: djmaze Date: Mon, 14 Sep 2020 12:39:15 +0200 Subject: [PATCH] Centralize window.fetch to rl.fetchJSON() --- dev/Common/Utils.js | 3 +-- dev/Promises/AbstractAjax.js | 32 ++------------------------------ dev/Remote/AbstractAjax.js | 35 +++-------------------------------- dev/bootstrap.js | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 66 deletions(-) diff --git a/dev/Common/Utils.js b/dev/Common/Utils.js index 5777f6d95..a9f2fe4cf 100644 --- a/dev/Common/Utils.js +++ b/dev/Common/Utils.js @@ -711,8 +711,7 @@ export function changeTheme(value, themeTrigger = ()=>{}) { __themeAjax = new AbortController(); init.signal = __themeAjax.signal; } - fetch(url, init) - .then(response => response.json()) + rl.fetchJSON(url, init) .then(data => { if (data && isArray(data) && 2 === data.length) { if (themeLink && !themeStyle) { diff --git a/dev/Promises/AbstractAjax.js b/dev/Promises/AbstractAjax.js index e208a7f58..6faf26ab6 100644 --- a/dev/Promises/AbstractAjax.js +++ b/dev/Promises/AbstractAjax.js @@ -37,34 +37,7 @@ class AbstractAjaxPromises extends AbstractBasicPromises { additionalGetString = pString(additionalGetString); - 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 = {}; this.setTrigger(fTrigger, true); @@ -76,8 +49,7 @@ class AbstractAjaxPromises extends AbstractBasicPromises { this.oRequests[action] = controller; } - return fetch(ajax(additionalGetString), init) - .then(response => response.json()) + return rl.fetchJSON(ajax(additionalGetString), init, pInt(timeOut, DEFAULT_AJAX_TIMEOUT), isPost ? params : null) .then(data => { this.abort(action, true); diff --git a/dev/Remote/AbstractAjax.js b/dev/Remote/AbstractAjax.js index 92755be71..8c5ec07c4 100644 --- a/dev/Remote/AbstractAjax.js +++ b/dev/Remote/AbstractAjax.js @@ -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) { diff --git a/dev/bootstrap.js b/dev/bootstrap.js index 6521aef91..a7626bf9e 100644 --- a/dev/bootstrap.js +++ b/dev/bootstrap.js @@ -2,7 +2,6 @@ import { data as GlobalsData, dropdownVisibility } from 'Common/Globals'; import * as Enums from 'Common/Enums'; import * as Plugins from 'Common/Plugins'; import { i18n } from 'Common/Translator'; -import { EmailModel } from 'Model/Email'; export default (App) => { GlobalsData.__APP__ = App; @@ -45,7 +44,6 @@ export default (App) => { rl.pluginSettingsGet = Plugins.settingsGet; rl.pluginRemoteRequest = Plugins.remoteRequest; - rl.EmailModel = EmailModel; rl.Enums = Enums; rl.Dropdowns = []; @@ -57,6 +55,40 @@ export default (App) => { dropdownVisibility(!!rl.Dropdowns.find(item => item.classList.contains('open'))) ).debounce(50); + rl.fetchJSON = (resource, init, timeout, postData) => { + init = Object.assign({ + mode: 'same-origin', + cache: 'no-cache', + redirect: 'error', + referrerPolicy: 'no-referrer', + credentials: 'same-origin' + }, init); + + if (postData) { + init.method = 'POST'; + init.headers = { +// 'Content-Type': 'application/json' + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' + }; + postData.XToken = rl.settings.app('token'); +// init.body = JSON.stringify(postData); + 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, postData); + init.body = new URLSearchParams(formData); + } + + return fetch(resource, init).then(response => response.json()); + }; + window.__APP_BOOT = fErrorCallback => { const doc = document, cb = () => setTimeout(() => {