Skip to main content
@talosjs/url wraps the native URL with a friendlier, typed API. It parses a URL into its parts (protocol, subdomain, domain, port, path, queries, fragment), coerces query values to their natural types, and exposes helpers for common parameters like lang, page, limit, and order. A chainable Url class lets you mutate and rebuild URLs, while ReadonlyUrl offers the same read accessors without mutation.

Installation

Add the package with Bun.

Usage

Construct a Url from a string (or a native URL) and read its parts. Query values are automatically parsed: "true"/"false" become booleans and numeric strings become numbers.
Typed convenience accessors return sensible defaults when a parameter is missing, which is handy for list/pagination endpoints.
The Url class is chainable: every setter returns the instance and rebuilds the underlying URL, so you can read the result with toString().
When you only need to read a URL and want to prevent mutation, use ReadonlyUrl, which exposes all the getters but none of the setters.

Where it pays off

The draw here is reading query parameters as numbers and booleans rather than raw strings, and the typed accessors with defaults for list endpoints (page, limit, order, orderBy, lang, and search via q). On the writing side, the chainable API builds and modifies URLs while handling path normalization, default ports, and query rebuilding for you. When you want a value to stay read-only as it gets passed around, ReadonlyUrl exposes the getters and nothing else. For trivial cases, the native URL and URLSearchParams already cover what you need.