try to use lightweight unfetch instead of heavy axious

This commit is contained in:
igoradamenko
2018-03-10 19:38:58 +03:00
parent e3601629c8
commit b020ef22a0
4 changed files with 30 additions and 38 deletions
+23 -36
View File
@@ -1,7 +1,8 @@
import 'common/promises'; import 'common/promises';
// TODO: i think we need to use unfetch here instead of heavy axios // TODO: i think we need to use unfetch here instead of heavy axios
import axios from 'axios'; // import axios from 'axios';
import fetch from 'unfetch';
import { BASE_URL, API_BASE } from './constants'; import { BASE_URL, API_BASE } from './constants';
import { siteId } from './settings'; import { siteId } from './settings';
@@ -9,16 +10,16 @@ import { siteId } from './settings';
const fetcher = {}; const fetcher = {};
const methods = ['get', 'post', 'put', 'patch', 'delete', 'head']; const methods = ['get', 'post', 'put', 'patch', 'delete', 'head'];
const { CancelToken } = axios; // const { CancelToken } = axios;
let cancelHandler = []; // let cancelHandler = [];
fetcher.cancel = (mask) => { // fetcher.cancel = (mask) => {
cancelHandler.forEach(req => { // cancelHandler.forEach(req => {
if (req.url.includes(mask)) { // if (req.url.includes(mask)) {
req.executor('Operation canceled by the user.'); // req.executor('Operation canceled by the user.');
} // }
}); // });
}; // };
methods.forEach(method => { methods.forEach(method => {
fetcher[method] = data => { fetcher[method] = data => {
@@ -31,40 +32,26 @@ methods.forEach(method => {
const basename = `${BASE_URL}${overriddenApiBase}`; const basename = `${BASE_URL}${overriddenApiBase}`;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
const parameters = { const parameters = {
method, method: method.toUpperCase(),
headers, headers: {
withCredentials, Accept: 'application/json',
'Content-Type': 'application/json',
},
credentials: withCredentials && 'include',
}; };
if (Object.keys(body).length) { if (Object.keys(body).length) {
parameters.data = body; parameters.data = body;
} }
parameters.url = `${basename}${url}`; let requestUrl = `${basename}${url}`;
if (method !== 'post') parameters.url += (parameters.url.includes('?') ? '&' : '?') + `site=${siteId}`; if (method !== 'post') requestUrl += (requestUrl.includes('?') ? '&' : '?') + `site=${siteId}`; // TODO: rewrite it
parameters.cancelToken = new CancelToken(executor => {
cancelHandler.push({
executor,
url: parameters.url,
});
});
axios(parameters) fetch(requestUrl, parameters)
.then(res => resolve(res.data)) .then(res => res.json())
.catch(error => { .then(data => resolve(data))
if (!axios.isCancel(error)) { .catch(error => reject(error));
reject(error);
}
})
.finally(() => {
cancelHandler = cancelHandler.filter(req => req.url !== parameters.url);
});
}); });
} }
}); });
-1
View File
@@ -4,7 +4,6 @@ import Promise from 'promise-polyfill';
Promise._unhandledRejectionFn = () => {}; Promise._unhandledRejectionFn = () => {};
/* eslint-enable no-underscore-dangle */ /* eslint-enable no-underscore-dangle */
// TODO: need to figure out, do we really need finally?
/* eslint-disable no-extend-native */ /* eslint-disable no-extend-native */
Promise.prototype.finally = function finallyFn(callback) { Promise.prototype.finally = function finallyFn(callback) {
const constructor = this.constructor; const constructor = this.constructor;
+5
View File
@@ -11217,6 +11217,11 @@
"integrity": "sha1-ccCL9rQosRM/N+ePo6Icgvcymw0=", "integrity": "sha1-ccCL9rQosRM/N+ePo6Icgvcymw0=",
"dev": true "dev": true
}, },
"unfetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/unfetch/-/unfetch-3.0.0.tgz",
"integrity": "sha1-jR4FE6Ts0OX/LUGmund3Gq6LZII="
},
"union-value": { "union-value": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.0.tgz", "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.0.tgz",
+2 -1
View File
@@ -34,6 +34,7 @@
"babel-polyfill": "^6.23.0", "babel-polyfill": "^6.23.0",
"bem-react-helper": "^1.1.2", "bem-react-helper": "^1.1.2",
"preact": "^8.2.7", "preact": "^8.2.7",
"promise-polyfill": "^7.0.0" "promise-polyfill": "^7.0.0",
"unfetch": "^3.0.0"
} }
} }