-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(url utils): add a utility fn to build URLs from a host, path, an…
…d queryParams AFFECTS PACKAGES: @esri/hub-common @esri/hub-content
- Loading branch information
Showing
4 changed files
with
65 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters