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

Customing FAILURE Response #189

Closed
rockacola opened this issue Jun 4, 2018 · 6 comments
Closed

Customing FAILURE Response #189

rockacola opened this issue Jun 4, 2018 · 6 comments

Comments

@rockacola
Copy link

Is it possible to force an 'FAILURE' type base on the response data of a success API execution?

In a JSON-RPC API, all non-network errors should result in "HTTP Code 200" with error code and message in its payload. For example, response for an invalid request:

{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}

So my question is, is it possible to utlize RSAA and have custom success/failure rules oppose to fully relying on the response HTTP code?

@darthrellimnad
Copy link
Contributor

darthrellimnad commented Jun 5, 2018

You should be able to customize your "success" type descriptor with a function that returns an action of the desired type based on response payload. See: https://github.com/agraboso/redux-api-middleware/blob/master/README.md#customizing-the-dispatched-fsas

Edit: forgot this only applies for the payload and meta of the type descriptor... don't think we can do this for the type property :(

Another option would be to write a second middleware that can capture the "success" FSA, and modify it before sending to next middleware (based on payload)

Maybe allowing the type property of an RSAA type descriptor to also be a function would be a good feature request. Or better yet, allowing the entire type descriptor to be a function that returns an FSA?

@rockacola
Copy link
Author

Thanks for the feedback.

Maybe allowing the type property of an RSAA type descriptor to also be a function would be a good feature request.

That's what I was after. Can this ticket serves as feature request? or do you suggest that I create a separate ticket for it?

@nason
Copy link
Collaborator

nason commented Jun 7, 2018

Hi this is interesting. Feel free to open a PR or comment here with what you think this should look like.

So my question is, is it possible to utlize RSAA and have custom success/failure rules oppose to fully relying on the response HTTP code?

#171 would allow you to pass in your own function to replace the default res.ok check. Would that help in your situation?

In your case I'd imagine the RSAA could look something like:

{
  [RSAA]: {
    ...
    ok: async (res) => {
      const json = await res.json();
      return res.ok && !json.error;
    }
    ...
  }
}

@darthrellimnad
Copy link
Contributor

darthrellimnad commented Jun 7, 2018

i think the suggestion by @nason could also work, without needing the second middleware, or changing the functionality of the typeDescriptor spec, but haven't tested myself :) downside to that is that it seems to be configured at the middleware level? so if you wanted to use the same middleware configured with a single redux store, for an app that uses a jsonrpc endpoint and some other types of endpoint, could still be problematic.

Edit: nvm I'm dumb :) didn't also see that this can be configured per RSAA 👍

@nason
Copy link
Collaborator

nason commented Jun 18, 2018

This functionality is in the 3.0 beta in the next branch now. I'm going to close this issue, but feel free to re-open if a custom ok does not solve this issue for you

@nason nason closed this as completed Jun 18, 2018
@Reeywhaar
Copy link

Reeywhaar commented Aug 1, 2019

I'm getting the following error as action data while using RSAA.ok function

{
  payload: {
    name: 'InternalError',
    message: 'body used already for: http://redacted'
  },
  type: 'REDACTED_FULFILLED',
  error: true
}

It seems that .ok should not call .json() or .text() on response data. Also function should return boolean, not Promise<boolean>. Docs on this feature a bit confusing and contradict with example you gave above :-)

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

No branches or pull requests

4 participants