forked from DebaucheryLibrarian/traxxx
101 lines
1.8 KiB
JavaScript
101 lines
1.8 KiB
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',
|
|
});
|
|
|
|
const contentTypes = res.headers.get('content-type');
|
|
|
|
if (res.ok && contentTypes?.includes('application/json')) {
|
|
return res.json();
|
|
}
|
|
|
|
if (res.ok) {
|
|
return null;
|
|
}
|
|
|
|
const errorMsg = await res.text();
|
|
|
|
throw new Error(errorMsg);
|
|
}
|
|
|
|
async function post(endpoint, data) {
|
|
const res = await fetch(`${config.api.url}${endpoint}`, {
|
|
method: 'POST',
|
|
mode: 'cors',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
const contentTypes = res.headers.get('content-type');
|
|
|
|
if (res.ok && contentTypes?.includes('application/json')) {
|
|
return res.json();
|
|
}
|
|
|
|
if (res.ok) {
|
|
return null;
|
|
}
|
|
|
|
const errorMsg = await res.text();
|
|
|
|
throw new Error(errorMsg);
|
|
}
|
|
|
|
async function del(endpoint) {
|
|
const res = await fetch(`${config.api.url}${endpoint}`, {
|
|
method: 'DELETE',
|
|
mode: 'cors',
|
|
credentials: 'same-origin',
|
|
});
|
|
|
|
if (res.ok) {
|
|
return true;
|
|
}
|
|
|
|
const errorMsg = await res.text();
|
|
|
|
throw new Error(errorMsg);
|
|
}
|
|
|
|
async function graphql(query, variables = null) {
|
|
const res = await fetch('/graphql', {
|
|
method: 'POST',
|
|
mode: 'cors',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
query,
|
|
variables,
|
|
}),
|
|
});
|
|
|
|
if (res.ok) {
|
|
const { data } = await res.json();
|
|
|
|
return data;
|
|
}
|
|
|
|
const errorMsg = await res.text();
|
|
|
|
throw new Error(errorMsg);
|
|
}
|
|
|
|
export {
|
|
get,
|
|
post,
|
|
del,
|
|
graphql,
|
|
};
|