Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Ensure onSuccess is called with transformed data when tranformResponse is used #222

Closed
asherccohen opened this issue Apr 24, 2021 · 4 comments
Milestone

Comments

@asherccohen
Copy link

Following the conversation with @phryneas on the discord channel (link ), we noticed that the flow of responses in callback methods with a transformResponse does not guarantee the same shape of data, this requires to transform in each callback (potential bottleneck and error prone).

I don't have the time for a PR, so here's the steps to get there.

How to reproduce:

  • create a service
  • add a transformResponse callback (with a simple fn that changes the shape of response)
  • add an onSuccess callback and log "result".

Expected:
"result" should be transformed

What happens:
"result" is not transformed

Code:

if (result.error) throw new HandledError(result.error, result.meta);
if (endpointDefinition.onSuccess)
endpointDefinition.onSuccess(arg.originalArgs, queryApi, result.data, result.meta);
return {
fulfilledTimeStamp: Date.now(),
result: await transformResponse(result.data, result.meta),
};

The above lines of code demonstrate that onSuccess and onError are called with the original data.

It's a bit unexpected that onSuccess and onError are called before the data is persisted in the store
Could be moved out of the asyncThunk into initiate.

@phryneas
Copy link
Collaborator

Might actually make sense to tackle this together with #215 and move all lifecycle event handling into one central place in the middleware.

@asherccohen
Copy link
Author

EDIT: Check that also selectFromResult adheres to the same shape.

@phryneas
Copy link
Collaborator

@asherccohen it should, that one reads from the redux store wayyy later

@phryneas phryneas added this to the 1.6.0-alpha3 milestone Apr 30, 2021
@phryneas
Copy link
Collaborator

This is on the integration branch right now, closing it here.

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

No branches or pull requests

2 participants