-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.ts
More file actions
43 lines (34 loc) · 796 Bytes
/
url.ts
File metadata and controls
43 lines (34 loc) · 796 Bytes
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
interface Url {
url?: URL;
setBaseUrl(baseUrl: string): void;
addPath(path: string): void;
addQueryParam(name: string, value: string): void;
getUrl(): string | null;
reset(): void;
}
export class UrlBuilder implements Url {
url?: URL;
constructor(baseUrl: string) {
this.setBaseUrl(`${baseUrl}?`);
}
setBaseUrl(baseUrl: string) {
this.url = new URL(baseUrl);
}
addPath(path: string) {
if (this.url) {
this.url.pathname = this.url.pathname.concat(path);
}
}
addQueryParam(name: string, value: string) {
this.url?.searchParams.set(name, value);
}
getUrl() {
if (!this.url || !this.url.toString) return null;
const url = this.url.toString();
this.reset();
return url;
}
reset() {
this.url = undefined;
}
}