svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
918 stars 96 forks source link

Action returned by promise not put through middleware #65

Open virzen opened 7 years ago

virzen commented 7 years ago

I have a middleware which formats API responses. Is it possible to actions returned by calling .then/.catch on the request action to be put through that middleware?

Example:

action/login.js

export const logIn = (username, password) => {
  const action = {
    types: [LOG_IN, LOG_IN_SUCCESS, LOG_IN_FAIL],
    payload: {
      request: {
        // request data
      }
    },
  };

  return dispatch => dispatch(action);
};

components/LogIn.js

class LogIn extends Component {
  submitForm = (event) => {
    const { logIn } = this.props;
    const { username, password } = this.state;

    event.preventDefault(); 
    logIn(username, password)
      .catch(failAction => console.log(failAction)) // <---
  };

  // render and stuff
}

failAction is "raw" action, meaning it didn't go through the middleware. Is there a possiblity to change that, or is formatting manually each time the only option?