2016-02-27 16:28:29 +05:30
|
|
|
function convertQueryValue(value) {
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value === true) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value === false) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-28 00:56:08 +05:30
|
|
|
|
|
|
|
return value;
|
2016-02-27 16:28:29 +05:30
|
|
|
}
|
|
|
|
|
2016-05-22 13:23:40 +05:30
|
|
|
function buildQuery(data = {}) {
|
2016-02-13 20:58:47 +05:30
|
|
|
return Object.keys(data)
|
|
|
|
.map(
|
|
|
|
(keyName) =>
|
2016-02-27 16:28:29 +05:30
|
|
|
[keyName, convertQueryValue(data[keyName])]
|
2016-02-13 20:58:47 +05:30
|
|
|
.map(encodeURIComponent)
|
|
|
|
.join('=')
|
|
|
|
)
|
|
|
|
.join('&')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2016-06-04 22:24:42 +05:30
|
|
|
const middlewares = [];
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
const checkStatus = (resp) => Promise[resp.status >= 200 && resp.status < 300 ? 'resolve' : 'reject'](resp);
|
2016-02-23 11:27:16 +05:30
|
|
|
const toJSON = (resp) => resp.json();
|
2016-03-02 02:06:14 +05:30
|
|
|
const rejectWithJSON = (resp) => toJSON(resp).then((resp) => {throw resp;});
|
2016-06-04 22:24:42 +05:30
|
|
|
const handleResponseSuccess = (resp) => Promise[resp.success || typeof resp.success === 'undefined' ? 'resolve' : 'reject'](resp);
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2016-06-04 22:24:42 +05:30
|
|
|
function doFetch(url, options = {}) {
|
|
|
|
// NOTE: we are wrapping fetch, because it is returning
|
|
|
|
// Promise instance that can not be pollyfilled with Promise.prototype.finally
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-06-04 22:24:42 +05:30
|
|
|
options.headers = options.headers || {};
|
|
|
|
options.headers.Accept = 'application/json';
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-06-04 22:24:42 +05:30
|
|
|
return runMiddlewares('before', {url, options})
|
|
|
|
.then(({url, options}) => fetch(url, options))
|
|
|
|
.then(checkStatus)
|
|
|
|
.then(toJSON, rejectWithJSON)
|
|
|
|
.then(handleResponseSuccess)
|
|
|
|
.then((resp) => runMiddlewares('then', resp))
|
|
|
|
.catch((resp) => runMiddlewares('catch', resp, () => doFetch(url, options)))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} action - the name of middleware's hook (before|then|catch)
|
|
|
|
* @param {Object} data - the initial data to pass through middlewares chain
|
|
|
|
* @param {Function} restart - a function to restart current request (for `catch` hook)
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
function runMiddlewares(action, data, restart) {
|
|
|
|
return middlewares
|
|
|
|
.filter((middleware) => middleware[action])
|
|
|
|
.reduce(
|
|
|
|
(promise, middleware) => promise.then((resp) => middleware[action](resp, restart)),
|
|
|
|
Promise[action === 'catch' ? 'reject' : 'resolve'](data)
|
|
|
|
);
|
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export default {
|
|
|
|
post(url, data) {
|
2016-06-03 00:43:24 +05:30
|
|
|
return doFetch(url, {
|
2016-02-13 20:58:47 +05:30
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
|
},
|
2016-02-27 16:23:58 +05:30
|
|
|
body: buildQuery(data)
|
2016-06-04 22:24:42 +05:30
|
|
|
});
|
2016-02-23 11:27:16 +05:30
|
|
|
},
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-02-23 11:27:16 +05:30
|
|
|
get(url, data) {
|
|
|
|
if (typeof data === 'object') {
|
|
|
|
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
2016-02-27 16:23:58 +05:30
|
|
|
url += separator + buildQuery(data);
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
|
|
|
|
2016-06-04 22:24:42 +05:30
|
|
|
return doFetch(url);
|
2016-02-26 11:55:47 +05:30
|
|
|
},
|
|
|
|
|
2016-02-27 16:23:58 +05:30
|
|
|
buildQuery,
|
|
|
|
|
2016-06-04 22:24:42 +05:30
|
|
|
addMiddleware(middleware) {
|
|
|
|
if (!middlewares.find((mdware) => mdware === middleware)) {
|
|
|
|
middlewares.push(middleware);
|
|
|
|
}
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
|
|
|
};
|