Class UrlBuilder<T>

A utility class for constructing URLs with typed query parameters.

This class allows you to dynamically build a URL with typed query parameters. Supports method chaining for setting multiple parameters before building the final URL.

interface Params {
userId: number;
search: string;
isActive: boolean;
}

const url = new UrlBuilder<Params>("https://example.com")
.setParam("userId", 123)
.setParam("search", "test")
.setParam("isActive", true)
.build();

console.log(url); // "https://example.com?userId=123&search=test&isActive=true"

Type Parameters

  • T extends Record<string, any>

    A generic type extending Record<string, any>, representing the expected query parameters.

Constructors

Methods

Constructors

  • Creates an instance of UrlBuilder.

    Type Parameters

    • T extends Record<string, any>

    Parameters

    • baseUrl: string

      The base URL to which query parameters will be appended.

    Returns UrlBuilder<T>

Methods

  • Builds and returns the final URL with the appended query parameters.

    Returns string

    The fully constructed URL with query parameters.

  • Sets a query parameter in the URL.

    Supports method chaining to allow setting multiple parameters fluently.

    Type Parameters

    • K extends string | number | symbol

      A key of T, representing a valid query parameter name.

    Parameters

    • key: K

      The key of the query parameter.

    • value: T[K]

      The value of the query parameter.

    Returns this

    Returns the current UrlBuilder instance for method chaining.