2023-03-22 01:04:52 +00:00
|
|
|
async function GET(url, {method = 'GET', data} = {}){
|
2023-03-27 00:24:29 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-03-22 01:04:52 +00:00
|
|
|
async function DELETE(url, {method = 'DELETE'} = {}){
|
2023-03-16 17:12:30 -04:00
|
|
|
return call({url, method})
|
2023-03-09 13:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
2023-03-09 13:40:59 +00:00
|
|
|
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))
|
2023-03-09 13:40:59 +00:00
|
|
|
}
|
2023-04-14 03:14:33 +01:00
|
|
|
static POST(url, {method = 'POST', body}, callback){
|
|
|
|
|
call({url, method, body}).then(res => callback(res))
|
|
|
|
|
}
|
2023-03-04 21:08:08 -05:00
|
|
|
}
|