-
Notifications
You must be signed in to change notification settings - Fork 67
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
Proof-of-concept for custom network adapters #4
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4 +/- ##
=========================================
+ Coverage 88.34% 89.9% +1.55%
=========================================
Files 7 8 +1
Lines 206 208 +2
=========================================
+ Hits 182 187 +5
+ Misses 24 21 -3
Continue to review full report at Codecov.
|
@iamlacroix The changes looks good as a proof of concept. I do have some concerns but first I'd like to get a better understanding of your end-goals. Smaller bundle size? Is there something you need that superagent doesn't support? I think it may make sense to have the ability to swap in a different library, but I want to make sure we make progress in the right areas. As far as supporting headers/auth, @adamsanderson and I are discussing some simple changes to support those here: #3 |
Hey @ryanashcraft, our main goals are:
Happy to help where needed. |
@iamlacroix Makes sense. I want to do some thinking about the right API for this. Some early thoughts:
|
@ryanashcraft I agree on keeping it together as one package. I don't think you should have to maintain separate adapters within the repo either (in case I was sending that message). As you mentioned, if we could find a way to have the default build inject the superagent adapter and an alternate build w/o, I think that could work well. Anyway, great work on this library! |
Thinking more about this. My next question: what request library are you using that you'd like to use with redux-query? |
If it's a public library like I'm currently thinking a nice way we could do this is a separate main entry point for "advanced usage" for being able to set the request library adapter. Using it would look something like this:
Then the existing query-middleware.js would become query-middleware-advanced.js with a new signature: Great thing is I don't think this is too far from your proof of concept. This is sort of similar to what react-redux does with connectRequest. And redux-form does a similar thing with "redux-form/immutable". |
It's not public unfortunately. Funny thing is it's actually a wrapper around superagent, so we could try to finagle its version so that npm/yarn can resolve to one instance. We could try this initially to reduce the amount of work around the build process aspect (if that helps), as long there is a way for us to inject it into the middleware. Your latest example above is a great approach. |
Alright cool. I'm feeling pretty good about this. Are you interested in making these changes on top of your PR? |
Yea, I'd be happy to make these changes. Thanks for discussing this! |
Superseded by #23 |
Upgrade redux
As the title states, this just a simple example which adds the ability to inject a network adapter to the milddleware. I'm adding this mostly to open up the discussion of this feature.
With this feature, we could create our own adapter to set headers/auth, or to even use a different request library in place of SuperAgent.
The idea is to update the
queryMiddleware
's signature so that an adapter is passed as the 3rd argument. The "adapter" in this example is a function that accepts three args:url
method
body
(optional)The adapter will return an object with two properties, both of which contain functions:
execute
abort
The
execute
function requires a callback, which will receive (at this moment) the following args:err
resOk
resStatus
resBody
resText
The main adapter function is called by the middleware and the returned object is then used to call and cancel requests.
The
execute
function's return args mirror the original variables that were used in the middleware, but we'll likely want to remove theresOk
argument in order to simplify the API. We could just ensure the status code is 2xx instead.Tests are passing, and the async example project is working with this POC, however there might be more I'm missing since I haven't read through the entire source.
Anyway, let me know your thoughts on this type of feature.