first commit
This commit is contained in:
39
resources/js/composables/useApi.js
Normal file
39
resources/js/composables/useApi.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { createFetch } from '@vueuse/core'
|
||||
import { destr } from 'destr'
|
||||
|
||||
export const useApi = createFetch({
|
||||
baseUrl: import.meta.env.VITE_API_BASE_URL || '/api',
|
||||
fetchOptions: {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
},
|
||||
},
|
||||
options: {
|
||||
refetch: true,
|
||||
async beforeFetch({ options }) {
|
||||
const accessToken = useCookie('accessToken').value
|
||||
if (accessToken) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
}
|
||||
}
|
||||
|
||||
return { options }
|
||||
},
|
||||
afterFetch(ctx) {
|
||||
const { data, response } = ctx
|
||||
|
||||
// Parse data if it's JSON
|
||||
let parsedData = null
|
||||
try {
|
||||
parsedData = destr(data)
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
|
||||
return { data: parsedData, response }
|
||||
},
|
||||
},
|
||||
})
|
Reference in New Issue
Block a user