24 lines
639 B
TypeScript
24 lines
639 B
TypeScript
import axios, { type AxiosRequestConfig } from "axios"
|
|
|
|
export function useGet<T>(path: string, options: AxiosRequestConfig = {}) {
|
|
console.log('useGet')
|
|
return useAsyncData(path, async () => {
|
|
if (import.meta.client) {
|
|
const auth = useAuth()
|
|
const getToken = auth.getToken.value
|
|
const token = await getToken()
|
|
|
|
options.headers = {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
const result = await axios.get<T>(path, options)
|
|
return result.data
|
|
} else {
|
|
const headers = useRequestHeaders(['cookie'])
|
|
options.headers = headers
|
|
const result = await axios.get<T>(path, options)
|
|
return result.data
|
|
}
|
|
})
|
|
}
|