transform-url
Version:
Build URLs by transforming a template with params.
112 lines (81 loc) • 3.57 kB
Markdown
//img.shields.io/npm/v/transform-url.svg?style=flat-square)](https://www.npmjs.com/package/transform-url)
[](https://github.com/godaddy/transform-url/actions/workflows/ci.yml)
[](https://coveralls.io/r/godaddy/transform-url?branch=master)
Build URLs by transforming a template with params.
```bash
yarn add transform-url
```
or
```bash
npm install --save transform-url
```
The first argument to `transformUrl` is a URL template, with the second being
a params object of key/value pairs.
```js
const transformUrl = require('transform-url');
const params = {
userId: 'user-1234',
orderId: 'order-6789'
};
const url = transformUrl('https://example.com', params);
// https://example.com?orderId=order-6789&userId=user-1234
```
If you want some params to be applied as path params, add placeholders to the
URL template.
```js
const url = transformUrl('https://example.com/api/users/:userId/orders/:orderId', params);
// https://example.com/api/users/user-1234/orders/order-6789
```
Any remaining params not specified in the URL template, will be applied as
query params.
```js
const url = transformUrl('https://example.com/api/users/:userId', params);
// https://example.com/api/users/user-1234?orderId=order-6789
```
Besides path and query params, placeholders can be added to any other part of
the URL template.
```js
const moreParams = {
...params,
scheme: 'https',
domain: 'example.com',
hash: 'somePlace'
};
const url = transformUrl('{scheme}://{domain}/api/users/:userId#{hash}', moreParams);
// https://example.com/api/users/user-1234?orderId=order-6789#somePlace
```
URL template placeholders can use either `:colon` or `{curlyBraces}` matchers.
Additionally, it is possible to provide a custom matcher via options.
```js
const options = {
matcher: /\$(\w+)/g // match `$paramName`
};
const url = transformUrl('$scheme://$domain/api/users/$userId#$hash', moreParams, options);
// https://example.com/api/users/user-1234?orderId=order-6789#somePlace
```
For additional options, see the API Reference below.
<a name="transformUrl"></a>
Transform URL templates with provided params.
Params that are not placeholders in the url template will be append as query params.
Urls with a port number will be respected, for example `http://example.com:8080`
**Kind**: global function
**Returns**: <code>String</code> - transformed url
| Param | Type | Description |
| --- | --- | --- |
| urlTemplate | <code>String</code> | URL template to be transformed with params. URL template placeholders can use either `:colon` or `{curlyBraces}` matchers. |
| params | <code>Object</code> | Key/value pairs to be applied as path and/or query params. Required to include params for all placeholders. |
| [options] | <code>Object</code> | Optional configuration to output results. |
| [options.matcher] | <code>RegExp</code> | Specify regular expression for custom placeholders in URL templates. |
<!-- Links -->
[ ]:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
[ ]:https://github.com/kevva/strict-uri-encode
[![Version npm](https: