snappymail/dev/Promises/AbstractAjax.js

181 lines
5.1 KiB
JavaScript
Raw Normal View History

import window from 'window';
2019-07-05 03:19:24 +08:00
import { ajax } from 'Common/Links';
import { isNormal, pString } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { DEFAULT_AJAX_TIMEOUT, TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT } from 'Common/Consts';
import { Notification } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { data as GlobalsData } from 'Common/Globals';
import { runHook } from 'Common/Plugins';
import * as Settings from 'Storage/Settings';
2019-07-05 03:19:24 +08:00
import { AbstractBasicPromises } from 'Promises/AbstractBasic';
2019-07-05 03:19:24 +08:00
class AbstractAjaxPromises extends AbstractBasicPromises {
2016-09-10 06:38:16 +08:00
oRequests = {};
constructor() {
super();
2016-09-10 06:38:16 +08:00
this.clear();
}
clear() {
this.oRequests = {};
}
abort(sAction, bClearOnly) {
2019-07-05 03:19:24 +08:00
if (this.oRequests[sAction]) {
if (!bClearOnly && this.oRequests[sAction].abort) {
// this.oRequests[sAction].__aborted__ = true;
this.oRequests[sAction].abort();
}
this.oRequests[sAction] = null;
delete this.oRequests[sAction];
}
return this;
2016-06-30 08:02:45 +08:00
}
ajaxRequest(action, isPost, timeOut, params, additionalGetString, fTrigger) {
additionalGetString = undefined === 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 = Settings.appSettingsGet('token');
// init.body = JSON.stringify(params);
const formData = new window.FormData();
Object.keys(params).forEach(key => {
formData.append(key, params[key])
});
init.body = (new URLSearchParams(formData)).toString();
}
runHook('ajax-default-request', [action, params, additionalGetString]);
2015-04-29 04:51:20 +08:00
this.setTrigger(fTrigger, true);
2015-04-29 04:51:20 +08:00
if (window.AbortController) {
this.abort(action);
const controller = new window.AbortController();
setTimeout(() => controller.abort(), isNormal(timeOut) ? timeOut : DEFAULT_AJAX_TIMEOUT);
init.signal = controller.signal;
this.oRequests[action] = controller;
}
2016-05-24 01:33:01 +08:00
return window.fetch(ajax(additionalGetString), init)
.then(response => response.json())
.then(data => {
this.abort(action, true);
2016-05-24 01:33:01 +08:00
if (!data) {
return Promise.reject(Notification.AjaxParse);
}
2016-05-24 01:33:01 +08:00
if (data.UpdateToken && GlobalsData.__APP__ && GlobalsData.__APP__.setClientSideToken) {
GlobalsData.__APP__.setClientSideToken(data.UpdateToken);
2020-01-01 01:44:41 +08:00
}
/*
let isCached = false, type = '';
if (data && data.Time) {
isCached = pInt(data.Time) > microtime() - start;
}
// backward capability
2019-07-05 03:19:24 +08:00
switch (true) {
case 'success' === textStatus && data && data.Result && action === data.Action:
type = StorageResultType.Success;
break;
case 'abort' === textStatus && (!data || !data.__aborted__):
type = StorageResultType.Abort;
break;
default:
type = StorageResultType.Error;
break;
}
runHook('ajax-default-response', [
2019-07-05 03:19:24 +08:00
action,
StorageResultType.Success === type ? data : null,
type,
isCached,
params
]);
*/
this.setTrigger(fTrigger, false);
2016-06-30 08:02:45 +08:00
if (!data.Result || action !== data.Action) {
if ([
2019-07-05 03:19:24 +08:00
Notification.AuthError,
Notification.AccessError,
Notification.ConnectionError,
Notification.DomainNotAllowed,
Notification.AccountNotAllowed,
Notification.MailServerError,
Notification.UnknownNotification,
Notification.UnknownError
].includes(data.ErrorCode)
2019-07-05 03:19:24 +08:00
) {
++GlobalsData.iAjaxErrorCount;
2015-05-06 00:41:15 +08:00
}
if (Notification.InvalidToken === data.ErrorCode) {
++GlobalsData.iTokenErrorCount;
}
2019-07-05 03:19:24 +08:00
if (TOKEN_ERROR_LIMIT < GlobalsData.iTokenErrorCount) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.loginAndLogoutReload) {
GlobalsData.__APP__.loginAndLogoutReload(false, true);
}
2016-05-24 01:33:01 +08:00
}
if (data.ClearAuth || data.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount) {
2019-07-05 03:19:24 +08:00
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken) {
GlobalsData.__APP__.clearClientSideToken();
}
if (GlobalsData.__APP__ && !data.ClearAuth && GlobalsData.__APP__.loginAndLogoutReload) {
GlobalsData.__APP__.loginAndLogoutReload(false, true);
}
2015-05-06 00:41:15 +08:00
}
return Promise.reject(data.ErrorCode ? data.ErrorCode : Notification.AjaxFalse);
}
return data;
}).catch(err => {
if (err.name == 'AbortError') { // handle abort()
return Promise.reject(Notification.AjaxAbort);
}
return Promise.reject(err);
});
}
getRequest(sAction, fTrigger, sAdditionalGetString, iTimeOut) {
sAdditionalGetString = undefined === sAdditionalGetString ? '' : pString(sAdditionalGetString);
sAdditionalGetString = sAction + '/' + sAdditionalGetString;
return this.ajaxRequest(sAction, false, iTimeOut, null, sAdditionalGetString, fTrigger);
}
postRequest(action, fTrigger, params, timeOut) {
params = params || {};
params.Action = action;
return this.ajaxRequest(action, true, timeOut, params, '', fTrigger);
}
}
2019-07-05 03:19:24 +08:00
export { AbstractAjaxPromises, AbstractAjaxPromises as default };