| title | description |
|---|---|
URL Templates |
A URL Template validator, expander and inspector |
A URL Template validator, expander and inspector — fully RFC 6570 compliant. Passes all the 252 tests from the uritemplate-test suite. Minimal, zero dependency, sync API; exposes AST for linters/interfaces and both validated and non-validated expansion paths.
npm i url-templatesThis library provides the following url-template functions:
const { isUrlTemplate } = require('url-templates');
try {
console.log('valid:', isUrlTemplate('/users/{id}')); // true
} catch (error) {
console.error('invalid:', error.message);
}Note:
It returns true or throws an error.
const { inspect } = require('url-templates');
try {
console.dir(inspect('/search{?q*,lang:2}'), { depth: null });
// [ '/search', { '?': [ { key: 'q', explode: true }, { key: 'lang', limit: 2 } ] } ]
} catch (error) {
console.error('invalid:', error.message);
}Note:
Same as with isUrlTemplate, but if valid returns the parsed AST instead of true.
const { parseTemplate } = require('url-templates');
try {
console.log(parseTemplate('/items/{id}').expand({ id: 42 })); // '/items/42'
} catch (error) {
console.error('parse/validation error:', error.message);
}Note:
If valid returns the expand(vars) function which returns the expanded url-template. Otherwise, it throws an error. The expand function also throws error if limit is defined on objects (isUrlTemplate function cannot know that without runtime vars).
const { compile } = require('url-templates');
console.log(compile('/broken{').expand({})); // returns '/broken{'; invalid parts left for postprocessing
console.log(compile('/good{id}').expand({id:42})); // returns '/good42'; Note: Returns a usable expander without validation (for cases where validation is done elsewhere, or for the cases where some sort of postprocessing will follow).