2019-08-31 04:07:25 +05:00
|
|
|
import { fetch } from 'whatwg-fetch';
|
|
|
|
|
|
|
|
import Config from '../constants/Config';
|
|
|
|
|
|
|
|
const http = {};
|
|
|
|
|
2020-05-05 01:30:06 +05:00
|
|
|
// TODO: add all methods
|
2024-09-01 09:31:04 +02:00
|
|
|
['GET', 'POST', 'DELETE'].forEach((method) => {
|
2022-08-09 18:03:21 +02:00
|
|
|
http[method.toLowerCase()] = (url, data, headers) => {
|
2023-10-17 19:18:19 +02:00
|
|
|
const formData =
|
|
|
|
data &&
|
|
|
|
Object.keys(data).reduce((result, key) => {
|
|
|
|
result.append(key, data[key]);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2023-10-17 19:18:19 +02:00
|
|
|
return result;
|
|
|
|
}, new FormData());
|
2019-08-31 04:07:25 +05:00
|
|
|
|
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,
|
2022-08-09 18:03:21 +02:00
|
|
|
headers,
|
2019-08-31 04:07:25 +05:00
|
|
|
body: formData,
|
2024-09-01 09:31:04 +02:00
|
|
|
credentials: 'include',
|
2019-08-31 04:07:25 +05:00
|
|
|
})
|
2020-03-25 00:15:47 +05:00
|
|
|
.then((response) =>
|
|
|
|
response.json().then((body) => ({
|
2020-02-03 18:42:31 +05:00
|
|
|
body,
|
|
|
|
isError: response.status !== 200,
|
|
|
|
})),
|
|
|
|
)
|
2019-08-31 04:07:25 +05:00
|
|
|
.then(({ body, isError }) => {
|
|
|
|
if (isError) {
|
|
|
|
throw body;
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export default http;
|