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

Commit

Permalink
Fix url composition pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Apr 16, 2019
1 parent b5c75ab commit 0d75fd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
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 escapedBase = base.endsWith("/") ? base : `${base}/`;
const escapedPath = path.startsWith("/") ? path.slice(1) : path;

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

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

0 comments on commit 0d75fd7

Please sign in to comment.