Skip to content

Node.js/Javascript RFC 6570 URI Template implementation that supports both URI expansion and extraction.

License

Notifications You must be signed in to change notification settings

rezigned/uri-template.js

Repository files navigation

uri-template.js

Node.js/Javascript RFC 6570 URI Template implementation that supports both URI expansion and extraction.

Build Status

  • PHP URI Template

Usage

Expansion

A very simple usage

UriTemplate.expand('/{username}/profile', {'username': 'john'});

>> '/john/profile'

More examples (supports all expansion levels defined in RFC6570)

UriTemplate.expand('/search/{term:1}/{term}/{?q*,limit}', {
    term: 'john',
    q: ['a', 'b'],
    limit: 10,
});

>> '/search/j/john/?q=a,b&limit=10'

Extraction

Extract variables from URI.

UriTemplate.extract('/search/{term:1}/{term}/{?q*,limit}', '/search/j/john/?q=a&q=b&limit=10');

>>
(
    'term:1': 'j',
    term: 'john',
    q: [
      'a',
      'b'
    ]
    limit: 10
)

UriTemplate class

You can also instantiate UriTemplate instance via new keyword. Its constructor accepts 2 optional params base uri and default params. Which is very useful when working with API endpoint.

var uri = new UriTemplate('https://api.twitter.com/{version}', {'version': 1.1});
uri.expand('/statuses/show/{id}.json', {'id': '210462857140252672'});

>> https://api.twitter.com/1.1/statuses/show/210462857140252672.json

Installation

In browsers:

bower install uri-template.js
<script src="dist/uri-template.js"></script>
UriTemplate.expand(...)

In Node.js

npm install uri-template.js

UriTemplate = require('uri-template.js');

About

Node.js/Javascript RFC 6570 URI Template implementation that supports both URI expansion and extraction.

Resources

License

Stars

Watchers

Forks

Packages

No packages published