Just a small suggestion. Would you like to try writing it like this? It might help.
export const useRoomStore = defineStore('roomStore', () => {
const body = ref<IRoomBody>(ROOM_BODY_DEFAULT)
const { data, execute } = useFetch('/api/rooms', {
key: 'rooms',
method: 'post',
body,
immediate: false,
onRequest ({ options }) {
let bearer = 'Bearer '
const token = localStorage.getItem('access_token')
if (!token) {
console.log('token ERROR GO TO LOGIN', token)
} else {
bearer = bearer + token
}
const headers = new Headers(options.headers)
headers.set('Content-Type', 'application/json')
headers.set('Authorization', bearer)
options.headers = headers
},
})
const fetch = () => execute()
return {
body,
data,
fetch,
}
})
And if you’re interested in learning more about how to use $fetch
, useFetch
, and useAsyncData
, this video might be helpful.