UriTemplatesEs
This is a ported library version built to support es standard
.
Can find original library here.
URI Templates (RFC6570) in JavaScript, including de-substitution.
It is tested against the official test suite, including the extended tests (more that 300 tests).
The 'de-substitution' extracts parameter values from URIs. It is also tested against the official test suite (including extended tests).
Usages
Install library using the next command:
npm i uri-templates-es --save
Create a template object:
import { UriTemplate } from 'uri-templates-es';
const template1 = new UriTemplate('/date/{colour}/{shape}/');
const template2 = new UriTemplate('/prefix/{?params*}');
Example of substitution using an object:
const uri1 = template1.fill({colour: 'green', shape: 'round'});
const uri2 = template2.fillFromObject({
params: {a: 'A', b: 'B', c: 'C'}
});
Example of substitution using a callback:
const uri1b = template1.fill(function (varName) {
return 'example_' + varName;
});
Example of guess variables from URI ('de-substitution'):
const uri2b = '/prefix/?beep=boop&bleep=bloop';
const params = template2.fromUri(uri2b);
While templates can be ambiguous (e.g. '{var1}{var2}'
), it will still produce something that reconstructs into the original URI.
It can handle all the cases in the official test suite, including the extended tests:
const template = new UriTemplate('{/id*}{?fields,token}');
const values = template.fromUri('/person/albums?fields=id,name,picture&token=12345');