Api en archivo separado
This commit is contained in:
@ -1,33 +1,9 @@
|
|||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.3/semantic.min.js" integrity="sha512-gnoBksrDbaMnlE0rhhkcx3iwzvgBGz6mOEj4/Y5ZY09n55dYddx6+WYc72A55qEesV8VX2iMomteIwobeGK1BQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.3/semantic.min.js" integrity="sha512-gnoBksrDbaMnlE0rhhkcx3iwzvgBGz6mOEj4/Y5ZY09n55dYddx6+WYc72A55qEesV8VX2iMomteIwobeGK1BQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
@include('layout.body.scripts.api')
|
||||||
class APIClient {
|
|
||||||
static fetch(url, options=null, showErrors=false) {
|
<script>
|
||||||
return fetchAPI(url, options, showErrors)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function fetchAPI(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 {{md5($API_KEY)}}{{($login->isIn()) ? $login->getSeparator() . $login->getToken() : ''}}'
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
const datatables_defaults = {
|
const datatables_defaults = {
|
||||||
language: {
|
language: {
|
||||||
emptyTable: 'No hay datos disponibles',
|
emptyTable: 'No hay datos disponibles',
|
||||||
|
28
app/resources/views/layout/body/scripts/api.blade.php
Normal file
28
app/resources/views/layout/body/scripts/api.blade.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<script>
|
||||||
|
class APIClient {
|
||||||
|
static fetch(url, options=null, showErrors=false) {
|
||||||
|
return fetchAPI(url, options, showErrors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function fetchAPI(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 {{md5($API_KEY)}}{{($login->isIn()) ? $login->getSeparator() . $login->getToken() : ''}}'
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
Reference in New Issue
Block a user