@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
198 lines (196 loc) • 6.29 kB
JavaScript
/** Represents an object that builds Uniform Resource Identifiers (URIs). */
export class UriBuilder {
segments = new Array();
queryParams = new Map();
/**
* Initializes a new instance of the UriBuilder.
* @param uri The optional string or URL to initialize the builder from.
*/
constructor(uri) {
if (uri === undefined) {
this.fragment = '';
this.host = '';
this.scheme = '';
this.port = -1;
return;
}
const url = typeof uri === 'string' ? new URL(uri) : uri;
this.fragment = url.hash ? url.hash.substring(1) : '';
this.host = url.hostname || '';
this.scheme = url.protocol ? url.protocol.substring(0, url.protocol.length - 1) : '';
this.port = url.port ? this.port = Number.parseInt(url.port, 10) : -1;
if (url.pathname) {
const paths = url.pathname.substring(1).split('/');
for (let i = 0; i < paths.length; i++) {
const segment = paths[i];
if (segment) {
this.segments.push(segment);
}
}
}
url.searchParams.forEach((v, k) => {
this.queryParams.set(k, v);
});
}
/** Gets or sets the fragment portion of the URI. */
fragment;
/** Gets or sets the Domain Name System (DNS) host name or IP address of a server. */
host;
/** Gets the path to the resource referenced by the URI. */
get path() {
return this.segments.join('/');
}
/** Sets the path to the resource referenced by the URI. */
set path(value) {
this.segments = new Array();
if (!value) {
return;
}
value = value.trim();
if (value.length === 0) {
return;
}
if (value[0] === '/') {
value = value.substring(1, value.length);
}
if (value[value.length - 1] === '/') {
value = value.substring(0, value.length - 1);
}
const newSegments = value.split('/');
for (let i = 0; i < newSegments.length; i++) {
this.segments.push(newSegments[i]);
}
}
/** Gets or sets the port number of the URI. */
port;
/** Gets any query information included in the URI. */
get query() {
let value = '';
const iterator = this.queryParams.entries();
let result = iterator.next();
if (!result.done) {
let [k, v] = result.value;
value += `${k}=${v}`;
result = iterator.next();
while (!result.done) {
[k, v] = result.value;
value += `&${k}=${v}`;
result = iterator.next();
}
}
return value;
}
/** Sets the query information included in the URI. */
set query(value) {
this.queryParams.clear();
if (!value) {
return;
}
value = value.trim();
if (value[0] === '?') {
value = value.substring(1).trim();
}
if (value.length === 0) {
return;
}
const pairs = value.split('&');
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].trim().split('=');
this.queryParams.set(pair[0], pair[1]);
}
}
/** Gets or sets the scheme name of the URI. */
scheme;
/** Gets the URL instance constructed by the builder. */
get uri() {
return new URL(this.toString());
}
get usingDefaultPort() {
if (!this.scheme) {
return false;
}
// non-exhaustive list of common application protocol ports
switch (this.scheme.toUpperCase()) {
case 'HTTP':
return this.port === 80;
case 'HTTPS':
return this.port === 443;
case 'FTP':
return this.port === 20 || this.port === 21;
case 'SSH':
return this.port === 22;
case 'TELNET':
return this.port === 23;
case 'SMTP':
return this.port === 25;
case 'DNS':
return this.port === 53;
case 'POP3':
return this.port === 113;
case 'LDAP':
return this.port === 389;
}
return false;
}
/**
* Appends the specified segment to the path.
* @param segment The segment to append.
*/
appendSegment(segment) {
this.segments.push(segment);
}
/**
* Sets the specified name/value pair in the query string.
* Duplicate query parameters are not supported.
* @param name The name of the query parameter.
* @param value The value of the query parameter.
*/
setQueryParameter(name, value) {
this.queryParams.set(name, value);
}
/** Returns the string equivalent of the builder. */
toString() {
let uri = '';
if (this.scheme) {
uri = this.scheme;
}
if (this.host) {
if (uri.length > 0) {
uri += '://';
}
uri += this.host;
if (this.port >= 0 && !this.usingDefaultPort) {
uri += ':' + this.port.toString();
}
}
if (this.segments.length > 0) {
if (uri.length > 0) {
uri += '/';
}
uri += this.path;
if (this.queryParams.size > 0) {
uri += '?' + this.query;
}
if (this.fragment) {
uri += '#' + this.fragment;
}
}
return uri;
}
/** Creates and returns a deep copy of the current builder. */
clone() {
const builder = new UriBuilder();
builder.fragment = this.fragment;
builder.host = this.host;
builder.scheme = this.scheme;
builder.port = this.port;
for (let i = 0; i < this.segments.length; i++) {
builder.segments.push(this.segments[i]);
}
this.queryParams.forEach((v, k) => {
builder.queryParams.set(k, v);
});
return builder;
}
}
//# sourceMappingURL=uri-builder.js.map