diff --git a/web/app/common/fetcher.js b/web/app/common/fetcher.js index 6075db30..675384f6 100644 --- a/web/app/common/fetcher.js +++ b/web/app/common/fetcher.js @@ -1,8 +1,7 @@ import 'common/promises'; // TODO: i think we need to use unfetch here instead of heavy axios -// import axios from 'axios'; -import fetch from 'unfetch'; +import axios from 'axios'; import { BASE_URL, API_BASE } from './constants'; import { siteId } from './settings'; @@ -10,16 +9,16 @@ import { siteId } from './settings'; const fetcher = {}; const methods = ['get', 'post', 'put', 'patch', 'delete', 'head']; -// const { CancelToken } = axios; -// let cancelHandler = []; +const { CancelToken } = axios; +let cancelHandler = []; -// fetcher.cancel = (mask) => { -// cancelHandler.forEach(req => { -// if (req.url.includes(mask)) { -// req.executor('Operation canceled by the user.'); -// } -// }); -// }; +fetcher.cancel = (mask) => { + cancelHandler.forEach(req => { + if (req.url.includes(mask)) { + req.executor('Operation canceled by the user.'); + } + }); +}; methods.forEach(method => { fetcher[method] = data => { @@ -32,26 +31,40 @@ methods.forEach(method => { const basename = `${BASE_URL}${overriddenApiBase}`; return new Promise((resolve, reject) => { + const headers = { + Accept: 'application/json', + 'Content-Type': 'application/json', + }; + const parameters = { - method: method.toUpperCase(), - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - credentials: withCredentials && 'include', + method, + headers, + withCredentials, }; if (Object.keys(body).length) { parameters.data = body; } - let requestUrl = `${basename}${url}`; - if (method !== 'post') requestUrl += (requestUrl.includes('?') ? '&' : '?') + `site=${siteId}`; // TODO: rewrite it + parameters.url = `${basename}${url}`; + if (method !== 'post') parameters.url += (parameters.url.includes('?') ? '&' : '?') + `site=${siteId}`; + parameters.cancelToken = new CancelToken(executor => { + cancelHandler.push({ + executor, + url: parameters.url, + }); + }); - fetch(requestUrl, parameters) - .then(res => res.json()) - .then(data => resolve(data)) - .catch(error => reject(error)); + axios(parameters) + .then(res => resolve(res.data)) + .catch(error => { + if (!axios.isCancel(error)) { + reject(error); + } + }) + .finally(() => { + cancelHandler = cancelHandler.filter(req => req.url !== parameters.url); + }); }); } }); diff --git a/web/app/common/promises.js b/web/app/common/promises.js index dff22a77..cd77d551 100644 --- a/web/app/common/promises.js +++ b/web/app/common/promises.js @@ -4,6 +4,7 @@ import Promise from 'promise-polyfill'; Promise._unhandledRejectionFn = () => {}; /* eslint-enable no-underscore-dangle */ +// TODO: need to figure out, do we really need finally? /* eslint-disable no-extend-native */ Promise.prototype.finally = function finallyFn(callback) { const constructor = this.constructor; diff --git a/web/package-lock.json b/web/package-lock.json index a97bf84f..9ead1997 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -11217,11 +11217,6 @@ "integrity": "sha1-ccCL9rQosRM/N+ePo6Icgvcymw0=", "dev": true }, - "unfetch": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/unfetch/-/unfetch-3.0.0.tgz", - "integrity": "sha1-jR4FE6Ts0OX/LUGmund3Gq6LZII=" - }, "union-value": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.0.tgz", diff --git a/web/package.json b/web/package.json index be8a86d2..fc6df2e9 100644 --- a/web/package.json +++ b/web/package.json @@ -34,7 +34,6 @@ "babel-polyfill": "^6.23.0", "bem-react-helper": "^1.1.2", "preact": "^8.2.7", - "promise-polyfill": "^7.0.0", - "unfetch": "^3.0.0" + "promise-polyfill": "^7.0.0" } }