svrcekmichal / redux-axios-middleware

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

Change default actions in middleware #10

Closed svrcekmichal closed 8 years ago

svrcekmichal commented 8 years ago

I would like to simplify little bit default actions onSuccess and onError. My suggestion is:

export const onSuccess = ({ action, next, response }, options) => {
  const nextAction = {
    type: getActionTypes(action, options)[1],
    payload: response,
    meta: {
      previousAction: action
    }
  };
  next(nextAction);
  return nextAction;
};

PROS:

This is the proposed error handler:

export const onError = ({ action, next, error }, options) => {
  let errorObject;
  if(error instanceof Error) {
    errorObject = {
      data: error.message,
      status: 0
    };
    if(process.env.NODE_ENV !== 'production') {
      console.log('HTTP Failure in Axios', error);
    }
  } else {
    errorObject = error;
  }
  const nextAction = {
    type: getActionTypes(action, options)[2],
    error: errorObject,
    meta: {
      previousAction: action
    }
  };

  next(nextAction);
  return nextAction;
};

Error handler will handle HTTP Failure and create generic action error if something bad occured. It will also handle logging to console if not production env is used. After little bit of googling I found that code 0 may represent various http failures.

PROS:

svrcekmichal commented 8 years ago

@nmaves Please look at this suggestion. My idea is to take this changes and your #9 and make it version 1, because package is getting quite mature and used.

nmaves commented 8 years ago

I think I like all of this. Having access to the full response in one place is a good idea as everyone is already familiar with an axios response.