Skip to content
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

Advanced middleware feature #23

Merged
merged 9 commits into from
Mar 19, 2017

Conversation

iamlacroix
Copy link
Contributor

This is the advanced middleware implementation per our discussion in #4.

It moves the built-in superagent request object into an adapter and that adapter is used by default importing the standard queryMiddleware. A new queryMiddlewareAdvanced export is available which requires an adapter as one of the parameters.

The adapter and it's initializer look like this:

type Adapter = {
  execute: Function,
  abort: Function,
}

type CreateAdapter = (
  url: string,
  method: 'GET' | 'POST' | 'PUT' | 'DELETE',
  config?: { body?: string | Object, headers?: Object } = {},
) => Adapter

Once/if all this is close to being merged, I can update the documentation in the README as well (for advanced usage, normal usage remains the same).

@codecov-io
Copy link

codecov-io commented Feb 23, 2017

Codecov Report

Merging #23 into master will increase coverage by 2.27%.
The diff coverage is 94.81%.

@@            Coverage Diff             @@
##           master      #23      +/-   ##
==========================================
+ Coverage   90.14%   92.41%   +2.27%     
==========================================
  Files           7        9       +2     
  Lines         213      211       -2     
==========================================
+ Hits          192      195       +3     
+ Misses         21       16       -5
Impacted Files Coverage Δ
src/middleware/query.js 100% <100%> (+8.75%)
src/middleware/query-advanced.js 94.54% <94.54%> (ø)
src/adapters/superagent.js 95.45% <95.45%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ba5e669...ae29cf4. Read the comment docs.

Copy link
Contributor

@ryanashcraft ryanashcraft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamlacroix Thanks for taking the time to pull this together. I have a few comments. Let me know what you think.

import superagent from 'superagent';
import * as httpMethods from '../constants/http-methods';

export const __createRequest = (url, method) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to indicate that it shouldn't be used except by tests? If so I think it's fine to just export it without the __ prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's all it meant, I'll remove it.


const resOk = (status) => Math.floor(status / 100) === 2;

const queryMiddlewareAdvanced = (queriesSelector, entitiesSelector, networkAdapter, config = defaultConfig) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we pull networkAdapter out like so:

const queryMiddlewareAdvanced = (networkAdapter) => (queriesSelector, entitiesSelector, config = defaultConfig)

Then the default middleware will be much simpler:

const queryMiddleware = queryAdvanced(superagentAdapter);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ That's a much better idea.

src/index.js Outdated
@@ -8,5 +8,6 @@ export { default as getQueryKey } from './lib/get-query-key';
export { default as queriesReducer } from './reducers/queries';
export { default as entitiesReducer } from './reducers/entities';
export { default as queryMiddleware } from './middleware/query';
export { default as queryMiddlewareAdvanced } from './middleware/query-advanced';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I see with this is that projects that use the new queryMiddlewareAdvanced will keep pulling in superagent through the queryMiddleware from above.

I'm thinking we can easily fix this by:

  • duplicating src/index.js to a new file src/advanced.js
  • removing the './middleware/query' import in src/advanced.js
  • removing the './middleware/query-advanced' import in src/index.js (to distinguish the different entries and usages)
  • add a file to the root of the project (advanced.js) that is a CommonJS alias to dist/commonjs/advanced

This is sort of brittle because a single import for regular redux-query will cause the app's bundler to start including superagent, but I think it's acceptable for now. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally I think we can modify the webpack config to export a new UMD build for redux-query/advanced.

@iamlacroix
Copy link
Contributor Author

@ryanashcraft Thanks for your feedback! I've updated the PR accordingly. Are the UMD changes I made what you had in mind? If not, I'll be happy to update it.

iamlacroix and others added 4 commits February 27, 2017 10:58
This moves most of the logic from the query middleware to the new
advanced middleware. The query middleware will call advanced using
the default superagent adapter.
@ryanashcraft
Copy link
Contributor

@iamlacroix Sorry I botched a minor change (ae29cf4) I added to this PR and had to do a force push.

The UMD builds look good. I'm ready to merge this. Before I do, can you do some testing with your app to make sure the advanced entry point and custom adapters works as expected? Also, if you can update add some info in the README that'd be awesome. Thanks!

@iamlacroix
Copy link
Contributor Author

@ryanashcraft Great to hear! I'll update the README and let you know the results of testing as soon as I can.

@jnutter
Copy link
Contributor

jnutter commented Mar 7, 2017

Can we add support for PATCH as a verb?

@ryanashcraft
Copy link
Contributor

@jnutter Yes, that should be easy but somewhat orthogonal to this PR. I made a new issue to track that here: #33.

@ryanashcraft
Copy link
Contributor

@iamlacroix Do you mind if I help out with this PR to get it merged? I actually need the advanced middleware for a project I have in mind. 😊

@iamlacroix
Copy link
Contributor Author

@ryanashcraft Yea, absolutely. Sorry for the delay on my end, I was sick for a bit and have been getting caught up on work.

@ryanashcraft ryanashcraft merged commit d922161 into amplitude:master Mar 19, 2017
@acontreras89
Copy link
Contributor

acontreras89 commented Mar 30, 2017

Sorry to write here but I didn't know where to ask.

I've recently developed an adapter for fetch. Fetch doesn't support request abortion yet, so it will require more work eventually.

I was wondering if this is something you'd like to add to this project (as an alternative to the SuperAngent adapter), or it would better fit its own separate package.

@ryanashcraft
Copy link
Contributor

@acontreras89 Nice! I think for now it makes more sense to have it as a separate package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants