Skip to content
Eugene Lazutkin edited this page Jun 17, 2017 · 2 revisions

io() sanitizes queries automatically, both keys and values. But sometimes our URLs may contain variable parameters inline, like that: '/api/clients/11' or '/api/v1/users/Bob%20Smith'.

url() is a function to use with tagged template literals (an ES6 feature). It helps to form a URL by properly sanitizing parameters.

The main API

The function follows standard guidelines, and do not exposes any user-level parameters. It is designed to be used in ES6 environment like that:

import io  from 'heya-io';
import url from 'heya-io/url';

// ...

const client = 'Bob & Jordan & Co';
io.get(url`/api/${client}/details`).then(function (value) {
  // GET /api/Bob%20%26%20Jordan%20%26%20Co/details
  // ...
});

Values of all expressions inside template literals are passed through encodeURIComponent() making them safe for URLs.

Clone this wiki locally