mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
docs: update to new ofetch
headers for interceptors (#29118)
This commit is contained in:
parent
462242a002
commit
27b17dfacd
@ -31,14 +31,8 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
baseURL: 'https://api.nuxt.com',
|
baseURL: 'https://api.nuxt.com',
|
||||||
onRequest({ request, options, error }) {
|
onRequest({ request, options, error }) {
|
||||||
if (session.value?.token) {
|
if (session.value?.token) {
|
||||||
const headers = options.headers ||= {}
|
// note that this relies on ofetch >= 1.4.0 - you may need to refresh your lockfile
|
||||||
if (Array.isArray(headers)) {
|
options.headers.set('Authorization', `Bearer ${session.value?.token}`)
|
||||||
headers.push(['Authorization', `Bearer ${session.value?.token}`])
|
|
||||||
} else if (headers instanceof Headers) {
|
|
||||||
headers.set('Authorization', `Bearer ${session.value?.token}`)
|
|
||||||
} else {
|
|
||||||
headers.Authorization = `Bearer ${session.value?.token}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async onResponseError({ response }) {
|
async onResponseError({ response }) {
|
||||||
|
@ -50,8 +50,8 @@ You can also use [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interce
|
|||||||
const { data, status, error, refresh, clear } = await useFetch('/api/auth/login', {
|
const { data, status, error, refresh, clear } = await useFetch('/api/auth/login', {
|
||||||
onRequest({ request, options }) {
|
onRequest({ request, options }) {
|
||||||
// Set the request headers
|
// Set the request headers
|
||||||
options.headers = options.headers || {}
|
// note that this relies on ofetch >= 1.4.0 - you may need to refresh your lockfile
|
||||||
options.headers.authorization = '...'
|
options.headers.set('Authorization', '...')
|
||||||
},
|
},
|
||||||
onRequestError({ request, options, error }) {
|
onRequestError({ request, options, error }) {
|
||||||
// Handle the request errors
|
// Handle the request errors
|
||||||
|
Loading…
Reference in New Issue
Block a user