soundbirder/public/javascripts/api-client.js

49 lines
1.0 KiB
JavaScript
Raw Normal View History

const API_VERSION = "0";
const API_URL = `/api/${API_VERSION}`;
const TOKEN = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
async function get(url, params) {
return await axios.get(url, {
params: params,
headers: {
'X-CSRF-Token': TOKEN
}
}).then(response => {
return response.data;
}).catch(error => {
throw error;
});
}
async function query(endpoint, params) {
return await get(`${API_URL}/${endpoint}`, params);
}
function getQuizz(lat, lng) {
return query('game/quizz', { lat, lng })
.then(data => {
console.log(data);
return data;
}).catch(error => {
throw error;
});
}
function checkResponse(speciesId) {
return query('game/check', {
species: speciesId
}).then(response => {
return response.data.correct
}).catch(error => {
throw error;
});
}
const client = {
getQuizz,
checkResponse
}
export default client;