Put error response interceptor while abbreviation-axios-middleware

I have a problem with https://github.com/svrcekmichal/redux-axios-middleware .

I want to set an interceptor response (error). But he cannot successfully configure it.

Here is my code:

function interceptorResponse({ dispatch, getState, getAction }, response) { console.log(response); } export const client = axios.create({ baseURL: API_URL, headers: { Accept: 'application/json', }, }); export const clientOptions = { interceptors: { request: [interceptorRequest], response: [interceptorResponse], }, }; 

console.log(response) answers only if the response is 200 . How can I configure it to accept an error response?

I tried installing it like this:

  function interceptorResponse({ dispatch, getState, getAction }) { return response => response.data, (error) => { const meta = error.response.data.meta; const { code, status } = meta; console.log(meta); }; } 

but still not show anything.

Any solution?

+5
source share
1 answer

Here is an example of use with ES6:

 import axios from 'axios' import axiosMiddleware from 'redux-axios-middleware' const options = { // not required, but use-full configuration option returnRejectedPromiseOnError: true, interceptors: { request: [ ({ getState, dispatch }, config) => { // Request interception return config } ], response: [ { success: ({ dispatch }, response) => { // Response interception return response }, error: ({ dispatch }, error) => { // Response Error Interception return Promise.reject(error) } } ] } } export default axiosMiddleware(axios, options) 

Note that the generated middleware must be passed to createStore ()

+2
source

Source: https://habr.com/ru/post/1266999/


All Articles