checklist/resources/js/useFetch.js
Juan Felipe Zapata Moreno 9c6eeb5fb3 Commit Inicial
2025-08-05 09:52:38 -06:00

41 lines
1.1 KiB
JavaScript

const getToken = () => {
return document.head.querySelector("[name~=csrf-token][content]").content;
}
/**
* Fetch personalizado para recibir respuestas y errores desde laravel
*
* Es necesario enviar la información con las funciones successFetch o errorFetch
* provistas en el controlador padre en laravel.
*/
export default async function useFetch(url, data = {}, method = 'get') {
let response = null;
let options = (method == 'get') ?
{
method: method,
cache: 'no-cache'
} : {
method: method,
cache: 'no-cache',
credentials: "same-origin",
headers: {
'Content-Type': 'application/json',
"X-CSRF-Token": getToken()
},
body: JSON.stringify(data)
}
await fetch(url, options)
.then((res) => res.json())
.then((res) => {
if(res.status != 200) {
Notify.error(res.message);
throw new Error(res.message);
} else {
response = res;
}
});
return response;
}