Represents a valid URL with parsing capabilities.

URL is immutable and automatically validates the format. Provides convenient access to URL components like protocol, domain, path, etc.

const result = URL.create('https://example.com/path?query=value');
if (result.isSuccess) {
const url = result.value;
console.log(url.protocol); // "https:"
console.log(url.domain); // "example.com"
console.log(url.path); // "/path"
}

Hierarchy (View Summary)

Accessors

  • get protocol(): string

    Gets the protocol of the URL (e.g., "https:").

    Returns string

    const url = URL.create('https://example.com').unwrap();
    console.log(url.protocol); // "https:"
  • get domain(): string

    Gets the hostname (domain) of the URL.

    Returns string

    const url = URL.create('https://example.com:8080').unwrap();
    console.log(url.domain); // "example.com"
  • get port(): string

    Gets the port of the URL.

    Returns string

    const url = URL.create('https://example.com:8080').unwrap();
    console.log(url.port); // "8080"
  • get path(): string

    Gets the path of the URL.

    Returns string

    const url = URL.create('https://example.com/path/to/resource').unwrap();
    console.log(url.path); // "/path/to/resource"
  • get queryString(): string

    Gets the query string of the URL.

    Returns string

    const url = URL.create('https://example.com?foo=bar&baz=qux').unwrap();
    console.log(url.queryString); // "?foo=bar&baz=qux"
  • get hash(): string

    Gets the hash/fragment of the URL.

    Returns string

    const url = URL.create('https://example.com#section').unwrap();
    console.log(url.hash); // "#section"

Methods

  • Compares this Value Object with another for equality. Value Objects are equal if they are of the same type and all equality components match.

    Parameters

    • other: ValueObject

      The other Value Object to compare with

    Returns boolean

    true if the Value Objects are equal, false otherwise

  • Creates a URL instance with validation.

    Parameters

    • value: string

      The URL string

    Returns Result<URL, DomainError>

    Success with URL or Failure with DomainError

    const result = URL.create('https://example.com');
    if (result.isSuccess) {
    console.log(result.value.value); // "https://example.com/"
    }

    const invalid = URL.create('not-a-url');
    if (invalid.isFailure) {
    console.log(invalid.error.code); // "INVALID_URL"
    }
  • Gets a specific query parameter value.

    Parameters

    • key: string

      The query parameter key

    Returns null | string

    The parameter value or null if not found

    const url = URL.create('https://example.com?foo=bar&baz=qux').unwrap();
    console.log(url.getQueryParam('foo')); // "bar"
    console.log(url.getQueryParam('missing')); // null
  • Gets all query parameters as an object.

    Returns Record<string, string>

    An object with all query parameters

    const url = URL.create('https://example.com?foo=bar&baz=qux').unwrap();
    console.log(url.getQueryParams()); // { foo: 'bar', baz: 'qux' }
  • Checks if the URL uses HTTPS protocol.

    Returns boolean

    true if the protocol is HTTPS

    const url = URL.create('https://example.com').unwrap();
    console.log(url.isSecure()); // true
  • Checks if the URL belongs to a specific domain.

    Parameters

    • domain: string

      The domain to check

    Returns boolean

    true if the URL belongs to the domain

    const url = URL.create('https://api.example.com').unwrap();
    console.log(url.belongsToDomain('example.com')); // true
    console.log(url.belongsToDomain('other.com')); // false
  • Returns the components that determine equality for this Value Object. Two Value Objects are equal if all their equality components are equal.

    Returns unknown[]

    An array of values used for equality comparison

Properties

value: string