
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
swagger-router
Advanced tools
An efficient swagger 2 based router with support for multiple APIs. For use in RESTBase.
O(path element)
lookup complexity, monomorphic design with simple fast path./en.wikipedia.org/v1/
and
/de.wikipedia.org/v1/
./{domain:en.wikipedia.org}/v1/
. This feature is especially useful in
prefixes, as it enables the consistent construction of sensible params._ls
parameter.npm install swagger-router
var Router = require('swagger-router');
var router = new Router();
// The main requirement is that each spec has a 'paths' member with some URL
// patterns
var swaggerSpec = {
paths: {
'/': {
get: {
hello: 'A listing'
}
},
'/some/{name}': { // This object is returned as 'value'
get: {
hello: 'world'
}
}
}
};
router.addSpec(swaggerSpec);
// Perform some lookups
console.log(router.lookup('/some/title'));
/*
{
params: {
name: 'title'
},
value: { get: { hello: 'world' } }
}
*/
// Use arrays internally for speed (no escaping / parsing)
router.lookup(['some','path']);
// Trailing slashes set an additional _ls param:
router.lookup(['']); // equivalent: router.lookup('/');
/*
{
params: {
_ls: ['some'],
name: 'title'
},
value: { get: { hello: 'A listing' } }
}
*/
URIs are represented by URI
class, which supports a limited set of features
from URI Template RFC 6570.
{pattern}
- on expansion, looks up a variable named pattern
in params
and substitutes its pct-encoded value. On matching, matches a single element in the path, and
sets params.pattern
to the path element value.{+pattern}
- on expansion, works the same way as simple expression, but doesn't
pct-encode reserved characters and ptc-triplets.
On matching, matches the whole subpath and writes it's value to params.pattern
variable.{/pattern}
- works the same way as simple expression, but on matching the path
element is optional.{pattern:value}
- on matching, matches only uris with path element equal to value
,
and exports value
as params.pattern
variable. On expansion, substitutes value
.These features are optimised and available with URI.expand(params)
method. Additional features
are available with request templating.
Module exports an efficient templating library under Template
class.
Example usage:
var template = new Template({
method: 'put',
uri: '/{domain}/{$.request.headers.location}',
headers: '{$$.merge($.request.headers, {"additional_name": "additional_value"})}'
body: {
field_from_req_body: '{field_name}',
global_reference: '{$.request.headers.header_name}',
field_with_default_value: '{$$.default($.request.params.param_name, "defaultValue")}'
}
});
var request = template.expand({
request: req,
additional_context: context
});
Expressions wrapped in curly braces are considered templates, which are resolved to values
on template expansion. In case some value cannot be resolved, template is expanded to undefined
.
$
references global context (object, passed to the expand
method). It can contain arbitrary number of objects,
but it must at least contain a request
property with an original request.
Short notations are supported, which are resolved to fields of a request part, for example,
'{field_name}'
in template body
would be resolved to '{$.request.body.field_name}'
.
Short notations in uri
would be resolved to $.request.params
.
Braced syntax is supported, so it's possible to write templates like '{$.request.body[$.request.params.some_field]}'
.
Several utility methods are supported:
$$.default(template, defaultValue)
- if template
is resolved, use it's value, otherwise use defaultValue
.$$.merge(template1, template2)
- both templates should be evaluated to objects. The result is an object
with merged properties, but without overriding.$$.strip(object, properties)
- removes field names listed in properties
array from an object
. properties
could also be a string, if a single field should be removed.FAQs
An efficient swagger 2 based router with support for multiple APIs. For use in RESTBase.
The npm package swagger-router receives a total of 570 weekly downloads. As such, swagger-router popularity was classified as not popular.
We found that swagger-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.