This module provides several Nuxt/Nitro runtime hooks that allow you to extend the functionality of the fetch clients. These hooks can be used to modify requests, handle responses, or log information about the fetch operations.
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('openFetch:onRequest', (ctx) => {
ctx.options.headers.set('X-Custom-Header', 'MyValue')
})
})
You may also hook into only specific clients:
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('openFetch:onRequest:myClient', (ctx) => {
ctx.options.headers.set('X-Custom-Header', 'MyValue')
})
})
For nitro, you can use the same hooks:
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('openFetch:onRequest', (ctx) => {
ctx.options.headers.set('X-Custom-Header', 'MyValue-nitro')
})
})
Available hooks:
openFetch:onRequestopenFetch:onRequestErroropenFetch:onResponseopenFetch:onResponseErroropenFetch:onRequest:<clientName>openFetch:onRequestError:<clientName>openFetch:onResponse:<clientName>openFetch:onResponseError:<clientName>