18284: Add shell login command copy button and style webshell button like a button.
[arvados-workbench2.git] / src / services / api / url-builder.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export class UrlBuilder {
6     private readonly url: string = "";
7     private query: string = "";
8
9     constructor(host: string) {
10         this.url = host;
11     }
12
13     public addParam(param: string, value: string) {
14         if (this.query.length === 0) {
15             this.query += "?";
16         } else {
17             this.query += "&";
18         }
19         this.query += `${param}=${value}`;
20         return this;
21     }
22
23     public get() {
24         return this.url + this.query;
25     }
26 }
27
28 export function joinUrls(url0?: string, url1?: string) {
29     let u0 = "";
30     if (url0) {
31         let idx0 = url0.length - 1;
32         while (url0[idx0] === '/') { --idx0; }
33         u0 = url0.substr(0, idx0 + 1);
34     }
35     let u1 = "";
36     if (url1) {
37         let idx1 = 0;
38         while (url1[idx1] === '/') { ++idx1; }
39         u1 = url1.substr(idx1);
40     }
41     let url = u0;
42     if (u1.length > 0) {
43         url += '/';
44     }
45     url += u1;
46     return url;
47 }