File

projects/wms-framework/src/lib/baseframework/Uri.ts

Description

Represents a URI.

Index

Properties
Methods
Accessors

Constructor

constructor(p1: any, p2?: any)

Creates an instance of Uri.

Parameters :
Name Type Optional
p1 any No
p2 any Yes

Properties

Private originalString
Type : string
Default value : ''

Stores the original URL used to instantitate the Uri.

Private relativeUrl
Type : string
Default value : ''

Stores the relative URL, when the Uri kind is relative.

Private Optional url
Type : URL

Stores the absolute URL when the Uri kind is absolute.

Methods

Private initializeRelative
initializeRelative(url: string)
Parameters :
Name Type Optional
url string No
Returns : void
Private initializeRelativeOrAbsolute
initializeRelativeOrAbsolute(url: string)
Parameters :
Name Type Optional
url string No
Returns : void
Public ToString
ToString()

Gets the absolute unescaped URL represented by this Uri.

Returns : string

{string}

Static TryCreate
TryCreate(url: string, uriKind: UriKind, result: out<Uri>)

Attempts to create a Uri with the given parameters.

Parameters :
Name Type Optional Description
url string No

The absolute or relative URL.

uriKind UriKind No

The kind of URL.

result out<Uri> No

The output URI if creation is successfull.

Returns : boolean

{boolean} true if succeeds, false otherwise.

Accessors

IsAbsoluteUri
getIsAbsoluteUri()

Returns true is this Uri represents an absolute path, false otherwise.

Returns : boolean
AbsoluteUri
getAbsoluteUri()

Gets the absolute escaped URL represented by this Uri.

Returns : string
Host
getHost()

Gets the host part of the URI.

Returns : string
OriginalString
getOriginalString()

Gets the original string from which this URI was constructed from.

Returns : string
Scheme
getScheme()

Gets the scheme (or protocol) part of this URI.

Returns : string
import { out } from '../utils/RefOrOut';

/**
 * Represents the kind of Uri.
 *
 * @export
 * @enum {number}
 * @wEnum System.UriKind
 */
export enum UriKind {
  RelativeOrAbsolute,
  Absolute,
  Relative,
}

/**
 * Represents a URI.
 *
 * @export
 * @class Uri
 * @wType System.Uri
 */
export class Uri {
  /**
   * Stores the absolute URL when the Uri kind is absolute.
   *
   * @private
   * @type {URL}
   * @memberof Uri
   */
  private url?: URL;

  /**
   * Stores the relative URL, when the Uri kind is relative.
   *
   * @private
   * @type {string}
   * @memberof Uri
   */
  private relativeUrl = '';

  /**
   * Stores the original URL used to instantitate the Uri.
   *
   * @private
   * @type {string}
   * @memberof Uri
   */
  private originalString = '';

  /**
   * Creates an instance of Uri. If `uriKind` is not specified,
   * then `uri` must be an absolute URL.
   *
   * @param {string} url A URL.
   * @param {UriKind} [uriKind] The kind of URL.
   * @memberof Uri
   */
  constructor(url: string, uriKind?: UriKind);

  /**
   * Creates an instance of Uri.
   *
   * @param {Uri} base The base URI
   * @param {string} relative A relative path to the base URI.
   * @memberof Uri
   */
  constructor(base: Uri, relative: string);

  /**
   * Creates an instance of Uri.
   *
   * @param {*} p1
   * @param {*} [p2]
   * @memberof Uri
   */
  constructor(p1: any, p2?: any) {
    if (typeof p1 === 'string' && p2 == null) {
      this.url = new URL(p1);
      this.originalString = p1;
    } else if (typeof p1 === 'string' && typeof p2 === 'number') {
      const url = p1;
      const uriKind = p2 as number;
      if (uriKind === UriKind.Absolute) {
        this.url = new URL(url);
        this.originalString = p1;
      } else if (uriKind === UriKind.Relative) {
        this.initializeRelative(url);
      } else if (uriKind === UriKind.RelativeOrAbsolute) {
        this.initializeRelativeOrAbsolute(url);
      } else {
        throw Error('Invalid Uri kind');
      }
    } else if (p1 instanceof Uri && typeof p2 === 'string') {
      const base = p1;
      const relative = p2;
      this.url = new URL(relative, base.url);
      const baseUrl = base.url.href.slice(
        0,
        base.url.href.lastIndexOf('/') + 1
      );
      this.originalString = baseUrl + relative;
    } else {
      throw Error('Invalid constructor parameters for Uri');
    }
  }

  /**
   * Attempts to create a Uri with the given parameters.
   *
   * @static
   * @param {string} url The absolute or relative URL.
   * @param {UriKind} uriKind The kind of URL.
   * @param {out<Uri>} result The output URI if creation is successfull.
   * @return {*}  {boolean} `true` if succeeds, `false` otherwise.
   * @memberof Uri
   */
  public static TryCreate(
    url: string,
    uriKind: UriKind,
    result: out<Uri>
  ): boolean {
    try {
      result.value = new Uri(url, uriKind);
      return true;
    } catch {
      return false;
    }
  }

  /**
   * Returns `true` is this Uri represents an
   * absolute path, `false` otherwise.
   *
   * @readonly
   * @type {boolean}
   * @memberof Uri
   */
  public get IsAbsoluteUri(): boolean {
    return this.url != null;
  }

  /**
   * Gets the absolute escaped URL represented by this Uri.
   *
   * @readonly
   * @type {string}
   * @memberof Uri
   * @throws Will throw an error if this Uri is not absolute.
   */
  public get AbsoluteUri(): string {
    if (this.url != null) {
      return this.url.href;
    } else {
      throw Error('Invalid operation');
    }
  }

  /**
   * Gets the host part of the URI.
   *
   * @readonly
   * @type {string}
   * @memberof Uri
   * @throws Will throw an error if this Uri is not absolute.
   */
  public get Host(): string {
    if (this.url != null) {
      return this.url.hostname;
    } else {
      throw Error('Invalid operation');
    }
  }

  /**
   * Gets the original string from which this URI was constructed from.
   *
   * @readonly
   * @type {string}
   * @memberof Uri
   */
  public get OriginalString(): string {
    return this.originalString;
  }

  /**
   * Gets the scheme (or protocol) part of this URI.
   *
   * @readonly
   * @type {string}
   * @memberof Uri
   * @throws Will throw an error if this Uri is not absolute.
   */
  public get Scheme(): string {
    if (this.url != null) {
      return this.url.protocol.slice(0, -1);
    } else {
      throw Error('Invalid operation');
    }
  }

  /**
   * Gets the absolute unescaped URL represented by this Uri.
   *
   * @return {*}  {string}
   * @memberof Uri
   */
  public ToString(): string {
    if (this.url != null) {
      return decodeURI(this.url.href);
    } else {
      return decodeURI(this.relativeUrl);
    }
  }

  private initializeRelative(url: string) {
    if (/^https?:/.test(url)) {
      throw Error(
        'Invalid URL: can not create a relative Uri with an absolute URL'
      );
    }
    this.relativeUrl = url;
    this.originalString = url;
  }

  private initializeRelativeOrAbsolute(url: string) {
    if (url.startsWith('/')) {
      this.relativeUrl = url;
    } else {
      if (/^https?:/.test(url)) {
        try {
          this.url = new URL(url);
        } catch {
          this.relativeUrl = url;
        }
      } else {
        this.relativeUrl = url;
      }
    }
    this.originalString = url;
  }
}

result-matching ""

    No results matching ""