nomilo/public/scripts/api.js

45 lines
927 B
JavaScript
Raw Normal View History

2022-04-29 02:29:10 +00:00
const baseUrl = '/api/v1';
function apiGet(url) {
2022-04-29 02:33:00 +00:00
return fetch(`${baseUrl}/${url}`)
.then(res => {
if (!res.ok) {
// do something here
throw new Error('Not ok');
}
return res.json();
2022-04-29 16:04:12 +00:00
});
2022-04-29 02:29:10 +00:00
}
2023-02-25 01:53:10 +00:00
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();
});
}
2022-04-29 02:29:10 +00:00
function getRecords(zone) {
2022-04-29 16:04:12 +00:00
return apiGet(`zones/${zone}/records`);
2022-04-29 02:29:10 +00:00
}
2023-02-25 01:53:10 +00:00
function createRecords(zone, record) {
return apiPost(`zones/${zone}/records`, record);
}
2022-04-29 02:29:10 +00:00
export {
2022-04-29 02:33:00 +00:00
getRecords,
2023-02-25 01:53:10 +00:00
createRecords,
2022-04-29 02:29:10 +00:00
};