-
Notifications
You must be signed in to change notification settings - Fork 26
/
url.ts
75 lines (66 loc) · 2.23 KB
/
url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {ensureNonEmptyString} from './ensure';
import qs from 'qs';
import urlParse from 'url-parse';
/**
* Helper to create a new URL by appending parameters to a base URL.
*
* The input URL may or may not having existing parameters.
*
* @example
* ```
* // Returns `"/someApi/someEndpoint?token=asdf&limit=5"`
* let url = withQueryParams("/someApi/someEndpoint", {token: "asdf", limit: 5});
* ```
*/
export function withQueryParams(url: string, params?: {[key: string]: any}): string {
if (!params) {
return url;
}
const parsedUrl = urlParse(url);
// Merge the params together
const updatedParams = Object.assign({}, qs.parse(parsedUrl.query as any, {ignoreQueryPrefix: true}), params);
parsedUrl.set('query', qs.stringify(JSON.parse(JSON.stringify(updatedParams)), {addQueryPrefix: true}));
return parsedUrl.toString();
}
/**
* Helper to take a URL string and return the parameters (if any) as a JavaScript object.
*
* @example
* ```
* // Returns `{token: "asdf", limit: "5"}`
* let params = getQueryParams("/someApi/someEndpoint?token=asdf&limit=5");
* ```
*/
export function getQueryParams(url: string): {[key: string]: any} {
const parsedUrl = urlParse(url);
// Merge the params together
return qs.parse(parsedUrl.query as any, {ignoreQueryPrefix: true});
}
/**
* Joins all the tokens into a single URL string separated by '/'. Zero length tokens cause errors.
* @param tokens Zero or more tokens to be combined. If token doesn't end with '/', one will be added as the separator
*/
export function join(...tokens: string[]): string {
if (!tokens || !tokens.length) {
return '';
}
const combinedTokens: string[] = [];
for (const token of tokens) {
ensureNonEmptyString(token);
if (combinedTokens.length === 0) {
combinedTokens.push(token);
} else {
// Ensure tokens (other than the first) don't have leading slashes
combinedTokens.push(token.replace(/^\/+/, ''));
}
if (!token.endsWith('/')) {
combinedTokens.push('/');
}
}
const combined = combinedTokens.join('');
if (!tokens[tokens.length - 1].endsWith('/')) {
// User didn't provide token with /, strip out the last one
return combined.slice(0, combined.length - 1);
}
return combined;
}