Interceptors allow you to define custom functions that will be used by sanctumClient during API calls. Here are some examples of what you can do with it:
X-Localization, Accept-Language, etc)This module provides special hooks to define your interceptors:
sanctum:requestsanctum:responseYou can set up a new plugin and describe the behaviour of handling each outgoing request and incoming response.
Here is an example of the plugin that writes a log entry for each request and response:
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('sanctum:request', (app, ctx, logger) => {
logger.info('Sanctum request hook triggered', ctx.request)
})
nuxtApp.hook('sanctum:response', (app, ctx, logger) => {
logger.info('Sanctum response hook triggered', ctx.request)
})
}
Each interceptor receives 3 arguments:
app - an instance of the current NuxtAppctx - FetchContext instance for the current operation with access to request, response, and options (query, headers, etc)logger - an instance of a Consola logger used by the module (will be prefixed with nuxt-auth-sanctum)