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

osh-route

v0.0.7

Published

openscihub.org: abstract routes for clients and servers

Downloads

11

Readme

Route

A Route is basically a translator between properties and URIs. Its two most important functions are complementary: route.uri(props) and route.props(uri). This allows you to think about URIs as POJOs, which is what you work with in your scripts.

Installation

npm install osh-route

Usage

Example:

var Route = require('osh-route');

var userRoute = new Route({
  path: '/users/<user>',
  params: {
    user: /^[a-z]+$/
  }
});

The path format keeps it simple; everything is literal in the string except for parameter names between < >. A parameter name should match an entry in the params object, which maps parameter names to RegExps. RegExps have the following restrictions/allowances:

  • May start with ^ (for RegExp reuse).
  • May end with $ (for RegExp reuse).
  • No capturing groups (if sub-parameters are needed, add another <param> to the path template)

Let's use the route.

userRoute.uri({user: 'tony', age: '31'});  // '/users/tony?age=31'
userRoute.uri({user: 'TONY'});             // undefined
userRoute.props('/users/tony?age=31');     // {user: 'tony', age: '31'}
userRoute.props('/users/TONY');            // undefined

// Also this stuff...
userRoute.path({user: 'tony', age: '31'}); // '/users/tony'
userRoute.query('/users/tony?age=31');     // {age: '31'}
userRoute.qs({user: 'tony', age: '31'});   // '?age=31'

The rules for generating a URI from properties are:

  • If a path parameter is missing or invalid, return undefined.
  • Any property that is not a path parameter is added to the query string.

The rules for obtaining props from a URI string are:

  • If the path does not match, return undefined.
  • If the path matches, return an object with a key for every path/query parameter.

Configuration

A Route is instantiated with a config object, that accepts the following properties:

  • path {String}: Path template. Parameters are specified by <param_name> and should correspond with an entry in the params config property.
  • params {Object}: An object mapping parameter names (specified in the path template) to validation RegExps or functions.
  • parent {Object|Route}: Route instance (or Route config object) to prepend to the current Route.

Properties

Of a Route instance.

route.PATH

The path RegExp created from the template and set of parameters.

route.uri(props)

Convert a props object into a URI string. The query section is always ordered by query key name, so that a uri can act as a unique id. If a path parameter is missing from props or exists but is invalid, undefined is returned.

route.props(uri)

Convert a uri string into a plain old javascript object. If the path does not match, undefined is returned.

route.path(props)

Return only the path part of the given props. If a path parameter is missing from props or exists but is invalid, undefined is returned.

route.query(props)

Return only the query part of the given props as an object. Always returns an object.

route.qs(props)

Return only the query part of the given props as a string. The string is always ordered by query key name. A ? is prepended if the query string is not empty.

Hostname

One more bump in the road occurs when dealing with hostnames. Accessing your API from another service on your backend requires requesting a host like localhost:3333 or something, whereas on the client it's something like https://api.app.com. Route doesn't really help you with this (apart from separating host from route), but here are some ways to deal with it.

One strategy is to use environment variables.

var userRoute = Route({
  host: process.env.API_HOST,
  path: '/users/<user>',
  params: {
    user: /\w+/
  }
});

then, host could be 'localhost:1234' on the server (using, for example, the command > API_HOST=localhost:1234 node serve.js), and 'https://api.app.com/api' on the client (using [envify] with [browserify] or something).

Because I'm scared of managing process.env and I already make heavy use of browserify, my preference is to separate server from client using the "browser" parameter in the package.json file. This parameter is used by browserify to select a different set of modules for use in the browser.

For example,

package.json (snippet):

{
  ...
  "browser": {
    "lib/host.js": "lib/browser-host.js"
  },
  ...
}

lib/host.js:

module.exports = 'localhost:3333';

lib/browser-host.js:

module.exports = 'https://api.app.com/api';

Now I can write my Route isomorphically,

var userRoute = Route({
  host: require('./host'),
  path: '/users/<user>',
  params: {
    user: /\w+/
  }
});

License

MIT