https://github.com/rezigned/uri-template.js
Node.js/Javascript RFC 6570 URI Template implementation that supports both URI expansion and extraction.
https://github.com/rezigned/uri-template.js
Last synced: about 1 year ago
JSON representation
Node.js/Javascript RFC 6570 URI Template implementation that supports both URI expansion and extraction.
- Host: GitHub
- URL: https://github.com/rezigned/uri-template.js
- Owner: rezigned
- License: mit
- Created: 2014-05-04T18:11:54.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2020-07-30T15:05:38.000Z (over 5 years ago)
- Last Synced: 2025-03-25T10:21:32.505Z (about 1 year ago)
- Language: CoffeeScript
- Size: 90.8 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# uri-template.js
Node.js/Javascript [RFC 6570 URI Template](http://tools.ietf.org/html/rfc6570) implementation that supports both URI expansion and extraction.
[](https://travis-ci.org/rezigned/uri-template.js)
* [PHP](https://github.com/rize/UriTemplate) URI Template
## Usage
### Expansion
A very simple usage
```js
UriTemplate.expand('/{username}/profile', {'username': 'john'});
>> '/john/profile'
```
More examples (supports all expansion levels defined in RFC6570)
```js
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.
```js
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.
```js
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:
```bash
bower install uri-template.js
```
```html
UriTemplate.expand(...)
```
In Node.js
```bash
npm install uri-template.js
UriTemplate = require('uri-template.js');
```