Search code examples
rtk-query

How can we send the request id that automatically gets generated by the RTK query as a header x-request-id?


I would like to access requestId that RTK generate for maintaining the cache for every request it fires and send it as a header under x-request-id.

Here's my slice

export const api = createApi({
    reducerPath: 'api',
    baseQuery: fetchBaseQuery({
        baseUrl: 'http://localhost:9000',
        prepareHeaders: (headers, { requestId }) => {
            headers.set('x-request-id', requestId)
        },
    }),
    endpoints: (builder) => ({
        getPage: builder.query<PageResponse, PageRequest>({
            query: (pageRequest) => {
                return {
                    url: '/api/4/page/fetch',
                    body: pageRequest,
                    method: 'POST',
                };
            },
        }),
    })
});

Is there a way we can access the requestId to be sent along in request headers? See type UseQueryResult<T> here


Solution

  • No, at this moment that is not being passed into the baseQuery (you can see relevant code here). But it's a neat idea. A pull request would be welcome.