简体   繁体   中英

How to access url from prepareHeaders in redux toolkit

In order to add specific header, I need to check what the api/query url is. There isn't any param based on redux toolkit documentation.

export const gatewayApi = createApi({
    reducerPath: 'gatewayApi',
    tagTypes: [
        'Country',
        'Users',
    ],
    baseQuery: fetchBaseQuery({
        baseUrl: '/api/',
        prepareHeaders: headers => {
            headers.set('content-type', 'application/json');
            const { auth } = JSON.parse(
            localStorage.getItem('user'),
            );

            const { accessToken } = JSON.parse(
             localStorage.getItem('token'),
            );

            const queryUrl = '**** TODO ****';

            if (isTokenAuth(queryUrl)) {
             headers.set('authorization', `${accessToken}`);
            } else if (auth) {
            headers.set('authorization', `${auth}`);
            }

            return headers;
        },
    }),
    endpoints: () => ({}),
});

No, there isn't. You can also just set headers from your endpoint's query function though:

myEndpoint: build.query({
  query(arg) {
    return {
      url: ...,
      headers: { foo: "bar" }
    }
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM