Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Resolve prop function does not work at RestfulProvider level #315

Open
ryanahamilton opened this issue Oct 27, 2020 · 2 comments
Open

Resolve prop function does not work at RestfulProvider level #315

ryanahamilton opened this issue Oct 27, 2020 · 2 comments

Comments

@ryanahamilton
Copy link

Describe the bug
In v15.1.1 I am unable to globally define Response Resolution at the RestfulProvider level.

To Reproduce

The following does not work to provide function to resolve in RestfulProvider. The data arrives in Component.js unmodified, in its original shape.

// In index.js
<RestfulProvider
  base="http://127.0.0.1:28080/api/v1/"
  resolve={(data) => humps.camelizeKeys(data)} // THIS DOES NOT WORK
  requestOptions={() => ({
    headers: { Authorization: authorization },
  })}
>
  <App />
</RestfulProvider>

// In Component.js
const Component = () => {
  const { data, loading } = useGet({
    path: '/items',
  });
  return (
    <ul>
      {data && data.items.map((item) => (
        <li key={item.itemId}>{item.itemName}</li>
      ))}
    </ul>
  );
};

However, when providing function to resolve in useGet hook from context consuming component it does work.

// In index.js
<RestfulProvider
  base="http://127.0.0.1:28080/api/v1/"
  requestOptions={() => ({
    headers: { Authorization: authorization },
  })}
>
  <App />
</RestfulProvider>

// In Component.js
const Component = () => {
  const { data, loading } = useGet({
    path: '/items',
    resolve: (data) => humps.camelizeKeys(data), // THIS DOES WORK
  });
  return (
    <ul>
      {data && data.items.map((item) => (
        <li key={item.itemId}>{item.itemName}</li>
      ))}
    </ul>
  );
};
@fabien0102
Copy link
Contributor

Indeed, context.resolve is not used at all in useGet, I totally forgot about this feature when implementing the hooks 🙄

This should be quite straight forward to implement, do you want to take care of this?

@danielchabr
Copy link

Could you add response status to the arguments when calling the provided resolve function. In my use case I would like to treat success and error data differently

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

3 participants