SAE-A2-TruthInquiry/truthseeker/static/js/api.js

29 lines
845 B
JavaScript

async function makeAPIRequest(endpoint, body){
return new Promise((resolve, reject)=>{
const fetchOptions = {
method: "POST",
body: new URLSearchParams(body)
}
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
resp.json().then(jsonResp=>{
if(jsonResp["error"] == 0){
resolve(jsonResp)
}else{
reject(endpoint+": "+jsonResp["msg"])
}
});
})
})
}
async function makeAPIImageRequest(endpoint, body){
return new Promise((resolve, reject)=>{
const fetchOptions = {
method: "POST",
body: new URLSearchParams(body)
}
fetch("/api/v1/"+endpoint, fetchOptions).then(resp => {
resolve(resp)
})
})
}