svrcekmichal / redux-axios-middleware

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

Interceptor Response not respond to anything #52

Closed stefensuhat closed 7 years ago

stefensuhat commented 7 years ago

Hi, Thanks for this great package!. Need ask or maybe I doing it wrong.

I'm trying to setup the axiosmiddleware with interceptor. The interceptor request is working properly but the interceptor response not working.

here is my code:


function interceptorRequest(dispatch, config) {
    const token = cookie.load('_t');
    const configuration = config;
    if (token) {
        configuration.headers.Authorization = `Bearer ${cookie.load('_t')}`;
    }

    return configuration;
}

function interceptorResponse(dispatch, response) {
    console.log(dispatch);
    console.log(response);
    console.log(error);
}

export const client = axios.create({
    baseURL: API_URL,
    headers: {
        Accept: 'application/json',
    },
});

export const clientOptions = {
    interceptors: {
        request: [interceptorRequest],
        response: [interceptorResponse],
    },
};

// store.js

// other import
import { client, clientOptions } from '../utils/axios';

const reduxRouterMiddleware = routerMiddleware(history);

const middlewares = [
    thunk,
    axiosMiddleware(client, clientOptions),
    reduxRouterMiddleware,
];

const store = compose(applyMiddleware(...middlewares))(createStore)(reducer);

export default store;

my question:

  1. my console.log (at response interceptor) never return anything. Why it not return anything?
  2. how can I setup the interceptors with error? For example:
    
    // Add a request interceptor
    axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
    }, function (error) {
    // Do something with request error
    return Promise.reject(error);
    });

// Add a response interceptor axios.interceptors.response.use(function (response) { // Do something with response data return response; }, function (error) { // Do something with response error return Promise.reject(error); });



Thanks!
nmaves commented 7 years ago

I think your issue might be with your params to your interceptors.

https://github.com/svrcekmichal/redux-axios-middleware#middleware-options

Try the following, not the first param.

function interceptorResponse({ dispatch, getState, getAction }, response) {
    console.log(dispatch);
    console.log(response);
    console.log(error);
}
stefensuhat commented 7 years ago

@nmaves thanks for you answer. actually your answer is no difference with mine except you destructuring the variables at top. Success response will console out. But if the response is an error it won't console it out.

nmaves commented 7 years ago

Are you setting returnRejectedPromiseOnError to true?

svrcekmichal commented 7 years ago

@stefensuhat by default interceptors are handling only success response. To handle also error response you must not add it as function, but as object with keys error/success

export const clientOptions = {
    interceptors: {
        response: [{ success: interceptorSuccessResponse, error: interceptorErrorResponse }],
    },
};
Palatnyi commented 7 years ago

@svrcekmichal do you think read me file has to be updated with description of how set interceptors? In current read.me doesn't contains information on how to set "error" interceptors properly. I spent some time to debug middleware to find out how set it. thanks. it's really nice middleware =)

svrcekmichal commented 7 years ago

@Palatnyi I believed it was mentioned somewhere. If someone can make PR, it would be great, if not I'll look at this later next week. Thanks

stefensuhat commented 7 years ago

@svrcekmichal perfecto! you saved my entire life 💃

nmaves commented 7 years ago

We have updated the readme in #53. Thank you @Palatnyi

Palatnyi commented 7 years ago

@nmaves Thanks for merging my commit. This is my debut in in open source =)