nomilo/public/scripts/api.js

45 lines
927 B
JavaScript

const baseUrl = '/api/v1';
function apiGet(url) {
return fetch(`${baseUrl}/${url}`)
.then(res => {
if (!res.ok) {
// do something here
throw new Error('Not ok');
}
return res.json();
});
}
function apiPost(url, data) {
return fetch(`${baseUrl}/${url}`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then(res => {
if (!res.ok) {
// do something here
throw new Error('Not ok');
}
return res.json();
});
}
function getRecords(zone) {
return apiGet(`zones/${zone}/records`);
}
function createRecords(zone, record) {
return apiPost(`zones/${zone}/records`, record);
}
export {
getRecords,
createRecords,
};