A
A
Andrew2017-03-15 18:09:39
React
Andrew, 2017-03-15 18:09:39

How to add a header to the CALL_API object from redux-api-middleware through your middleware?

action

import { CALL_API } from 'redux-api-middleware';

export const MOVIES_GET_SUCCESS = 'MOVIES_GET_SUCCESS';

export const getMovies = () => {
  return {
    [CALL_API]: {
      endpoint: 'http://localhost:3005/api/movies',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      },
      types: [type: 'REQUEST', MOVIES_GET_SUCCESS, 'FAILURE']
    }
  };
};

My Middleware
export default store => next => action => {
  if (typeof action !== 'function' && action.type.search(/REQUEST/) !== -1) {
    if (Object.keys(action).indexOf('headers') !== -1) {
      // здесь Action который я написал выше
      // console.log(action) -> Object {type: "REQUEST", payload: undefined, meta: undefined}
    }
    console.log(action);
  }
  next(action);
};

Before that, I passed the token { authorization: token } parameter everywhere where I dispatched the action, and I already processed the request on the server, but this is a bad solution, I think everything would be much more convenient through the middleware, add { authorization: to all actions with type: 'REQUEST' 'token' } where is the token pulled through the Store, how can I do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2017-03-15
@undefined_title

https://gist.github.com/benpickles/b465db90ca11adc9ea00

M
Mikhail Osher, 2017-03-15
@miraage

{
  [CALL_API]: {
    ...
    headers: { 'Content-Type': 'application/json', 'Authorization': 'token' }
    ...
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question