npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swagger-router

v0.7.4

Published

An efficient swagger 2 based router with support for multiple APIs. For use in RESTBase.

Downloads

3,194

Readme

Swagger 2 router

Build
Status

Features

  • O(path element) lookup complexity, monomorphic design with simple fast path.
  • Support for prefix-based 'mounting' of swagger specs. Example: Mount the same spec fragments at /en.wikipedia.org/v1/ and /de.wikipedia.org/v1/.
  • Support for capture of fixed path segments. Example: /{domain:en.wikipedia.org}/v1/. This feature is especially useful in prefixes, as it enables the consistent construction of sensible params.
  • Support for static (purely spec-based) listings. Matching requests ending on a slash are passed an array of matching child paths in the spec in the _ls parameter.

Installation

npm install swagger-router

Usage

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' } }
}
*/

URI templating

URIs are represented by URI class, which supports a limited set of features from URI Template RFC 6570.

Supported URI template expressions:

  • Simple string expression {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.
  • Restricted expression {+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.
  • Optional expression {/pattern} - works the same way as simple expression, but on matching the path element is optional.
  • Fixed expression {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.

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.