Getting Started

URL Building

Rezo provides a buildUri() method that constructs URLs from individual components and returns a RezoUri object. This is useful when you need to compose URLs programmatically from a base URL, path segments, query parameters, and authentication credentials.

import rezo from 'rezo';

const uri = rezo.buildUri({
  baseURL: 'https://api.example.com',
  pathname: '/v2/users',
  params: { role: 'admin', active: true },
});

console.log(uri.href);
// "https://api.example.com/v2/users?role=admin&active=true"

BuildUriOptions

The buildUri() method accepts a configuration object with the following properties:

PropertyTypeDescription
urlstringFull URL string. Used as the starting point if provided.
baseURLstringBase URL. Combined with pathname when both are present.
paramsRecord<string, any>Query parameters to append to the URL.
paramsSerializer(params: any) => stringCustom function to serialize params into a query string.
auth{ username: string; password: string }Credentials encoded into the userinfo portion of the URL.
hrefstringFull URL string (alias for url).
protocolstringURL protocol (e.g., 'https:').
hostnamestringHost name without port.
portstring \| numberPort number.
pathnamestringURL path (e.g., '/api/users').
searchstringRaw query string (e.g., '?key=value'). Overridden by params if both are set.
hashstringURL fragment (e.g., '#section').

RezoUri

buildUri() returns a RezoUri object that provides a convenient API for reading and modifying the constructed URL.

Properties

PropertyTypeDescription
hrefstringThe full URL string.
protocolstringThe URL protocol (e.g., 'https:').
hostnamestringThe host name.
portstringThe port number as a string.
pathnamestringThe URL path.
searchstringThe query string including the leading ?.
hashstringThe fragment including the leading #.
originstringThe URL origin (protocol + '//' + host).
hoststringThe host including port if non-default.

params (getter / setter)

Read or replace the query parameters as a plain object:

const uri = rezo.buildUri({ url: 'https://api.example.com/search?q=rezo&page=1' });

// Read params
console.log(uri.params); // { q: 'rezo', page: '1' }

// Replace params
uri.params = { q: 'http-client', page: '2', sort: 'stars' };
console.log(uri.href);
// "https://api.example.com/search?q=http-client&page=2&sort=stars"

auth (getter / setter)

Read or set the userinfo credentials in the URL:

const uri = rezo.buildUri({
  url: 'https://api.example.com',
  auth: { username: 'admin', password: 's3cret' },
});

console.log(uri.href);
// "https://admin:s3cret@api.example.com"

// Read auth
console.log(uri.auth); // { username: 'admin', password: 's3cret' }

// Update auth
uri.auth = { username: 'root', password: 'newpass' };

clone()

Returns a new RezoUri with the same URL. Mutations on the clone do not affect the original:

const original = rezo.buildUri({ url: 'https://example.com/path?a=1' });
const copy = original.clone();

copy.params = { a: '2' };

console.log(original.href); // "https://example.com/path?a=1"
console.log(copy.href);     // "https://example.com/path?a=2"

toObject()

Returns a plain object with all URL components:

const uri = rezo.buildUri({ url: 'https://api.example.com:8080/v1/users?active=true#top' });

console.log(uri.toObject());
// {
//   href: 'https://api.example.com:8080/v1/users?active=true#top',
//   protocol: 'https:',
//   hostname: 'api.example.com',
//   port: '8080',
//   pathname: '/v1/users',
//   search: '?active=true',
//   hash: '#top',
//   origin: 'https://api.example.com:8080',
//   host: 'api.example.com:8080',
//   params: { active: 'true' }
// }

toJSON()

Same as toObject(). Allows RezoUri instances to be serialized with JSON.stringify().


Examples

Building a URL from parts

const uri = rezo.buildUri({
  protocol: 'https:',
  hostname: 'api.example.com',
  port: 8443,
  pathname: '/v2/resources',
  params: { limit: 50, offset: 100 },
});

console.log(uri.href);
// "https://api.example.com:8443/v2/resources?limit=50&offset=100"

Combining baseURL with pathname and params

const uri = rezo.buildUri({
  baseURL: 'https://api.example.com/v1',
  pathname: '/users',
  params: { role: 'admin', sort: '-created' },
});

console.log(uri.href);
// "https://api.example.com/v1/users?role=admin&sort=-created"

Auth encoding

Credentials in the auth option are percent-encoded into the URL’s userinfo section:

const uri = rezo.buildUri({
  url: 'https://registry.example.com/packages',
  auth: { username: 'deploy@ci', password: 'p@ss/word' },
});

console.log(uri.href);
// "https://deploy%40ci:p%40ss%2Fword@registry.example.com/packages"

Custom params serializer

const uri = rezo.buildUri({
  url: 'https://api.example.com/search',
  params: { tags: ['node', 'http', 'typescript'] },
  paramsSerializer: (params) =>
    Object.entries(params)
      .flatMap(([key, val]) =>
        Array.isArray(val) ? val.map(v => `${key}=${v}`) : [`${key}=${val}`]
      )
      .join('&'),
});

console.log(uri.href);
// "https://api.example.com/search?tags=node&tags=http&tags=typescript"