Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for global interceptors via app.config.ts #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Velman04
Copy link

@Velman04 Velman04 commented Feb 5, 2025

Summary

This PR introduces global request/response interceptors, allowing developers to define them in app.config.ts, similar to how it's handled in nuxt-auth-sanctum.

Why this change?

Currently, developers must specify onRequest and onResponse manually for every useSanctumFetch() call. This PR simplifies API handling by enabling global interceptors, reducing repetitive code and improving flexibility.

Changes Introduced

  • Added support for interceptors.onRequest and interceptors.onResponse inside app.config.ts
  • Integrated these interceptors into createFetchService(), ensuring they apply globally before any per-request hooks
  • Fully compatible with existing request-level onRequest and onResponse handlers

Example Usage

Developers can now define interceptors globally in app.config.ts:

export default defineAppConfig({
  laravelSanctum: {
    interceptors: {
      onRequest: async (app, ctx) => {
        ctx.options.headers.set('X-Localization', app.$i18n.localeProperties.value.code);
      },
      onResponse: async (app, ctx) => {
        console.log(`[Response] ${ctx.request}`, ctx.response);
      }
    },
  },
});

Benefits

✅ Automatically applies custom headers to all requests
✅ Enables global logging/telemetry for API interactions
✅ Keeps API request logic cleaner by reducing duplication

…via `app.config.ts`. This allows developers to define interceptors at the application level instead of specifying them for each `createFetchService()` call individually.

- Introduced `interceptors.onRequest` and `interceptors.onResponse` support in `app.config.ts`
- Global interceptors execute before per-request interceptors (if defined)
- Fully backward-compatible with existing request-level hooks
- Enhances API customization, such as setting custom headers (`X-Localization`, `Authorization`) or logging requests/responses globally
@manchenkoff
Copy link

You may also consider adding AppConfig type augmentation to provide an autocomplete for developers, like this - templates.ts.

@Velman04
Copy link
Author

Velman04 commented Feb 6, 2025

@manchenkoff thanks for the suggestion! I’ve added AppConfig type augmentation to enable autocomplete support for developers.

Also, I really appreciate your work on nuxt-auth-sanctum—it plays a significant role in the development of similar modules and serves as a great reference for improving authentication in Nuxt projects. 🚀

@hkp22
Copy link
Member

hkp22 commented Feb 6, 2025

@Velman04

Thanks for the PR! I really appreciate the contribution, and I learned something new about type templates in templates.ts.

I also noticed that you've introduced a useSanctumAppConfig. The module already has a useSanctumOptions composable, which provides similar functionality, so there's no need for the additional composable.

Regarding the configuration example:

export default defineAppConfig({
  laravelSanctum: {
    interceptors: {
      onRequest: async (app, ctx) => {
        ctx.options.headers.set('X-Localization', app.$i18n.localeProperties.value.code);
      },
      onResponse: async (app, ctx) => {
        console.log(`[Response] ${ctx.request}`, ctx.response);
      }
    },
  },
});

Currently, this only provides global options for onRequest and onResponse. However, ofetch supports additional options that users might want to set globally, such as timeout, headers.

Wouldn't it be better to simplify and extend this by introducing a globalFetchOptions property?

Example:

laravelSanctum: {
  globalFetchOptions: FetchOptions
}

This would offer more flexibility, allowing users to define a broader set of global options for ofetch rather than being limited to just request/response interceptors. What do you think? Looking forward to your thoughts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants