You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
778 B
JavaScript
38 lines
778 B
JavaScript
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());
|
|
|
|
return fetch(`${Config.SERVER_BASE_URL}/api${url}`, {
|
|
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;
|