Files
oficial/app/resources/views/layout/body/scripts/api.blade.php
aldarien 307f2ac7d7 feature/cierres (#25)
Varios cambios

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #25
2025-07-22 13:18:00 +00:00

33 lines
1.1 KiB
PHP

<script>
class APIClient {
static getApiKey() {
return '{{md5($API_KEY)}}{{($login->isIn()) ? $login->getSeparator() . $login->getToken() : ''}}'
}
static fetch(url, options=null, showErrors=false) {
if (options === null) {
options = {}
}
if (!Object.hasOwn(options, 'headers')) {
options['headers'] = {}
}
if (!Object.hasOwn(options['headers'], 'Authorization')) {
options['headers']['Authorization'] = `Bearer ${APIClient.getApiKey()}`
}
return fetch(url, options).then(response => {
if (response.ok) {
return response
}
throw new Error(JSON.stringify({code: response.status, message: response.statusText, url}))
}).catch(error => {
if (showErrors) {
console.error(error)
}
})
}
}
function fetchAPI(url, options=null, showErrors=false) {
return APIClient.fetch(url, options, showErrors)
}
</script>