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

Add response handler #171

Closed
wants to merge 6 commits into from
Closed

Add response handler #171

wants to merge 6 commits into from

Conversation

iamandrewluca
Copy link
Collaborator

@iamandrewluca iamandrewluca commented Feb 8, 2018

Added a wrapper for apiMiddleware so user can pass his own check if response is OK

User can import as usual apiMiddleware
Or can import function createMiddleware that when called with options will return the apiMiddleware

TODO:

  • global response OK handler
  • global JSON response handler
  • per-request response OK handler
  • per-request JSON response handler
  • Write tests
  • Add JsDocs
  • Update docs.

Closes #170

@coveralls
Copy link

coveralls commented Feb 8, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling a7cb996 on iamandrewluca:custom-error-evaluation into ed982b5 on agraboso:master.

return next(action);
}
function createMiddleware(options = {}) {
const middlewareOptions = Object.assign({}, defaults, options);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR! I've only skimmed over this but have one immediate concern -- the 'responseOk' config seems much more useful on a per-request basis (ie, a property on callAPI below) than on the middleware as a whole.

Perhaps I'm misunderstanding the implementation here, but what are your thoughts? In either case, we'll need some docs explaining how to use this 😆

@iamandrewluca
Copy link
Collaborator Author

iamandrewluca commented Feb 12, 2018

@nason Yes for response check makes sense per-request basis.
When I'll have more time, I'll try to add this, and keep back compatibility 😃

  • global response OK handler
  • global JSON response handler
  • per-request response OK handler
  • per-request JSON response handler

With the new createMiddleware function we can customize everything and give the user options to customize the middleware how he wants.

@nason
Copy link
Collaborator

nason commented Mar 3, 2018

Hey @iamandrewluca are you still interested in working on this?

My opinion is that adding per-RSAA options (vs global middleware config) is the most useful for now, but I could be convinced otherwise 😆

@nason nason added this to the 3.0.0 milestone Mar 3, 2018
@iamandrewluca
Copy link
Collaborator Author

iamandrewluca commented Mar 4, 2018

@nason Yes, still interested, don't have so much time. Next week, I'll try to add some bits of code. 😉 My use case is exactly like in #170. Our backend is kind of legacy. And on any exception is responding with 200 instead of 500.

@iamandrewluca
Copy link
Collaborator Author

iamandrewluca commented Mar 7, 2018

@nason it seems #174 fixes the problem with JSON handling. I realised this after rebasing on top of it 😃
Adding just res.ok check will be enough I think.

Now user can provide a global ok/fetch handler or a local ok/fetch handler
Local ok/fetch handler will take priority
@shulcsm
Copy link

shulcsm commented Mar 27, 2018

Does this solve redirect handling? Is there an ETA?
At the moment redirects cause broken ApiError

@iamandrewluca
Copy link
Collaborator Author

@shulcsm yes, with this PR, you could do your own response status handling, need to write some test, and may be merged.

@nason nason mentioned this pull request Jun 7, 2018
Copy link
Collaborator

@nason nason left a comment

Choose a reason for hiding this comment

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

I finally got to spend some time looking at this. Great work @iamandrewluca!

I left a few comments below, and I agree the bulk of work that remains is tests and documentation. I'm happy to jump in with anything thats left, just let me know how I can be helpful.

@@ -176,7 +174,7 @@ function apiMiddleware({ getState }) {
}

// Process the server response
if (res.ok) {
if (ok(res)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets handle errors thrown from the ok function in a try/catch here. Should they be handled like those from doFetch above?

*/
function apiMiddleware({ getState }) {
return next => action => {
function createMiddleware(options = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should document this and how to use it 📚

Would configuring defaults for your middleware be like:

const apiMiddlewareWithDefaults = createMiddleware({
  fetch: myFetch,
  ok: myOk
})

when configuring your store? What other defaults can you set this way?

And then you can override from individual RSAAs?

{
  [RSAA]: {
    ...
    ok: myOtherOk
    ...
  }
}

@nason
Copy link
Collaborator

nason commented Jun 10, 2018

@iamandrewluca I opened #194 from this branch, rebased it onto next (cut for 3.0 release), did a little cleanup, added docs, tests, and added error handling around the ok(res) call.

I'm going to close this PR and merge the updated one into next. Thank you so much for your help here!

@nason nason closed this Jun 10, 2018
nason added a commit that referenced this pull request Jun 10, 2018
Add custom `ok` hander (#171) + cleanup, docs, and tests
@nason
Copy link
Collaborator

nason commented Jun 14, 2018

This has been released in 3.0.0-beta.0

@iamandrewluca
Copy link
Collaborator Author

@nason Thanks! :octocat:

@iamandrewluca iamandrewluca deleted the custom-error-evaluation branch June 14, 2018 12:01
@nealYangVic
Copy link

Any example what is good practice here?

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

Successfully merging this pull request may close these issues.

Custom error evaluation
5 participants