2016-07-30 23:52:33 +05:30
|
|
|
import PromiseMiddlewareLayer from './PromiseMiddlewareLayer';
|
|
|
|
|
|
|
|
const middlewareLayer = new PromiseMiddlewareLayer();
|
2016-02-27 16:28:29 +05:30
|
|
|
|
2016-06-04 22:41:42 +05:30
|
|
|
export default {
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} data
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-06-04 22:41:42 +05:30
|
|
|
post(url, data) {
|
|
|
|
return doFetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
|
},
|
|
|
|
body: buildQuery(data)
|
|
|
|
});
|
|
|
|
},
|
2016-02-27 16:28:29 +05:30
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} data
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-06-04 22:41:42 +05:30
|
|
|
get(url, data) {
|
|
|
|
if (typeof data === 'object') {
|
|
|
|
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
|
|
|
url += separator + buildQuery(data);
|
|
|
|
}
|
2016-02-28 00:56:08 +05:30
|
|
|
|
2016-06-04 22:41:42 +05:30
|
|
|
return doFetch(url);
|
|
|
|
},
|
2016-02-27 16:28:29 +05:30
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* Serializes object into encoded key=value presentation
|
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2016-06-04 22:41:42 +05:30
|
|
|
buildQuery,
|
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* @param {object} middleware
|
|
|
|
* @param {function} [middleware.before] - a function({url, options}), that will be called before executing request.
|
|
|
|
* It will get data object {url, options} as an argument and should return
|
|
|
|
* Promise, that will resolve into new data object
|
|
|
|
* @param {function} [middleware.then] - a function(resp), that will be called on successful request result. It will
|
|
|
|
* get response as an argument and should return a Promise that resolves to
|
|
|
|
* the new response
|
|
|
|
* @param {function} [middleware.catch] - a function(resp, restart), that will be called on request fail. It will
|
|
|
|
* get response and callback to restart request as an arguments and should
|
|
|
|
* return a Promise that resolves to the new response.
|
|
|
|
*/
|
2016-06-04 22:41:42 +05:30
|
|
|
addMiddleware(middleware) {
|
2016-07-30 23:52:33 +05:30
|
|
|
middlewareLayer.add(middleware);
|
2016-06-04 22:41:42 +05:30
|
|
|
}
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
|
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-07-30 00:59:27 +05:30
|
|
|
const toJSON = (resp) => resp.json().then((json) => ({...json, originalResponse: resp}));
|
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-07-30 23:52:33 +05:30
|
|
|
return middlewareLayer.run('before', {url, options})
|
2016-06-04 22:24:42 +05:30
|
|
|
.then(({url, options}) => fetch(url, options))
|
|
|
|
.then(checkStatus)
|
|
|
|
.then(toJSON, rejectWithJSON)
|
|
|
|
.then(handleResponseSuccess)
|
2016-07-30 23:52:33 +05:30
|
|
|
.then((resp) => middlewareLayer.run('then', resp))
|
|
|
|
.catch((resp) => middlewareLayer.run('catch', resp, () => doFetch(url, options)))
|
2016-06-04 22:24:42 +05:30
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* Converts specific js values to query friendly values
|
|
|
|
*
|
|
|
|
* @param {any} value
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2016-06-04 22:41:42 +05:30
|
|
|
function convertQueryValue(value) {
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
return '';
|
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-06-04 22:41:42 +05:30
|
|
|
if (value === true) {
|
2016-07-29 23:45:16 +05:30
|
|
|
return '1';
|
2016-06-04 22:41:42 +05:30
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
|
2016-06-04 22:41:42 +05:30
|
|
|
if (value === false) {
|
2016-07-29 23:45:16 +05:30
|
|
|
return '0';
|
2016-06-04 22:41:42 +05:30
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-06-04 22:41:42 +05:30
|
|
|
return value;
|
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* Serializes object into encoded key=value presentation
|
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2016-06-04 22:41:42 +05:30
|
|
|
function buildQuery(data = {}) {
|
|
|
|
return Object.keys(data)
|
|
|
|
.map(
|
|
|
|
(keyName) =>
|
|
|
|
[keyName, convertQueryValue(data[keyName])]
|
|
|
|
.map(encodeURIComponent)
|
|
|
|
.join('=')
|
|
|
|
)
|
|
|
|
.join('&')
|
|
|
|
;
|
|
|
|
}
|