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

simple-isomorphic-router

v1.1.0

Published

Simple isomorphic router that is easily extendable

Downloads

12

Readme

simple-isomorphic-router

Simple isomorphic javascript router that is easily estendable.

Installation

npm i simple-isomorphic-router

Usage

Match URL instances to routes. The only requirement for a route is that it has a match method that will return the parameters for a matching URL or null if there was no match. The only provided route with this library is PathToRegexpRoute that uses the path-to-regexp library to match against route path- and hostnames and construct them from parameters. To use PathToRegexpRoute run npm i path-to-regexp to install all dependencies.

import { Router, PathToRegexpRoute } from 'simple-isomorphic-router';

const authorUrl = new URL('https://example.com/authors/123');
const bookUrl = new URL('https://example.com/books/123');
const router = new Router();
const route = new PathToRegexpRoute(
  {
    pathname: '/authors/:id',
  },
  { URLSearchParams }
);
router.addRoute(route);
console.log(router.match(authorUrl)); // -> { id: '123' }
console.log(router.match(bookUrl)); // -> null

PathToRegexpRoute also allows you to match on the hostname too.

import { Router, PathToRegexpRoute } from 'simple-isomorphic-router';

const url = new URL('https://jkrowling.example.com/authors/123');
const router = new Router();
const route = new PathToRegexpRoute(
  {
    pathname: '/authors/:id',
    hostname: ':author.example.com',
  },
  { URLSearchParams }
);
router.addRoute(route);
console.log(router.match(url)); // -> { id: '123', author: 'jkrowling' }

Use PathToRegexpRoute to construct URL's.

import { PathToRegexpRoute } from 'simple-isomorphic-router';

const route = new PathToRegexpRoute(
  {
    pathname: '/books/:id',
    hostname: ':author.example.com',
  },
  { URLSearchParams }
);
console.log(router.getPathname({ id: 123 })); // -> '/books/123'
console.log(router.getHostname({ author: 'roalddahl' })); // -> 'roalddahl.example.com'
console.log(router.getHref({ author: 'roalddahl', id: 123 })); // -> '//roalddahl.example.com/books/123'

All parts of an url can be passed to the the PathToRegexpRoute constructor.

import { PathToRegexpRoute } from 'simple-isomorphic-router';

const route = new PathToRegexpRoute(
  {
    pathname: '/books/:id',
    hostname: ':author.example.com',
    hash: 'section3',
    port: 1234,
    protocol: 'https:',
    username: 'someuser',
    password: 'somesecret',
    searchParams: { page: 1 },
  },
  { URLSearchParams }
);
console.log(router.getHref({ author: 'roalddahl', id: 123 })); // -> 'https://roalddahl.example.com:1234/books/123?page=1#section3'
console.log(
  router.getHref(
    { author: 'roalddahl', id: 123 },
    { searchParams: { page: 2 }, replace: true }
  )
); // -> 'https://roalddahl.example.com:1234/books/123?page=2#section3'

To roll your own route logic you can extend the Route class that allows you to pass a list of matchers.

import { Route } from 'simple-isomorphic-router';

class MyRoute extends Route {
  constructor() {
    super(
      {
        pathname,
        hostname,
        hash,
        port,
        protocol,
        username,
        password,
        searchParams,
      },
      {
        matchers: [
          (url) => {
            // some magic here that will match against the incoming url and return a parameters object in case of a match or null when there was no match
          },
        ],
        URLSearchParams,
      }
    );
  }
}

API

Router

The Router constructor takes no arguments

const router = new Router();

addRoute(...routes)

Add one or more routes to match against. The only requirement for a route is that it has a match method that will get passed an URL instance and return the route parameters as an object or null when there is no match.

match(url: URL)

Can be passed an URL instance and returns all parameters found as an object or null if there was no match.

Route

The Route constructor takes two arguments, the first is an object of url parts, the second is an options object containing a list of matchers and the global URLSearchParams object.

const route = new Route(
  {
    pathname,
    hostname,
    hash,
    port,
    protocol,
    username,
    password,
    searchParams,
  },
  {
    matchers: [
      (url) => {
        // some magic here that will match against the incoming url and return a parameters object in case of a match or null when there was no match
      },
    ],
    URLSearchParams,
  }
);

Route methods take an optional params argument that is not used by default, but can be used by extending classes.

match(url: URL)

Matches the route against an URL and returns all parameters found as an object or null if there was no match

getPathname(params)

returns the route pathname

getHash(params)

returns the route hash

getHostname(params)

returns the route hostname

getPort(params)

returns the route port

getProtocol(params)

returns the route protocol

getUsername(params)

returns the route username

getPassword(params)

returns the route password

getSearchParams(searchParams = null, replace = false)

returns an URLSearchParams instance that will contain the route searchParams. Extended with the searchParams argument (searchParams get merged by default, pass true as second argument to replace any existing values)

getSearch(searchParams = null, replace = false)

returns the url parameter string based on the route searchParams extended with the searchParams argument, containing a ? following the parameters of the URL.

getHref(params, { searchParams })

returns a full url href string based on the route parts.

PathToRegexpRoute

The PathToRegexpRoute constructor takes two arguments just like the Route constructor, with the exception that matchers for the pathname and hostname are provided by default (any other matchers will be added) and that the options object also takes two extra properties; compileOptions and matchOptions which can be used to provide options to the compile and match methods of the path-to-regexp library.

const route = new PathToRegexpRoute(
  {
    pathname,
    hostname,
    hash,
    port,
    protocol,
    username,
    password,
    searchParams,
  },
  { URLSearchParams, compileOptions, matchOptions }
);