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

Fix useGet url composition pattern #114

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/useGet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,19 @@ describe("useGet hook", () => {
[
{ base: "https://my-awesome-api.fake", path: "/", expected: ["https://my-awesome-api.fake", "/"] },
{ base: "https://my-awesome-api.fake", path: "/plop", expected: ["https://my-awesome-api.fake", "/plop"] },
{ base: "https://my-awesome-api.fake/plop", path: "/", expected: ["https://my-awesome-api.fake", "/"] },
{ base: "https://my-awesome-api.fake/plop/", path: "/", expected: ["https://my-awesome-api.fake", "/"] },
{ base: "https://my-awesome-api.fake/plop", path: "/", expected: ["https://my-awesome-api.fake", "/plop/"] },
{ base: "https://my-awesome-api.fake/plop/", path: "/", expected: ["https://my-awesome-api.fake", "/plop/"] },
{ base: "https://my-awesome-api.fake/plop/", path: "", expected: ["https://my-awesome-api.fake", "/plop/"] },
{ base: "https://my-awesome-api.fake/plop/", path: "../", expected: ["https://my-awesome-api.fake", "/"] },
{ base: "https://my-awesome-api.fake/a", path: "/b", expected: ["https://my-awesome-api.fake", "/b"] },
{ base: "https://my-awesome-api.fake/a", path: "/b", expected: ["https://my-awesome-api.fake", "/a/b"] },
{
base: "https://my-awesome-api.fake/a",
path: "/tejas/",
expected: ["https://my-awesome-api.fake", "/a/tejas/"],
},
{ base: "https://my-awesome-api.fake/a/", path: "", expected: ["https://my-awesome-api.fake", "/a/"] },
].forEach(({ base, path, expected }) => {
it(`should call ${expected.join("")}`, async () => {
].forEach(({ base, path, expected }, i) => {
it(`should call ${expected.join("")}(${i})`, async () => {
nock(expected[0])
.get(expected[1])
.reply(200, { oh: "my god 😍" });
Expand Down Expand Up @@ -442,6 +447,7 @@ describe("useGet hook", () => {
expect(children).toHaveBeenCalledWith({ data: null, error: null, loading: false });
});
});

describe("with base", () => {
it("should override the base url", async () => {
nock("https://my-awesome-api.fake")
Expand Down Expand Up @@ -710,6 +716,7 @@ describe("useGet hook", () => {
expect(resolve).not.toHaveBeenCalled();
});
});

describe("refetch after update", () => {
it("should not refetch when base, path or resolve don't change", () => {
let apiCalls = 0;
Expand Down
14 changes: 9 additions & 5 deletions src/useGet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export interface UseGetProps<TData, TQueryParams> {
| number;
}

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

return url.resolve(appendedBase, queryParams ? `${trimmedPath}?${qs.stringify(queryParams)}` : trimmedPath);
}

async function _fetchData<TData, TError, TQueryParams>(
props: UseGetProps<TData, TQueryParams>,
state: GetState<TData, TError>,
Expand Down Expand Up @@ -83,7 +90,7 @@ async function _fetchData<TData, TError, TQueryParams>(
(typeof context.requestOptions === "function" ? context.requestOptions() : context.requestOptions) || {};

const request = new Request(
url.resolve(base, queryParams ? `${path}?${qs.stringify(queryParams)}` : path),
resolvePath(base, path, queryParams),
merge(contextRequestOptions, requestOptions, { signal }),
);

Expand Down Expand Up @@ -196,10 +203,7 @@ export function useGet<TData = any, TError = any, TQueryParams = { [key: string]

return {
...state,
absolutePath: url.resolve(
props.base || context.base,
props.queryParams ? `${props.path}?${qs.stringify(props.queryParams)}` : props.path,
),
absolutePath: resolvePath(props.base || context.base, props.path, props.queryParams),
cancel: () => {
setState({
...state,
Expand Down