-
Notifications
You must be signed in to change notification settings - Fork 64
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
Add middleware support #122
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! It's super slick, and I can definitely see how this opens a lot of possibilities!
With the current implementation, calling next()
twice in a JsonRpcSenderMiddleware
function would result in skipped middlewares functions or an array out of bounds error. Can you update the sendJsonRpcPayload
function to include a boolean check that makes sure each middleware function can only call next()
a single time?
Thanks!
@thebrianchen nice catch. I created a closure and added a condition to ensure that middlewares can call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the quick turn around, looks good to me! just adding @dphilipson as a reviewer for another check
@thebrianchen I was thinking of a case where if we want to implement our custom retry logic with middleware, in that case, we would not be able to call |
@thebrianchen I made the change that allows calling |
@emr Great work on this- it's a fantastic addition. I'll merge this then publish. |
Published in v1.4.0. |
Hello, thank you for this useful library. We were using ethers.js to listen to new logs on blockchain, but due to occasional websocket disconnection, we made research for solutions and found this library and decided to use it. Now we want to add our functionalities to the alchemy-web3 provider. Functionalities like monitoring, caching, I mean, middleware things. To do so, we added middleware support to the library. So now we can monitor our outgoing/incoming ws/http messages and cache them.
Example middleware for monitoring:
For caching:
I removed some boring logics like checking if a request/response is batch, if it contains ids, etc to keep examples simple.
Usage:
We tested the feature with these 2 functionalities, it works perfectly. If you accept this PR, we can update the docs with your help.
In the future, maybe you can think of implementing internal caching which can be enabled with the library options.
Thanks!