mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
23 lines
651 B
TypeScript
23 lines
651 B
TypeScript
import useSWR from 'swr'
|
|
import { useSession } from 'next-auth/react'
|
|
import { type ResponseData } from "@/types/types"
|
|
|
|
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
|
|
}
|
|
} |