lndg/gui/static/api.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

async function GET(url, {method = 'GET', data} = {}){
if(!data.limit) data.limit = 100000
2023-03-28 13:24:57 -04:00
if(!data.format) data.format = 'json'
2023-03-12 18:08:26 -04:00
return call({url, method, data})
2023-03-04 21:08:08 -05:00
}
2023-03-12 18:08:26 -04:00
async function POST(url, {method = 'POST', body}){
2023-03-16 17:12:30 -04:00
return call({url, method, body})
2023-03-04 21:08:08 -05:00
}
2023-03-12 18:08:26 -04:00
async function PUT(url, {method = 'PUT', body}){
2023-03-16 17:12:30 -04:00
return call({url, method, body})
2023-03-04 21:08:08 -05:00
}
2023-03-12 18:08:26 -04:00
async function PATCH(url, {method = 'PATCH', body}){
2023-03-16 17:12:30 -04:00
return call({url, method, body})
2023-03-04 22:35:36 -05:00
}
async function DELETE(url, {method = 'DELETE'} = {}){
2023-03-16 17:12:30 -04:00
return call({url, method})
}
2023-03-12 18:08:26 -04:00
async function call({url, method, data, body, headers = {'Content-Type':'application/json'}}){
2023-03-16 17:12:30 -04:00
if(url.charAt(url.length-1) != '/') url += '/'
2023-03-19 19:01:17 +00:00
if(method != 'GET') headers['X-CSRFToken'] = document.getElementById('api').dataset.token
const result = await fetch(`api/${url}${data ? '?': ''}${new URLSearchParams(data).toString()}`, {method, body: JSON.stringify(body), headers})
return result.json()
}
class Sync{
2023-03-12 18:08:26 -04:00
static PUT(url, {method = 'PUT', body}, callback){
2023-03-16 17:12:30 -04:00
call({url, method, body}).then(res => callback(res))
}
static POST(url, {method = 'POST', body}, callback){
call({url, method, body}).then(res => callback(res))
}
2023-03-04 21:08:08 -05:00
}