svrcekmichal / redux-axios-middleware

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

it should be possible to get action which triggered interceptor #35

Closed svrcekmichal closed 7 years ago

tlenclos commented 7 years ago

Hello there, something is blocking this PR ?

nmaves commented 7 years ago

It looks like we are just waiting for confirmation from the OP of #34 to confirm that it solved the issue.

svrcekmichal commented 7 years ago

@nmaves I don't believe it will work. I was quite naive with my little testing of this PR. Solution proposed by @tlenclos looks better from my point of view. The question is, aren't we doing too much to support axios interceptors? bending whole axios and redux to play nicely while we can implement interceptors ourself, it's few lines of code and have nicer API overall?

nmaves commented 7 years ago

I am not against that @svrcekmichal. Can you give me an example of how we would handle it on our side?

svrcekmichal commented 7 years ago

Well, my idea was to create nicer API for whole middleware, for example would be created with closure createAxiosClient(name, axiosConfig). Function will return closure with multiple functions to setup middleware options and also three new functions getClient, addRequestInterceptor and addResponseInterceptor.

getClient will handle logic about creating axios client instance when first time needed and two other functions describe what they do. Implementation of interceptors is only chaining of promises before and after the request is made

tlenclos commented 7 years ago

We definitly need to handle a stack to link the action that started the request with the response. I got a new bug in my app when I start two different requests in parallels, the second is linked with the first action 😞 .

svrcekmichal commented 7 years ago

I believe we can try and publish @benarmston solution to see if it will work for now, before we remake it completly. If anyone can make PR to this branch it would be great. If not I will try to handle as soon as I can.

tlenclos commented 7 years ago

Yes, @svrcekmichal do you want me to work on this ?

svrcekmichal commented 7 years ago

@tlenclos would be great 👍

mgenov commented 7 years ago

Is this intended to work only for the request ? On my side calling getSourceAction by passing the response is returning undefined. Here is a sample snippet:

response: [
      {
        success: ({ dispatch, getSourceAction }, response) => {
          console.log(getSourceAction(response))
          return Promise.resolve(response)
        },
        error: ({ dispatch, getSourceAction }, error) => {
          console.log(getSourceAction(error))
          return Promise.reject(error)
        }
      }
    ]

What I'm trying to accomplish is refreshing of a token when it expires and server returns that request is not authorized.

Here is an example how this could be accomplished using axios interceptors: https://plnkr.co/edit/0ZLpc8jgKI18w4c0f905?p=preview

webberwang commented 6 years ago

The getSourceAction receives the config object, which is nested under response at response.config

response: [
      {
        success: ({ dispatch, getSourceAction }, response) => {
          console.log(getSourceAction(response.config))
          return Promise.resolve(response)
        },
        error: ({ dispatch, getSourceAction }, error) => {
          console.log(getSourceAction(error.config))
          return Promise.reject(error)
        }
      }
    ]