URLBuilder class This class is used to build URLs with query parameters.

const urlBuilder = new URLBuilder({
baseUrl: 'https://example.com',
path: '/path/to/resource',
queryBuilder: new QueryBuilder({ page: 1, limit: 10, search: 'hello' }),
});
console.log(urlBuilder.build()); // https://example.com/path/to/resource?page=1&limit=10&search=hello

Constructors

  • Constructor for URLBuilder

    Parameters

    Returns URLBuilder

    const urlBuilder = new URLBuilder({
    baseUrl: 'https://example.com',
    path: '/path/to/resource',
    queryBuilder: new QueryBuilder({ page: 1, limit: 10, search: 'hello' }),
    });

    If the baseUrl is invalid

    If the path is invalid

    If the query parameters are invalid

Accessors

  • get baseUrl(): string
  • Getter for the base URL

    Returns string

    The base URL

    const urlBuilder = new URLBuilder({
    baseUrl: 'https://example.com',
    path: '/path/to/resource',
    query: { page: 1, limit: 10, search: 'hello' },
    });
    console.log(urlBuilder.baseUrl); // https://example.com
  • get path(): string
  • Getter for the path

    Returns string

    The path

    const urlBuilder = new URLBuilder({
    baseUrl: 'https://example.com',
    path: '/path/to/resource',
    });
    console.log(urlBuilder.path); // /path/to/resource
  • get queryBuilder(): QueryBuilder
  • Getter for the query builder

    Returns QueryBuilder

    The query builder

    const urlBuilder = new URLBuilder({
    baseUrl: 'https://example.com',
    path: '/path/to/resource',
    queryBuilder: new QueryBuilder({ page: 1, limit: 10, search: 'hello' }),
    });
    console.log(urlBuilder.queryBuilder); // QueryBuilder { query: { page: 1, limit: 10, search: 'hello' } }

Methods

  • Builds the URL with query parameters

    Returns string

    The URL with query parameters

    const urlBuilder = new URLBuilder({
    baseUrl: 'https://example.com',
    path: '/path/to/resource',
    query: { page: 1, limit: 10, search: 'hello' },
    });
    console.log(urlBuilder.build()); // https://example.com/path/to/resource?page=1&limit=10&search=hello
  • Replace path parameters with the given parameters

    Parameters

    • params: Record<string, string>

      The parameters to replace

    Returns URLBuilder

    The URLBuilder instance

    const urlBuilder = new URLBuilder({
    baseUrl: 'https://example.com',
    path: '/path/:id',
    });
    console.log(urlBuilder.path); // /path/:id
    urlBuilder.replacePathParams({ id: '123' });
    console.log(urlBuilder.path); // /path/123

    If the path parameter is missing