Advanced
Hooks
nuxt-open-fetch provides hooks to extend the functionality of the fetch clients
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.
plugins/openFetch.ts
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:
plugins/openFetch.ts
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:
server/plugins/openFetch.ts
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('openFetch:onRequest', (ctx) => {
ctx.options.headers.set('X-Custom-Header', 'MyValue-nitro')
})
})
If you need more fine-grained control of the client, you can create custom clients instead.
Available hooks:
openFetch:onRequest
openFetch:onRequestError
openFetch:onResponse
openFetch:onResponseError
openFetch:onRequest:<clientName>
openFetch:onRequestError:<clientName>
openFetch:onResponse:<clientName>
openFetch:onResponseError:<clientName>