-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How about an onRequest hook? #33
Comments
Sounds like you want to dispatch an action on every single request you make? If that is the case you could use a request interceptor. |
I did not know we had dispatch in interceptors! Thanks, let me try this and come back to you |
Actually, interceptors do not help in this case at all. I added them in this way: interceptors: {
request: [({dispatch, action}, config) => {
console.log("request", action)
if (action.messages && action.messages[0]) {
let message = action.messages[0]
if (typeof message == 'function') {
message = message(action)
}
dispatch(notify(message, 'info'))
}
return config
}],
response: [
{
success: ({dispatch, action}, response) => {
console.log("success", action)
if (action.messages && action.messages[1]) {
let message = action.messages[1]
if (typeof message == 'function') {
message = message(action)
}
dispatch(notify(message, 'success'))
}
return response
},
error: ({dispatch, action}, error) => {
console.log("error", action)
if (action.messages && action.messages[2]) {
let message = action.messages[2]
if (typeof message == 'function') {
message = message(action)
}
dispatch(notify(message, 'error'))
}
return Promise.reject(error)
}
}
]
} But these functions fire only in response to middleware's own actions and do not contain the data I need — messages: |
Issue is little bit different from what you describe, but here's the problem. After initialization of middleware, one of the arguments are clients. Client have their own interceptors and interceptors are bound to client when first action which require client is fired. And here comes the issue, interceptor is bound with getState, dispatch and action, but problem of action is that it is the inicialization action not the action which interceptor runs for. Example: I init middleware with client google, client google got request and response interceptor. I fire first action to google client, called the question is, if we can move interceptors to this middleware and use axios just for request, would it be big deal for us? @ckald To give you solution, I would probably use custom middleware before our one. I believe this would take some time to solve |
Thanks, this is precisely what I did. On Sat, Sep 17, 2016, 17:22 Michal Svrček notifications@github.com wrote:
Andrii Magalich |
This is the source, for the reference: export const axiosNotificationsMiddleware = store => next => action => {
if (action.type) {
if (action.type.endsWith('REQUEST') && action.messages && action.messages[0]) {
let message = action.messages[0]
if (typeof message == 'function') {
message = message(action)
}
store.dispatch(notify(message, 'info'))
}
if (action.type.endsWith('SUCCESS')) {
let message = (action.meta && action.meta.previousAction
&& action.meta.previousAction.messages
&& action.meta.previousAction.messages[1])
if (message) {
if (typeof message == 'function') {
message = message(action)
}
store.dispatch(notify(message, 'success'))
}
}
if (action.type.endsWith('FAIL')) {
let message = (action.meta && action.meta.previousAction
&& action.meta.previousAction.messages
&& action.meta.previousAction.messages[2])
if (message) {
if (typeof message == 'function') {
message = message(action)
}
store.dispatch(notify(message, 'error'))
}
}
}
return next(action)
} As you can see, it relies on a "messages" field just like axios middleware relies on "request". Messages can be strings or even functions. |
I believe this was fixed in the 4.0.0 release. We have added a getSourceAction parameter to the request and response interceptors. |
Hi
I am working on a notification system for my app and was able to use onError and onSuccess hooks to dispatch additional actions like this:
However, I cannot find any kind of onRequest handler to display the loading message. I can add another middleware, but it seems that it would make sense for redux-axios-middleware to have this
The text was updated successfully, but these errors were encountered: