1
import { URL } from 'url';
4
protocol: string | null;
8
query: Record<string, string>;
10
hasTrailingSlash: boolean;
12
constructor(url: string) {
13
const parsedUrl = new URL(url, 'http://localhost');
15
const isProtocolRelative = /^\/\//.test(url);
16
const isRootRelative = /^\/$|^\/[^/]/.test(url);
17
const isPathRelative = /^\./.test(url);
20
!isProtocolRelative && !isRootRelative && !isPathRelative
21
? parsedUrl.protocol.substring(0, parsedUrl.protocol.length - 1)
24
this.host = !isRootRelative && !isPathRelative ? parsedUrl.hostname : null;
25
this.port = parsedUrl.port !== '' ? parsedUrl.port : null;
26
this.path = parsedUrl.pathname.split('/').filter((p) => p !== '');
27
this.query = Object.fromEntries(parsedUrl.searchParams.entries());
28
this.hash = parsedUrl.hash !== '' ? parsedUrl.hash.substring(1) : null;
30
this.hasTrailingSlash = parsedUrl.pathname.length > 1 ? parsedUrl.pathname.endsWith('/') : url.endsWith('/');
33
public isAbsolute(): boolean {
34
return this.protocol !== null && this.host !== null;
37
public isProtocolRelative(): boolean {
38
return this.protocol === null && this.host !== null;
41
public isRootRelative(): boolean {
42
return this.protocol === null && this.host === null;
45
public addPath(...paths: (string | number)[]): Url {
46
const pathToAdd = paths.flatMap((p) => String(p).split('/')).filter((p) => p !== '');
48
for (const pathSegment of pathToAdd) {
49
if (pathSegment === '..') {
51
} else if (pathSegment !== '.') {
52
this.path.push(pathSegment);
56
const lastPath = paths.at(-1);
58
if (pathToAdd.length > 0 && lastPath !== '.' && lastPath !== '..') {
59
this.hasTrailingSlash = typeof lastPath === 'string' && lastPath.endsWith('/');
65
public setQuery(key: string, value: string): Url {
66
this.query[key] = value;
71
public toString({ rootRelative } = { rootRelative: false }): string {
72
const protocol = this.protocol !== null ? `${this.protocol}:` : '';
73
const host = this.host ?? '';
74
const port = this.port !== null ? `:${this.port}` : '';
75
const origin = `${this.host !== null ? `${protocol}//` : ''}${host}${port}`;
77
const path = this.path.length ? `/${this.path.join('/')}` : '';
79
const trailingSlash = this.hasTrailingSlash ? '/' : '';
81
const query = Object.keys(this.query).length !== 0 ? `?${new URLSearchParams(this.query).toString()}` : '';
83
const hash = this.hash !== null ? `#${this.hash}` : '';
85
return `${!rootRelative ? origin : ''}${path}${trailingSlash}${query}${hash}`;