You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
707 B

import Cookies from 'js-cookie';
const fecher = async (url: string, token: string) => {
return await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization":token
},
});
}
export const useFetchWithAuth = async (url: string) => {
url= process.env.API_URL+"/api/"+url+"/";
const token = Cookies.get('token') || "";
let data;
let error;
try{
const response = await fecher(url, token)
if (response.ok){
data = await response.json();
}else {
console.log(url)
error = "Servidor: "+ ((await response.json()).trace);
}
}catch (e){
error = "Cliente: "+e;
}
return {
data,
error
}
}