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

routeswitch

v0.6.3

Published

A fast regexp-based router. Supports Swagger 2 specs.

Downloads

64

Readme

RouteSwitch

Simple and fast regexp-based switcher on regexps or URL patterns. Supports building a switcher from Swagger 2.0 API specs.

Build
-Status

Installation

npm install routeswitch

Documentation

Path specs are defined in a subset of RFC 6570 URL patterns:

  • /{foo}/{bar} -- matches two non-empty path components
  • /{foo}/{bar}/ -- only matches with trailing slash
  • /foo{/bar} -- optionally matches a slash and a path component, if not empty
  • /{+foo} -- matches any non-empty path, including slashes

In the event of route overlaps, the most specific & shortest routes will win:

  1. regexps
  2. paths with fixed segments
  3. paths with templated segments

Examples:

  • /foo/{bar} gets a higher precedence than /{some}/{thing} and /{some}

Construction

RouteSwitch.fromDirectory(path, [options]) -> Promise<RouteSwitch>

Loads all modules in a directory tree. Modules can either directly export a Swagger 2.0 spec with optional additional data (such as a reference to a handler), or they can export a function returning a promise for the spec. Returns a promise for a RouteSwitch.

By default, RouteSwitch loads each handler by passing its path to require(). This can be overridden by providing a custom loader, implemented as a function that takes a path and returns a spec fragment, and included in the optional options object under the loader key:

RouteSwitch.fromDirectory(path, { loader: myLoaderFn })

RouteSwitch.fromHandlers(specs) -> RouteSwitch

Builds a RouteSwitch directly from an array of spec fragments.

new RouteSwitch(routes) -> RouteSwitch

Low-level construction. Routes are objects with the following members:

  • pattern: either a RegExp, or a URL pattern
  • methods: an arbitrary object, which will be returned as a member on successful .match(uri). Typically this is the object providing the method handlers for the route defined by pattern.

Dynamic route addition / removal

RouteSwitch.addRoute(route)

Add a route to a RouteSwitch instance.

RouteSwitch.removeRoute(route)

Remove a route from a RouteSwitch instance.

Matching

RouteSwitch.match(uri) -> (null | object)

Returns null when there is no match. On match, it returns an object containing

  • pattern: the matched URL pattern
  • methods: the original Swagger spec object defined for this pattern, keyed on method (lowercase)
  • params: Named parameters defined in the URL pattern

Example spec fragment

{
    paths: {
        '/v1/{domain}': {
            put: {
                summary: "Create or update a domain",
                // optionally, more swagger docs optionally
                request_handler: this.putDomain.bind(this)
            }
        },
        '/v1/{domain}/': {
            get: {
                summary: "List buckets and tables for a domain",
                request_handler: this.listBuckets.bind(this)
            }
        },
        '/v1/{domain}/{bucket}': {
            put: {
                summary: "Create or update a bucket",
                request_handler: this.putBucket.bind(this)
            },
            get: {
                summary: "Get bucket metadata",
                request_handler: this.getBucket.bind(this)
            }
        },
        '/v1/{domain}/{bucket}/': {
            get: {
                request_handler: this.handleAll.bind(this)
            }
        },
        '/v1/{domain}/{bucket}/{+rest}': {
            all: {
                request_handler: this.handleAll.bind(this)
            }
        }
    }
};