uri-templates-es
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

UriTemplatesEs

Last release version   Pipeline info   Total open issues   Total downloads by npm   License info  

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:

// '/categories/green/round/'
const uri1 = template1.fill({colour: 'green', shape: 'round'});
 
// '/prefix/?a=A&b=B&c=C
const uri2 = template2.fillFromObject({
    params: {a: 'A', b: 'B', c: 'C'}
});

Example of substitution using a callback:

// '/categories/example_colour/example_shape/'
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);
/*
{
  params: {
    beep: 'boop',
    bleep: 'bloop'
    
  }
*/

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');
/*
{
  id: ['person', 'albums'],
  fields: ['id', 'name', 'picture'],
  token: '12345'
}
*/

Package Sidebar

Install

npm i uri-templates-es

Weekly Downloads

513

Version

1.0.0

License

MIT

Unpacked Size

58.8 kB

Total Files

8

Last publish

Collaborators

  • lagoshny