ajax fullResponse

This commit is contained in:
Yuri Kuznetsov
2022-10-24 16:10:59 +03:00
parent 0c350b5f16
commit 8feab8f394
2 changed files with 39 additions and 11 deletions
+36 -7
View File
@@ -46,6 +46,7 @@ define('ajax', [], function () {
* @property {'xml'|'json'|'text'} [dataType] A data type.
* @property {boolean} [local] If true, the API URL won't be prepended.
* @property {string} [contentType] A content type.
* @property {boolean} [fullResponse] To resolve with `module:ajax.XhrWrapper`.
*/
/**
@@ -69,11 +70,15 @@ define('ajax', [], function () {
return new AjaxPromise((resolve, reject) => {
$.ajax(options)
.then((...args) => {
resolve(...args);
.then((response, status, xhr) => {
let obj = options.fullResponse ?
new XhrWrapper(xhr) :
response;
resolve(obj);
})
.fail((...args) => {
reject(...args);
.fail(xhr => {
reject(xhr);
});
});
},
@@ -158,10 +163,34 @@ define('ajax', [], function () {
// For bc.
class AjaxPromise extends Promise {
fail(...args) {
return this.catch(...args);
return this.catch(args[0]);
}
success(...args) {
return this.then(...args);
done(...args) {
return this.then(args[0]);
}
}
/**
* @name module:ajax.XhrWrapper
*/
class XhrWrapper {
/**
* @param {JQueryXHR} xhr
*/
constructor(xhr) {
this.xhr = xhr;
}
getResponseHeader(name) {
return this.xhr.getResponseHeader(name);
}
getStatus() {
return this.xhr.status;
}
getParsedBody() {
return this.xhr.responseJSON;
}
}
+3 -4
View File
@@ -930,10 +930,9 @@ function (
let arr = Base64.decode(this.auth).split(':');
if (arr.length > 1) {
Ajax.postRequest('App/action/destroyAuthToken', {
token: arr[1]
})
.then((data, status, xhr) => {
Ajax
.postRequest('App/action/destroyAuthToken', {token: arr[1]}, {fullResponse: true})
.then(xhr => {
let redirectUrl = xhr.getResponseHeader('X-Logout-Redirect-Url');
if (redirectUrl) {