Raj Adroit Raj Adroit - 1 year ago 250
React JSX Question

redux-api-middleware How to do global settings for endpoint


we can create a API call like below,

export function loadUsers() {
return {
headers: { 'Content-Type': 'application/json' },
endpoint: 'http://localhost:1337/users',
method: 'GET',

Problem is, for every request, am using the common end point
, Header content type and authorization token settings.

I need a place to set those settings globally, from their official documentation am unable to find the solution. Anybody know this? or have any idea to achieve this ?

Thanks in advance..

Answer Source

In middleware you can get the access to the store state, so you can put token and other auth information into the store and then use it in middleware.

I had the same issue and had ended with implementation like this:

const callApiMiddleware = store => next => action => {
  // skip everything which is not API call
  const callAPI = action[CALL_API]
  if (typeof callAPI === 'undefined') {
    return next(action);

  // the session info from store
  const session = store.getState().session;

  // perform request
  const {endpoint, headers, method} = callAPI;
  return fetch(endpoint, {
    headers: Object.assign({
        Authorization: `Bearer ${session && session.token}`
        // you can add more default header there if you would like so
    }, headers),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download