2026-02-24 00:20:54 -06:00
|
|
|
export const $api = <T>(
|
|
|
|
|
path: string,
|
|
|
|
|
options: Parameters<typeof $fetch<T>>[1] = {}
|
|
|
|
|
) => {
|
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
const xsrfToken = useCookie('XSRF-TOKEN')
|
|
|
|
|
|
|
|
|
|
return $fetch<T>(path, {
|
|
|
|
|
baseURL: config.public.apiBase,
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
...(xsrfToken.value ? {'X-XSRF-TOKEN': xsrfToken.value} : {}),
|
|
|
|
|
},
|
2026-02-25 17:33:37 -06:00
|
|
|
onResponseError({response}) {
|
|
|
|
|
if (response.status === 401) {
|
|
|
|
|
navigateTo('/auth/login')
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-02-24 00:20:54 -06:00
|
|
|
...options,
|
|
|
|
|
})
|
|
|
|
|
}
|