diff --git a/lib/progressPlugBrowser.js b/lib/progressPlugBrowser.js index 008176a..910cabe 100644 --- a/lib/progressPlugBrowser.js +++ b/lib/progressPlugBrowser.js @@ -20,6 +20,22 @@ function _doXhr({ xhr, body, progressInfo }) { xhr.send(body); }); } +function _readCookies(request) { + if(this._cookieManager !== null) { + return this._cookieManager.getCookieString(this.url).then((cookieString) => { + if(cookieString !== '') { + request.xhr.setRequestHeader('Cookie', cookieString); + } + }).then(() => request); + } + return Promise.resolve(request); +} +function _handleCookies(xhr) { + if(this._cookieManager !== null) { + return this._cookieManager.storeCookies(this.url, xhr.getResponseHeader('Set-Cookie')).then(() => xhr); + } + return Promise.resolve(xhr); +} function _doRequest({ method, headers, body = null, progressInfo }) { const xhr = new XMLHttpRequest(); // eslint-disable-line no-undef xhr.open(method, this.url, true); @@ -31,25 +47,9 @@ function _doRequest({ method, headers, body = null, progressInfo }) { } const request = { xhr: xhr, body: body, progressInfo: progressInfo }; progressInfo.callback({ loaded: 0, total: progressInfo.size }); - return this._readCookies(request).then(_doXhr).then(this._handleCookies.bind(this)); + return _readCookies.call(this, request).then(_doXhr).then(_handleCookies.bind(this)); } export class ProgressPlugBrowser extends Plug { - _readCookies(request) { - if(this._cookieManager !== null) { - return this._cookieManager.getCookieString(this.url).then((cookieString) => { - if(cookieString !== '') { - request.xhr.setRequestHeader('Cookie', cookieString); - } - }).then(() => request); - } - return Promise.resolve(request); - } - _handleCookies(xhr) { - if(this._cookieManager !== null) { - return this._cookieManager.storeCookies(this.url, xhr.getResponseHeader('Set-Cookie')).then(() => xhr); - } - return Promise.resolve(xhr); - } constructor(url, params) { super(url, params); } diff --git a/plug.js b/plug.js index aa35504..6afa4da 100644 --- a/plug.js +++ b/plug.js @@ -34,6 +34,22 @@ function _handleHttpError(response) { } }); } +function _readCookies(request) { + if(this._cookieManager !== null) { + return this._cookieManager.getCookieString(request.url).then((cookieString) => { + if(cookieString !== '') { + request.headers.set('Cookie', cookieString); + } + }).then(() => request); + } + return Promise.resolve(request); +} +function _handleCookies(response) { + if(this._cookieManager !== null) { + return this._cookieManager.storeCookies(response.url, response.headers.getAll('Set-Cookie')).then(() => response); + } + return Promise.resolve(response); +} function _doFetch({ method, headers, body = null }) { let requestHeaders = new Headers(headers); let requestData = { method: method, headers: requestHeaders, credentials: 'include' }; @@ -41,25 +57,9 @@ function _doFetch({ method, headers, body = null }) { requestData.body = body; } let request = new Request(this._url.toString(), requestData); - return this._readCookies(request).then(fetch).then(_handleHttpError).then(this._handleCookies.bind(this)); + return _readCookies.call(this, request).then(fetch).then(_handleHttpError).then(_handleCookies.bind(this)); } export class Plug { - _readCookies(request) { - if(this._cookieManager !== null) { - return this._cookieManager.getCookieString(request.url).then((cookieString) => { - if(cookieString !== '') { - request.headers.set('Cookie', cookieString); - } - }).then(() => request); - } - return Promise.resolve(request); - } - _handleCookies(response) { - if(this._cookieManager !== null) { - return this._cookieManager.storeCookies(response.url, response.headers.getAll('Set-Cookie')).then(() => response); - } - return Promise.resolve(response); - } constructor(url = '/', { uriParts = {}, headers = {}, timeout = null, beforeRequest = (params) => params, cookieManager = null } = {}) { // Initialize the url for this instance