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

Add support for requestOptions as a method to Poll #113

Merged
merged 2 commits into from
Apr 8, 2019

Conversation

vxsx
Copy link
Contributor

@vxsx vxsx commented Apr 7, 2019

Why

Fixes #112

I'm not sure this is a proper way, but it works. Get / Poll seem to be quite different and sadly I didn't have much time to dig into the source code.

@TejasQ TejasQ requested a review from fabien0102 April 8, 2019 12:22
@TejasQ TejasQ self-assigned this Apr 8, 2019
Copy link
Contributor

@fabien0102 fabien0102 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks for you contribution, just a little edgecase and we are good :)

src/Poll.tsx Outdated
{...contextProps}
{...props}
requestOptions={{
...contextRequestOptions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost perfect! But you need to use merge from lodash here, so we can add a header without erase these set in the Provider

Example:
https://github.com/contiamo/restful-react/blob/master/src/useGet.tsx#L87

and the associate test:

it("should merge headers with providers", async () => {
nock("https://my-awesome-api.fake", { reqheaders: { foo: "bar", bar: "foo" } })
.get("/")
.reply(200, { oh: "my god 😍" });
const MyAwesomeComponent = () => {
const { data, loading } = useGet<{ oh: string }>({ path: "/", requestOptions: { headers: { foo: "bar" } } });
return loading ? <div data-testid="loading">Loading…</div> : <div data-testid="data">{data.oh}</div>;
};
const { getByTestId } = render(
<RestfulProvider base="https://my-awesome-api.fake" requestOptions={() => ({ headers: { bar: "foo" } })}>
<MyAwesomeComponent />
</RestfulProvider>,
);
await waitForElement(() => getByTestId("data"));
expect(getByTestId("data")).toHaveTextContent("my god 😍");
});
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good catch, thanks!

Fixed and added a test.

Copy link
Contributor

@fabien0102 fabien0102 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to me! Thank again for this beautiful PR 👌

@TejasQ
Copy link
Contributor

TejasQ commented Apr 8, 2019

@vxsx, you're a legend. Thanks for your contribution! 🎉

@TejasQ TejasQ merged commit b5c75ab into contiamo:master Apr 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants