restful-react icon indicating copy to clipboard operation
restful-react copied to clipboard

Resolve prop function does not work at RestfulProvider level

Open ryanahamilton opened this issue 4 years ago • 2 comments

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>
  );
};

ryanahamilton avatar Oct 27 '20 16:10 ryanahamilton

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?

fabien0102 avatar Oct 28 '20 12:10 fabien0102

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

danielchabr avatar Nov 26 '20 17:11 danielchabr