initial commit
This commit is contained in:
23
web/composables/useGet.ts
Normal file
23
web/composables/useGet.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user