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

thing-router

v0.1.1

Published

A simple framework-neutral router

Downloads

5

Readme

ThingRouter

A small router package, written in typesript. The goal of this router is to simply call a function if a path such as /author/:author/article/:id matches.

The primary reason this was made, is to create a simple system for finding an 'entity' or 'model' based on its url.

This package does not aim to replace routers commonly found in server-side frameworks, but such a router could be built with this library.

This package uses the following two dependencies matching express-style url patterns:

Installation

npm i thing-router

Usage

Setting up a route:

import ThingRouter from 'thing-router';

const thingRouter = new ThingRouter();

type ArticleParams = {
  id: string
}
type Article = {
  id: number,
  title: string
};

thingRouter.add(
  '/article/:id',
  (params: ArticleParams): Article => {

    return {
      id: parseInt(params.id, 10),
      title: 'Article id: '  + params.id,
    };
  }
);

The above example registers a new route /article/:id. This callback should find an article associated with this url.

It's recommended to specify the types of both the parameters you're expecting, and the type that's being returned for a matched path.

Matching the route

const article = thingRouter('/article/5');

If the path could not be matched in the router, or if the callback returned null, a NotFoundError will be thrown.

Structuring code

If you're setting up many routes, it might be beneficial to define the callback function in another file, simply pass in the imported function.

thingRouter.add('/article/:id', articleFinder);
thingRouter.add('/author/:id', authorFinder);
thingRouter.add('/category/:id', categoryFinder);

This will make long lists more readable. It's also possible to pass all the routes via the constructor. This has the same effect:

const thingRouter = new ThingRouter([
  ['/article/:id',  articleFinder],
  ['/author/:id',   authorFinder],
  ['/category/:id', categoryFinder],
]);

Ensuring that the you're getting the right kind of thing.

Suppose you expect to receive an 'author' from the router, but the user specified a url to an 'article', you can let the router check this.

To do this, you must specifiy a 'kind' argument when setting up the route. This is the optional 3rd argument to 'add()':

thingRouter.add(
  '/author/:name', //path
  authorFinder, // callback
  'author', // kind
);

Now we're trying to fetch an author, but the path leads to a category instead:

const author = thingRouter('/category/5', 'author');

The above will thow a WrongKindError.

If you are using json-schema a suggested value for the kind parameter could be the $id attribute.