Skip to content

Commit

Permalink
feat(url utils): add a utility fn to build URLs from a host, path, an…
Browse files Browse the repository at this point in the history
…d queryParams

AFFECTS PACKAGES:
@esri/hub-common
@esri/hub-content
  • Loading branch information
tomwayson committed Aug 3, 2020
1 parent feec322 commit b3c3331
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
30 changes: 30 additions & 0 deletions packages/common/src/urls/build-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
interface IQueryParams {
[key: string]: string;
}

/**
* @private
*/
export function buildUrl(params: {
host: string;
path: string;
query?: IQueryParams;
}): string {
const { host, path, query } = params;
const baseUrl = host.endsWith("/") ? host : `${host}/`;
const url = new URL(path, baseUrl);
url.search = buildQueryString(query);
return url.toString();
}

function buildQueryString(params: IQueryParams = {}): string {
const queryParams = Object.keys(params)
.filter(key => {
return params[key] !== undefined;
})
.reduce((acc: any, key: string) => {
acc[key] = params[key];
return acc;
}, {});
return new URLSearchParams(queryParams).toString();
}
1 change: 1 addition & 0 deletions packages/common/src/urls/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./build-url";
export * from "./get-hub-locale-asset-url";
export * from "./get-portal-api-url";
export * from "./get-portal-url";
Expand Down
27 changes: 27 additions & 0 deletions packages/common/test/urls/build-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { buildUrl } from "../../src";

describe("urls", () => {
describe("buildUrl", () => {
it("should build url without query params", done => {
const result = buildUrl({ host: "https://test.com", path: "/foo/bar" });
expect(result).toEqual("https://test.com/foo/bar");
done();
});

it("should build url with query params", done => {
const result = buildUrl({
host: "https://test.com",
path: "/foo/bar",
query: { hello: "world" }
});
expect(result).toEqual("https://test.com/foo/bar?hello=world");
done();
});

it("should build url when host ends with slash", done => {
const result = buildUrl({ host: "https://test.com/", path: "/foo/bar" });
expect(result).toEqual("https://test.com/foo/bar");
done();
});
});
});
17 changes: 7 additions & 10 deletions packages/content/src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ import {
IHubGeography,
IHubRequestOptions,
IBBox,
buildUrl,
getType,
getCategory,
getItemThumbnailUrl
} from "@esri/hub-common";

function buildUrl(base: string, path: string): string {
const baseUrl = base.endsWith("/") ? base : `${base}/`;
const url = new URL(path, baseUrl);
return url.toString();
}

// TODO: make this real; use request() under the hood?
export function hubRequest(url: string, requestOptions?: IHubRequestOptions) {
return fetch(url, {
Expand Down Expand Up @@ -51,10 +46,12 @@ function getContentFromHub(
id: string,
requestOptions?: IHubRequestOptions
): Promise<IHubContent> {
const url = buildUrl(
requestOptions && requestOptions.hubApiUrl,
`/datasets/${id}`
);
const host = requestOptions && requestOptions.hubApiUrl;
// TODO: what if no host? reject?
const url = buildUrl({
host,
path: `/datasets/${id}`
});
return hubRequest(url, requestOptions).then(resp => {
const dataset = resp && resp.data && resp.data[0];
return datasetToContent(dataset, requestOptions.portalSelf);
Expand Down

0 comments on commit b3c3331

Please sign in to comment.