Search code examples
react-reduxrtk-query

How to modify headers to RTK Query set by prepareHeaders when code splitting?


Using RTK Query code generation I have a generated slice of my API from an OpenAPI spec. Following on from that example I have extended the generated slice as described by using generatedApi.enhanceEndpoints({/**/}).

Now I want to add prepareHeaders to the slice which is typically set via fetchBaseQuery, and per the docs my use case is for adding an auth token to each request. As the createApi function is called within the generated file I'd like to avoid touching this to include custom logic.

I think I'm looking for something like generatedApi.enhancePrepareHeaders({/**/}) which does not seem to exist yet.

How do I set headers for all requests when following the code splitting approach and without touching the generated file?


Solution

  • At the moment, that is only possible by writing a custom baseQuery function wrapping the original fetchBaseQuery. From the next version of the code generator on, it will only create injectEndpoints calls and leave all the baseQuery configuration to a non-generated file.