Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Url

Implements a primitive that represents a URL value. It is a string primitive so can be passed to any API that requires a string or string primitive. It also provides the WHATWG properties.

The Url primitive needs a global URL implementation of the WHATWG URL specification. This can easily be added on modern Node versions:

import { URL } from 'url';
global.URL = URL

The UMD module automatically performs this installation.

However, if native Node modules cannot be used (React Native) the url-polyfill package can be used to install a pure JavaScript version of the API.

example
const url = new Url('https://abc:xyz@example.com:899/some/path?param=one#hash');
console.log(url.origin);  // 'https://example.com';
console.log(url.host);  // 'example.com:899';
console.log(url.hostname);  // 'example.com';
console.log(url.username);  // 'abc';
console.log(url.password);  // 'xyz';
console.log(url.search);  // '?param=one';
console.log(url.searchParams.get('param'));  // 'one';
console.log(url.protocol);  // 'https:';
console.log(url.port);  // '899';
console.log(url.href);  // 'https://abc:xyz@example.com:899/some/path?param=one#hash';
console.log(url.toString());  // 'https://abc:xyz@example.com:899/some/path?param=one#hash';
console.log(url[Symbol.toPrimitive]('string'));  // 'https://abc:xyz@example.com:899/some/path?param=one#hash';

Hierarchy

  • Url

Implements

Index

Constructors

constructor

Accessors

hash

  • get hash(): string
  • set hash(value: string): void
  • Gets the hash portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.hash);  // '#hash';
    

    Returns string

  • Gets the hash portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.hash = 'value';
    console.log(url.href);  // 'https://abc:xyz@example.com:80/some/path?param=one#value';
    

    Parameters

    • value: string

      the hash value to set

    Returns void

host

  • get host(): string
  • set host(value: string): void
  • Gets the host portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.host);  // 'example.com:80';
    

    Returns string

  • Gets the host portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.host = 'value';
    console.log(url.href);  // 'https://abc:xyz@value/some/path?param=one#value';
    

    Parameters

    • value: string

      the host value to set

    Returns void

hostname

  • get hostname(): string
  • set hostname(value: string): void
  • Gets the hostname portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.hostname);  // 'example.com';
    

    Returns string

  • Gets the hostname portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.hostname = 'value';
    console.log(url.href);  // 'https://abc:xyz@value:80/some/path?param=one#value';
    

    Parameters

    • value: string

      the hostname value to set

    Returns void

href

  • get href(): string
  • set href(value: string): void
  • Gets the href, which is equivalent to calling toString()

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.href);  // 'https://abc:xyz@example.com:80/some/path?param=one#hash';
    

    Returns string

  • Gets the href portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.href = 'http://value.com';
    console.log(url.href);  // 'http://value.com';
    

    Parameters

    • value: string

      the href value to set

    Returns void

origin

  • get origin(): string
  • Gets the origin portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.origin);  // 'https://example.com';
    

    Returns string

password

  • get password(): string
  • set password(value: string): void
  • Gets the password portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.password);  // 'xyz';
    

    Returns string

  • Gets the password portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.password = 'value';
    console.log(url.href);  // 'https://abc:value@example.com:80:80/some/path?param=one#value';
    

    Parameters

    • value: string

      the password value to set

    Returns void

pathname

  • get pathname(): string
  • set pathname(value: string): void
  • Gets the pathname portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.pathname);  // '/some/path'
    

    Returns string

  • Gets the pathname portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.pathname = 'value';
    console.log(url.href);  // 'https://abc:xyz@example.com:80/value?param=one#hash';
    

    Parameters

    • value: string

      the pathname value to set

    Returns void

port

  • get port(): string
  • set port(value: string): void
  • Gets the port portion of the URL. Default ports are automatically transformed to an empty string

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.port);  // '80'
    

    Returns string

  • Gets the port portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.port = '1024';
    console.log(url.href);  // 'https://abc:xyz@example.com:1024/some/path?param=one#hash'
    

    Parameters

    • value: string

      the port value to set

    Returns void

protocol

  • get protocol(): string
  • set protocol(value: string): void
  • Gets the protocol portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.protocol);  // 'https:'
    

    Returns string

  • Gets the protocol portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.protocol = 'ftp:';
    console.log(url.href);  // 'ftp://abc:xyz@example.com:1024/some/path?param=one#hash'
    

    Parameters

    • value: string

      the protocol value to set

    Returns void

search

  • get search(): string
  • set search(value: string): void
  • Gets the search portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.search);  // '?param=one'
    

    Returns string

  • Gets the search portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.search = 'whoop=yep';
    console.log(url.href);  // 'ftp://abc:xyz@example.com:1024/some/path?whoop=yep#hash'
    

    Parameters

    • value: string

      the search value to set

    Returns void

searchParams

  • get searchParams(): IReadonlySearchParameters
  • Gets the search parameters portion of the URL. Cannot be set, use the search property

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.searchParams);  // '{ 'param' => 'one' }'
    console.log(url.searchParams.get('param'));  // 'one
    

    Returns IReadonlySearchParameters

username

  • get username(): string
  • set username(value: string): void
  • Gets the username portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    console.log(url.username);  // 'abc'
    

    Returns string

  • Gets the username portion of the URL

    example
    const url = new Url('https://abc:xyz@example.com:80/some/path?param=one#hash');
    url.username = 'value';
    console.log(url.href);  // 'https://value:xyz@example.com:80/some/path?param=one#hash'
    

    Parameters

    • value: string

      the username value to set

    Returns void

value

  • get value(): string
  • set value(value: string): void
  • Returns string

  • Parameters

    • value: string

    Returns void

Methods

__@iterator

  • __@iterator(): IterableIterator<string>

__@toPrimitive

  • __@toPrimitive(hint: "string" | "number" | "default"): string | number
  • Converts the object into a primitive

    Parameters

    • hint: "string" | "number" | "default"

    Returns string | number

__@toStringTag

  • __@toStringTag(): string

get

  • get(): string

set

  • set(value: string | Any): IString
  • Parameters

    • value: string | Any

    Returns IString

toJSON

  • toJSON(_?: string): any | number | string | boolean | null
  • Converts the primitive into it's JSON representation

    Parameters

    • Default value _: string = ""

    Returns any | number | string | boolean | null

    the object to serialize with JSON.stringify

toString

  • toString(): string
  • Converts the primitive into it's string representation. Often has a unit value associated with it such as 125ms

    Returns string

valueOf

  • valueOf(): string