Skip to main content

API Reference

Complete API documentation for all fast-url functions with detailed examples and type signatures
3 min read

Type Definitions#

ParamMap#

The base type for parameter objects used throughout the API.

All parameter values are converted to strings during URL encoding. The following types are supported:

  • string - Used as-is (after URL encoding)
  • number - Converted to string
  • boolean - Converted to 'true' or 'false'
  • Array - Each element becomes a separate parameter with the same key
  • null / undefined - Filtered out and not included in the URL

Main Functions#

createUrl()#

The primary function for building URLs with multiple overloads for different use cases.

Signature 1: Base Template with Parameters#

Build a URL using only a template string with path parameters. Unused parameters become query parameters.

Parameters:

  • baseTemplate - A URL template containing zero or more :param placeholders
  • params - Object with properties that correspond to the :params in the template

Returns: A complete URL with path parameters substituted and unused parameters as query string

Example:

Signature 2: Base URL with Path#

Concatenate a base URL and path using / as separator. Handles duplicate slashes automatically.

Parameters:

  • baseUrl - The first part of the URL
  • path - The second part of the URL to append

Returns: Concatenated URL with exactly one / separator

Example:

Signature 3: Complete URL Building#

Build a complete URL by combining base URL, path template, and parameters.

Parameters:

  • baseUrl - The base URL (protocol + domain)
  • pathTemplate - Path template with optional :param placeholders
  • params - Object with path and query parameters

Returns: Complete URL with substituted path parameters and query string

Example:

Note

Path vs Query Parameters

Parameters that match :param placeholders in the path template are used for path substitution. All other parameters become query string parameters.


Utility Functions#

query()#

Build a query string from a parameter object.

Parameters:

  • params - Object to convert into a query string

Returns: URL-encoded query string (without leading ?)

Examples:

Use Cases:

  • Building query strings independently
  • Updating existing URLs with new parameters
  • Creating search or filter parameters

subst()#

Substitute path parameters in a template string without building a full URL.

Parameters:

  • template - String containing :param placeholders
  • params - Object with keys matching the placeholders

Returns: Template with all :param placeholders replaced with URL-encoded values

Examples:

Errors:

Use Cases:

  • Building URL paths without query strings
  • Template rendering
  • Path parameter validation

join()#

Join two strings using a separator, ensuring exactly one occurrence at the boundary.

Parameters:

  • part1 - First string
  • separator - Separator character(s)
  • part2 - Second string

Returns: Joined string with exactly one separator

Examples:

Use Cases:

  • Safe URL concatenation
  • Building paths from segments
  • Joining strings with any separator

Advanced Examples#

Building Complex API Clients#

Dynamic Route Building#

Pagination Helper#

Performance Notes#

fast-url is optimized for performance:

  • Query string encoding: Matches Node.js performance with optimized implementations
  • Direct constant access: Better V8 JIT optimization
  • Minimal allocations: Efficient memory usage
  • Zero dependencies: Only relies on fast-querystring
Check

Benchmarks

In performance benchmarks, fast-url achieves 5-20% better performance than the original urlcat implementation in many scenarios while maintaining full compatibility.

Browser and Node.js Compatibility#

fast-url works in all modern JavaScript environments:

  • ✅ Node.js 14+
  • ✅ Bun
  • ✅ Deno
  • ✅ Modern browsers (Chrome, Firefox, Safari, Edge)
  • ✅ React Native
  • ✅ Electron