From 27b17dfacd3bb6715a080f6712bc7041063a92d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 8 Oct 2024 15:49:02 +0200 Subject: [PATCH] docs: update to new `ofetch` headers for interceptors (#29118) --- docs/2.guide/4.recipes/3.custom-usefetch.md | 10 ++-------- docs/3.api/2.composables/use-fetch.md | 4 ++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/2.guide/4.recipes/3.custom-usefetch.md b/docs/2.guide/4.recipes/3.custom-usefetch.md index 1391720db3..a0ac6d7e29 100644 --- a/docs/2.guide/4.recipes/3.custom-usefetch.md +++ b/docs/2.guide/4.recipes/3.custom-usefetch.md @@ -31,14 +31,8 @@ export default defineNuxtPlugin((nuxtApp) => { baseURL: 'https://api.nuxt.com', onRequest({ request, options, error }) { if (session.value?.token) { - const headers = options.headers ||= {} - if (Array.isArray(headers)) { - 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}` - } + // note that this relies on ofetch >= 1.4.0 - you may need to refresh your lockfile + options.headers.set('Authorization', `Bearer ${session.value?.token}`) } }, async onResponseError({ response }) { diff --git a/docs/3.api/2.composables/use-fetch.md b/docs/3.api/2.composables/use-fetch.md index 9e24b1766a..8d6075f044 100644 --- a/docs/3.api/2.composables/use-fetch.md +++ b/docs/3.api/2.composables/use-fetch.md @@ -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', { onRequest({ request, options }) { // Set the request headers - options.headers = options.headers || {} - options.headers.authorization = '...' + // note that this relies on ofetch >= 1.4.0 - you may need to refresh your lockfile + options.headers.set('Authorization', '...') }, onRequestError({ request, options, error }) { // Handle the request errors