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

Commit

Permalink
Clone response before passing to onResponse
Browse files Browse the repository at this point in the history
This ensure that onResponse can read the response body.
  • Loading branch information
torkelrogstad authored and fabien0102 committed Jul 10, 2020
1 parent c4b0494 commit cc1a801
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/Get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ describe("Get", () => {
const children = jest.fn();
children.mockReturnValue(<div />);

const onResponse = jest.fn();
let body: any;
const onResponse = jest.fn(async (res: Response) => {
body = await res.json();
});

render(
<RestfulProvider base="https://my-awesome-api.fake" onResponse={onResponse}>
Expand All @@ -172,6 +175,7 @@ describe("Get", () => {

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(onResponse).toBeCalled();
expect(body).toMatchObject({ hello: "world" });
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ class ContextlessGet<TData, TError, TQueryParams, TPathParams = unknown> extends
if (onRequest) onRequest(request);
try {
const response = await fetch(request, { signal: this.signal });
if (onResponse) onResponse(response.clone());
const { data, responseError } = await processResponse(response);
if (onResponse) onResponse(response);

// avoid state updates when component has been unmounted
if (this.signal.aborted) {
Expand Down
6 changes: 5 additions & 1 deletion src/Mutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,10 @@ describe("Mutate", () => {
const children = jest.fn();
children.mockReturnValue(<div />);

const onResponse = jest.fn();
let body: any;
const onResponse = jest.fn(async (res: Response) => {
body = await res.json();
});

render(
<RestfulProvider base="https://my-awesome-api.fake" onResponse={onResponse}>
Expand All @@ -979,6 +982,7 @@ describe("Mutate", () => {

// expect onResponse to be called
expect(onResponse).toBeCalled();
expect(body).toMatchObject({ hello: "world" });
});
});
});
2 changes: 1 addition & 1 deletion src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class ContextlessMutate<TData, TError, TQueryParams, TRequestBody, TPathParams>
let response: Response;
try {
response = await fetch(request, { signal: this.signal });
if (onResponse) onResponse(response);
if (onResponse) onResponse(response.clone());
} catch (e) {
const error = {
message: `Failed to fetch: ${e.message}`,
Expand Down
6 changes: 5 additions & 1 deletion src/Poll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ describe("Poll", () => {
const children = jest.fn();
children.mockReturnValue(<div />);

const onResponse = jest.fn();
let body: any;
const onResponse = jest.fn(async (res: Response) => {
body = await res.json();
});

render(
<RestfulProvider base="https://my-awesome-api.fake" onResponse={onResponse}>
Expand All @@ -431,6 +434,7 @@ describe("Poll", () => {

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(onResponse).toBeCalled();
expect(body).toMatchObject({ hello: "world" });
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ class ContextlessPoll<TData, TError, TQueryParams, TPathParams = unknown> extend

try {
const response = await fetch(request, { signal: this.signal });
if (onResponse) onResponse(response.clone());
const { data, responseError } = await processResponse(response);
if (onResponse) onResponse(response);

if (!this.keepPolling || this.signal.aborted) {
// Early return if we have stopped polling or component was unmounted
Expand Down
6 changes: 5 additions & 1 deletion src/useGet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,16 @@ describe("useGet hook", () => {
.get("/")
.reply(200, { oh: "my god 😍" });

let body: any;
const MyAwesomeComponent = () => {
const { data, loading } = useGet<{ oh: string }>({ path: "/" });

return loading ? <div data-testid="loading">Loading…</div> : <div data-testid="data">{data?.oh}</div>;
};

const onResponse = jest.fn();
const onResponse = jest.fn().mockImplementation(async (response: Response) => {
body = await response.json();
});

const { getByTestId } = render(
<RestfulProvider base="https://my-awesome-api.fake" onResponse={onResponse}>
Expand All @@ -159,6 +162,7 @@ describe("useGet hook", () => {
await waitForElement(() => getByTestId("data"));

expect(onResponse).toBeCalled();
expect(body).toMatchObject({ oh: "my god 😍" });
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/useGet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ async function _fetchData<TData, TError, TQueryParams, TPathParams>(

try {
const response = await fetch(request);
if (context.onResponse) context.onResponse(response.clone());
const { data, responseError } = await processResponse(response);
if (context.onResponse) context.onResponse(response);

if (signal && signal.aborted) {
return;
Expand Down
6 changes: 5 additions & 1 deletion src/useMutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,10 @@ describe("useMutate", () => {
.post("/")
.reply(200, { hello: "world" });

const onResponse = jest.fn();
let body: any;
const onResponse = jest.fn(async (res: Response) => {
body = await res.json();
});
const wrapper: React.FC = ({ children }) => (
<RestfulProvider base="https://my-awesome-api.fake" onResponse={onResponse}>
{children}
Expand All @@ -623,6 +626,7 @@ describe("useMutate", () => {
const { result } = renderHook(() => useMutate("POST", ""), { wrapper });
await result.current.mutate({ foo: "bar" });
expect(onResponse).toBeCalled();
expect(body).toMatchObject({ hello: "world" });
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/useMutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function useMutate<
let response: Response;
try {
response = await fetch(request);
if (context.onResponse) context.onResponse(response);
if (context.onResponse) context.onResponse(response.clone());
} catch (e) {
const error = {
message: `Failed to fetch: ${e.message}`,
Expand Down

0 comments on commit cc1a801

Please sign in to comment.