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

41 lines
855 B
JavaScript
Raw Normal View History

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
['GET', 'POST', 'DELETE'].forEach((method) => {
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
return fetch(`${Config.SERVER_BASE_URL}/api${url}`, {
2019-08-31 04:07:25 +05:00
method,
headers,
2019-08-31 04:07:25 +05:00
body: formData,
credentials: 'include',
2019-08-31 04:07:25 +05:00
})
2020-03-25 00:15:47 +05:00
.then((response) =>
response.json().then((body) => ({
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;