2019-06-03 03:31:38 +00:00
|
|
|
import config from 'config';
|
|
|
|
|
|
|
|
async function get(endpoint, query = {}) {
|
2019-12-04 20:58:08 +00:00
|
|
|
const curatedQuery = Object.entries(query).reduce((acc, [key, value]) => (value ? { ...acc, [key]: value } : acc), {}); // remove empty values
|
|
|
|
const q = new URLSearchParams(curatedQuery).toString();
|
2019-06-03 03:31:38 +00:00
|
|
|
|
2019-11-13 02:14:24 +00:00
|
|
|
const res = await fetch(`${config.api.url}${endpoint}?${q}`, {
|
2019-06-03 03:31:38 +00:00
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
const errorMsg = await res.text();
|
|
|
|
|
|
|
|
throw new Error(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function post(endpoint, data) {
|
|
|
|
const res = await fetch(`${config.api.url}${endpoint}`, {
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
credentials: 'same-origin',
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
const errorMsg = await res.text();
|
|
|
|
|
|
|
|
throw new Error(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
export { get, post };
|