Use Custom Fetch Composable
This example shows a convenient wrapper for the useFetch composable from nuxt. It allows you to customize the fetch request with default values and user authentication token.
customFetch.ts
Open docsexport default defineNuxtPlugin((nuxtApp) => {
const userAuth = useCookie('token')
const config = useRuntimeConfig()
const $customFetch = $fetch.create({
baseURL: config.baseUrl as string ?? 'https://api.nuxt.com',
onRequest({ request, options, error }) {
if (userAuth.value) {
// Add Authorization header
options.headers.set('Authorization', `Bearer ${userAuth.value}`)
}
},
onResponse({ response }) {
// response._data = new myBusinessResponse(response._data)
},
async onResponseError({ response }) {
if (response.status === 401) {
await nuxtApp.runWithContext(() => navigateTo('/login'))
}
},
})
// Expose to useNuxtApp().$customFetch
return {
provide: {
customFetch: $customFetch,
},
}
})