From 8feab8f394ec889efe6aa1f7a9367bc83797dcf3 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 24 Oct 2022 16:10:59 +0300 Subject: [PATCH] ajax fullResponse --- client/src/ajax.js | 43 ++++++++++++++++++++++++++++++++++++------- client/src/app.js | 7 +++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/client/src/ajax.js b/client/src/ajax.js index fe020ea3c9..250430b34c 100644 --- a/client/src/ajax.js +++ b/client/src/ajax.js @@ -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; } } diff --git a/client/src/app.js b/client/src/app.js index 4db8ab798d..6f9c5235d8 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -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) {