-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🩹 back: tolerate extraneous
/
s in env vars
- Loading branch information
1 parent
00f819f
commit dcadfa4
Showing
2 changed files
with
26 additions
and
1 deletion.
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
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,17 @@ | ||
/** Suitable type for query arguments */ | ||
export type QueryParams = { [key: string]: string | number }; | ||
|
||
/** | ||
* Compose a URL removing and adding slashes and query parameters as warranted. | ||
* Does not encode paths. | ||
*/ | ||
export function joinURL(path: string[], params?: QueryParams) { | ||
let joinedPath = path.map(x => x.replace(/(?:^\/+)|(?:\/+$)/g, "")).join("/"); | ||
if (path[path.length - 1].endsWith("/")) joinedPath += "/"; | ||
const paramEntries = Object.entries(params || {}).filter( | ||
([, value]) => value !== undefined && value !== null, | ||
); | ||
if (paramEntries.length === 0) return joinedPath; | ||
const query = paramEntries.map(p => p.map(encodeURIComponent).join("=")).join("&"); | ||
return joinedPath + (joinedPath.indexOf("?") > -1 ? "&" : "?") + query; | ||
} |