1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/client/src/api/http.js

40 lines
855 B
JavaScript
Executable file

import { fetch } from 'whatwg-fetch';
import Config from '../constants/Config';
const http = {};
// TODO: add all methods
['GET', 'POST', 'DELETE'].forEach((method) => {
http[method.toLowerCase()] = (url, data, headers) => {
const formData =
data &&
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,
credentials: 'include',
})
.then((response) =>
response.json().then((body) => ({
body,
isError: response.status !== 200,
})),
)
.then(({ body, isError }) => {
if (isError) {
throw body;
}
return body;
});
};
});
export default http;