2019-06-03 03:31:38 +00:00
|
|
|
import config from 'config';
|
|
|
|
|
2019-11-13 02:14:24 +00:00
|
|
|
import queryString from 'query-string';
|
|
|
|
|
2019-06-03 03:31:38 +00:00
|
|
|
async function get(endpoint, query = {}) {
|
2019-11-13 02:14:24 +00:00
|
|
|
const q = queryString.stringify(query);
|
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 };
|