N
Nuxt13mo ago
Zerro

How to create custom $fetch for server side only

Hi all! I'm trying to think of way to create custom $fetch function to include authorization header for JWT token. JWT token is obtained from another backend API that I have. Currently I'm storing the JWT token in server side of nuxt app and only exposes session id to the client side of nuxt app through cookie. When I'm making a request from server side of nuxt app to my other backend API, I want to include this JWT token that i stored in the session (sqlite db). I've seen some examples that used nuxt plugin to define custom $fetch during vue app initialization but I wasn't sure how to achieve this only for server side $fetch (it has to be server side because that's where i store the jwt token).
1 Reply
kleinpetr
kleinpetr11mo ago
I have the same question, becuase Nitro plugins work in a different way. I've been trying to create a util which would export $customFetch using $fetch.create() but it says the $fetch is undefined. So is there any recommended way how to register a custom fetch within the Nitro context? Thanks my current workaround is to use server util for example export const $customFetch = () => $fetch.create().. and the usage looks like this const data = $customFetch()('/path')

Did you find this page helpful?