43 lines
1016 B
JavaScript
43 lines
1016 B
JavaScript
import config from 'config';
|
|
|
|
async function get(endpoint, query = {}) {
|
|
const curatedQuery = Object.entries(query).reduce((acc, [key, value]) => (value ? { ...acc, [key]: value } : acc), {}); // remove empty values
|
|
const q = new URLSearchParams(curatedQuery).toString();
|
|
|
|
const res = await fetch(`${config.api.url}${endpoint}?${q}`, {
|
|
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 };
|