Files
hats-app/lib/axios/useFetch.tsx
T
2025-12-19 13:57:55 +07:00

40 lines
1018 B
TypeScript

import useSWR from 'swr'
import { useSession } from 'next-auth/react';
import { type ResponseData } from '@/types/types';
import useAxiosAuth from './useAxiosAuth';
export default function useFetch(endpoint: string) {
const BASE_URL = process.env.BASE_URL_API
const { data: session } = useSession()
const fetcher = (endpoint: string) => fetch(BASE_URL + endpoint, {
headers: {
Authorization: `Bearer ${session?.user.access_token}`,
}
}).then(res => res.json())
const { data, error, isLoading, mutate } = useSWR<ResponseData<any>>(endpoint, fetcher)
return {
data: data?.data,
isLoading,
error,
mutate
}
}
export function useFetchWithAxios(url: string) {
const axios = useAxiosAuth();
const fetcher = () => axios.get(url).then(res => res.data)
const { data, error, isLoading, mutate } = useSWR<ResponseData<any>>(url, fetcher)
return {
data,
isLoading,
error,
mutate
}
}