BetterTyped / hyper-fetch

⚡ Fetching and realtime data exchange framework.
https://hyperfetch.bettertyped.com/
Apache License 2.0
1.02k stars 27 forks source link

ResponseMapper in combination with useFetch #65

Closed tschirky closed 1 year ago

tschirky commented 1 year ago

First of all, I wanna say that I like hyper-fetch a lot!! Especially for the offline use-cases we're about to tackle, it seems the perfect solution.

Anyways, I found this inconsistency when creating a request. As by the documentation, you can append a response mapper. Here's an example:

export const getUsers = client
  .createRequest<ObjectWithUsers>()({
    method: "GET",
    endpoint: "/users",
  })
  .setResponseMapper(async (response) => {
    if (response.data) {
      return {
        ...response,
        data: {
           ...response.data,
           users: response.data.map((user) => new User(user));
        }
      };
    }
    return response;
  });

When using this request in a useFetch hook afterwards, I observe the following:

const { data, error, loading, onSuccess } = useFetch(getUsers);

Now I read that the responses are cached in their "original" form, but I would expect that since "data" and "onSuccess" are based on the same request definition (i.e. getUsers), they would both be provided with the same response.

prc5 commented 1 year ago

@tschirky Thanks for the support and the issue, we will handle this!

tschirky commented 1 year ago

Very cool! Thanks for fixing this.

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 5.7.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: