summaryrefslogtreecommitdiff
path: root/shared/utils/src/url.ts
blob: f15792d7575dd04cf011c147623f1e6ccd615caa (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * Remove the scheme and separators from the given URL.
 *
 * @example
 * ```javascript
 * removeScheme('https://music.apple.com/browse') // => 'music.apple.com/browse'
 * removeScheme('apple-music://music.apple.com/browse') // => 'music.apple.com/browse'
 * removeScheme('music.apple.com/browse') // => 'music.apple.com/browse'
 * ```
 */
export function removeScheme(
    url: string | URL | null | undefined,
): string | undefined {
    if (url === null || url === undefined) {
        return undefined;
    }

    return String(url).replace(/^((?:[^:]*:[/]{0,2})|(?::?\/\/))/i, '');
}

/**
 * Strip scheme and host (hostname + port) from a URL, leaving just the path, query
 * params, and hash.
 *
 * @param {string} url The URL possibly containing a host
 * @returns {string} hostlessUrl The url without its host
 */
export function removeHost(
    url: string | URL | null | undefined,
): string | undefined {
    return removeScheme(url)?.replace(/^([^/]*)/i, '');
}

/**
 * Strip query params and fragment from a URL.
 */
export function removeQueryParams(
    url: string | URL | undefined,
): string | undefined {
    if (url === undefined) {
        return undefined;
    }

    const value = String(url);
    const splitIndex = value.indexOf('?');
    return splitIndex >= 0 ? value.slice(0, splitIndex) : value;
}

export function getBaseUrl(): string {
    const currentUrl = new URL(window.location.href);
    return `${currentUrl.protocol}//${currentUrl.host}`;
}

export function buildUrl(props: {
    protocol?: string;
    hostname: string;
    pathname?: string | string[];
    queryParams?: string | Record<string, string>;
    hash?: string;
}): URL {
    const {
        hostname,
        pathname = '/',
        queryParams = {},
        protocol = 'https',
        hash = '',
    } = props;

    // Base URL with domain
    const url = new URL(protocol + '://' + removeScheme(hostname));

    // URL path
    url.pathname = Array.isArray(pathname)
        ? '/' + pathname.map(encodeURIComponent).join('/').replace(/[/]+/, '/')
        : pathname;

    // URL search (a.k.a. queryParams)
    if (typeof queryParams === 'string') {
        url.search = queryParams;
    } else {
        for (const [key, value] of Object.entries(queryParams)) {
            url.searchParams.set(key, value);
        }
    }

    // URL hash
    url.hash = hash;

    return url;
}