Skip to content

Hooks ​

Hooks run inside your app – in a Nuxt plugin or a Nitro plugin – and see every request the module sends, whichever endpoint it belongs to. Each runtime hook receives the endpoint ID, so one handler covers all endpoints and branches where they differ. Use hooks for behavior that spans endpoints, such as attaching a per-visitor token on the server, setting a retry count, or logging failures centrally.

To change a single call instead, reach for an interceptor.

For more information on how to work with hooks, see the Nuxt documentation.

Available Hooks ​

Hook NameRuns InArgumentsDescription
api-party:extendBuildoptionsCalled at module initialization, before the module reads the endpoints. Allows modifying them.
api-party:requestNuxtctx, endpointIdCalled before each request a composable sends, in the browser and during SSR.
api-party:responseNuxtctx, endpointIdCalled after each response a composable receives, error responses included.
api-party:requestNitroctx, event, endpointIdCalled before each request the server proxy or a server import sends to the API.
api-party:responseNitroctx, event, endpointIdCalled after each response the server proxy or a server import receives from the API.

ctx is the ofetch context of the request, the same object an interceptor receives: ctx.request, ctx.options and, in a response hook, ctx.response.

Module Hook ​

api-party:extend runs once, at module initialization, and may await. Use it to modify the endpoint configuration before the module is fully initialized. For example, log the configured endpoints:

ts
export default defineNuxtConfig({
  modules: ['nuxt-api-party'],

  hooks: {
    'api-party:extend': async (options) => {
      console.log(`Configured endpoints:`, options.endpoints)
    },
  },
})

Nuxt Runtime Hooks ​

Register Nuxt runtime hooks in a Nuxt plugin. They fire for every $myApi and useMyApiData call: in the browser, and on the server during SSR.

ctx.options is the place for defaults that span endpoints, such as headers, retry and retryDelay:

ts
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('api-party:request', (ctx, endpointId) => {
    // Add a unique request ID to each request
    ctx.options.headers.set('X-Request-Id', crypto.randomUUID())

    if (endpointId === 'jsonPlaceholder') ctx.options.retry ??= 3
  })
})

The hook runs before every attempt, a retry included. Set a default with ??=: a plain assignment to retry resets the count on each attempt, so a failing request retries without end, and it overrides the option of a call.

A timeout set here reaches neither useMyApiData, which takes its own timeout option, nor a request through the server proxy during SSR, which takes it from a Nitro hook.

Client-Side Execution

Nuxt hooks run in the browser as well, and a request from the browser reaches the server proxy like any visitor's. Without credential forwarding, an Authorization header set here never reaches the API. Attach credentials in a Nitro hook instead.

Nitro Runtime Hooks ​

Register Nitro runtime hooks in a server plugin. They fire for every request the server proxy forwards and for every server import call, and are the place for credentials that depend on the visitor:

ts
export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('api-party:request', async (ctx, event, endpointId) => {
    if (endpointId !== 'myApi' || !event) return

    // Fetch a token for the visitor and attach it to the request
    const token = await getUserToken(event)
    ctx.options.headers.set('Authorization', `Bearer ${token}`)
  })
})

event is the h3 event of the request the proxy is answering. A server import has no request to answer, so event is undefined there – which also tells the two apart.

A request hook that throws an h3 error, such as throw createError({ statusCode: 401 }), answers the proxy request with that error and the API is never called.

A timeout set on ctx.options bounds the proxy's wait for the API's response headers. When it runs out, the proxy answers 502.

Response Body in the Proxy

The server proxy streams the API's response through, so in a Nitro response hook for a proxy request, ctx.response._data is a ReadableStream, not the parsed body. Read status and headers from ctx.response, and set additional response headers on event. The proxy copies the API's headers after the hook, so to replace one of them, answer the request in the hook – with send(event, body) or sendRedirect(event, location) – and the proxy leaves the response alone. A server import parses the body as usual.

Released under the MIT License.