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

Commit

Permalink
Add querystring params
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Feb 18, 2020
1 parent cf9920c commit 1805ecd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
33 changes: 33 additions & 0 deletions src/useGet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,39 @@ describe("useGet hook", () => {
});
});

describe("querystring custom params", () => {
it("should parse the querystring regarding the options", async () => {
nock("https://my-awesome-api.fake")
.get("/")
.query(i => {
return i["anArray[]"] === "nice";
})
.reply(200, () => ({ id: 42 }));

const children = jest.fn();
children.mockReturnValue(<div />);

const MyAwesomeComponent: React.FC<{ path: string }> = ({ path }) => {
const params = useGet<{ id: number }, any, { anArray: string[] }>({
path,
queryParams: { anArray: ["nice"] },
queryParamStringifyOptions: { arrayFormat: "brackets" },
});
return children(params);
};

render(
<RestfulProvider base="https://my-awesome-api.fake">
<MyAwesomeComponent path="" />
</RestfulProvider>,
);

await wait(() => expect(children).toBeCalledTimes(2));
expect(children.mock.calls[1][0].loading).toEqual(false);
expect(children.mock.calls[1][0].data).toEqual({ id: 42 });
});
});

describe("generation pattern", () => {
it("should call the correct endpoint", async () => {
nock("https://my-awesome-api.fake")
Expand Down
22 changes: 16 additions & 6 deletions src/useGet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export interface UseGetProps<TData, TQueryParams> {
| number;
}

export function resolvePath<TQueryParams>(base: string, path: string, queryParams: TQueryParams, queryParamOptions: IStringifyOptions = {}) {
export function resolvePath<TQueryParams>(
base: string,
path: string,
queryParams: TQueryParams,
queryParamOptions: IStringifyOptions = {},
) {
const appendedBase = base.endsWith("/") ? base : `${base}/`;
const trimmedPath = path.startsWith("/") ? path.slice(1) : path;

Expand Down Expand Up @@ -97,7 +102,7 @@ async function _fetchData<TData, TError, TQueryParams>(
(typeof context.requestOptions === "function" ? await context.requestOptions() : context.requestOptions) || {};

const request = new Request(
resolvePath(base, path, { ...context.queryParams, ...queryParams }, props.queryParamStringifyOptions || P{),
resolvePath(base, path, { ...context.queryParams, ...queryParams }, props.queryParamStringifyOptions || {}),
merge({}, contextRequestOptions, requestOptions, { signal }),
);

Expand Down Expand Up @@ -216,10 +221,15 @@ export function useGet<TData = any, TError = any, TQueryParams = { [key: string]

return {
...state,
absolutePath: resolvePath(props.base || context.base, props.path, {
...context.queryParams,
...props.queryParams,
}, props.queryParamStringifyOptions || {}),
absolutePath: resolvePath(
props.base || context.base,
props.path,
{
...context.queryParams,
...props.queryParams,
},
props.queryParamStringifyOptions,
),
cancel: () => {
setState({
...state,
Expand Down
31 changes: 31 additions & 0 deletions src/useMutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,38 @@ describe("useMutate", () => {
});
expect(res).toEqual({ vegan: true });
});

it("should parse the querystring regarding the options", async () => {
nock("https://my-awesome-api.fake")
.delete("/")
.query(i => {
return i["anArray[]"] === "nice";
})
.reply(200, () => ({ vegan: true }));

const wrapper: React.FC = ({ children }) => (
<RestfulProvider base="https://my-awesome-api.fake">{children}</RestfulProvider>
);
const { result } = renderHook(
() =>
useMutate("DELETE", "", {
queryParams: { anArray: ["nice"] },
queryParamStringifyOptions: { arrayFormat: "brackets" },
}),
{
wrapper,
},
);
const res = await result.current.mutate("");

expect(result.current).toMatchObject({
error: null,
loading: false,
});
expect(res).toEqual({ vegan: true });
});
});

it("should merge with the provider's query parameters if both specified", async () => {
nock("https://my-awesome-api.fake")
.delete("/")
Expand Down
7 changes: 6 additions & 1 deletion src/useMutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ export function useMutate<
}

const request = new Request(
resolvePath(base, isDelete ? `${path}/${body}` : path, { ...context.queryParams, ...queryParams }),
resolvePath(
base,
isDelete ? `${path}/${body}` : path,
{ ...context.queryParams, ...queryParams },
props.queryParamStringifyOptions,
),
merge({}, contextRequestOptions, options, propsRequestOptions, mutateRequestOptions, { signal }),
);

Expand Down

0 comments on commit 1805ecd

Please sign in to comment.