diff --git a/src/useGet.test.tsx b/src/useGet.test.tsx index 69398e03..a5726282 100644 --- a/src/useGet.test.tsx +++ b/src/useGet.test.tsx @@ -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 😍" }); @@ -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") @@ -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; diff --git a/src/useGet.tsx b/src/useGet.tsx index 66b0df2f..920971a2 100644 --- a/src/useGet.tsx +++ b/src/useGet.tsx @@ -56,6 +56,13 @@ export interface UseGetProps { | number; } +function resolvePath(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( props: UseGetProps, state: GetState, @@ -83,7 +90,7 @@ async function _fetchData( (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 }), ); @@ -196,10 +203,7 @@ export function useGet { setState({ ...state,