import type { UseFetchOptions } from '#app'import {useAuth, useFetch} from "#imports";export async function useApiFetch<T>(url: string | (() => string), options: UseFetchOptions<T> = {}, authAccess: boolean = true) { const {token} = useAuth() const config = useRuntimeConfig() await useFetch(config.public.apiDomain + 'sanctum/csrf-cookie', { credentials: 'include', headers: { Accept: 'application/json', } }) const laravelCookie = useCookie('XSRF-TOKEN') const defaults: UseFetchOptions<T> = { baseURL: config.public.apiDomain + 'api/v1/', headers: { Accept: 'application/json', 'X-XSRF-TOKEN': laravelCookie.value as string, ...(authAccess && {Authorization: token.value ? token.value : ''}), }, credentials: 'include', ...options, } return useFetch(url, defaults)}
<script setup lang="ts">const composableLoaded = ref(false)const loadMyComposable = async () => { try { const { myComposable } = await import('./path/to/myComposable') myComposable() composableLoaded.value = true } catch (error) { console.error("...", error) composableLoaded.value = false }}onMounted(() => { loadMyComposable()})</script>
const { myComposable } = await import('./path/to/myComposable')