2019-08-31 04:07:25 +05:00
|
|
|
import { fetch } from 'whatwg-fetch';
|
|
|
|
|
|
|
|
import Config from '../constants/Config';
|
|
|
|
|
|
|
|
const http = {};
|
|
|
|
|
|
|
|
// TODO: all methods
|
|
|
|
['POST'].forEach((method) => {
|
|
|
|
http[method.toLowerCase()] = (url, data, headers) => {
|
|
|
|
const formData = Object.keys(data).reduce((result, key) => {
|
|
|
|
result.append(key, data[key]);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}, new FormData());
|
|
|
|
|
2019-10-01 04:18:33 +05:00
|
|
|
return fetch(`${Config.SERVER_BASE_URL}/api${url}`, {
|
2019-08-31 04:07:25 +05:00
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then((response) => response.json().then((body) => ({
|
|
|
|
body,
|
|
|
|
isError: response.status !== 200,
|
|
|
|
})))
|
|
|
|
.then(({ body, isError }) => {
|
|
|
|
if (isError) {
|
|
|
|
throw body;
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export default http;
|