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

tagged-routes

v0.0.3

Published

Routing with tagged unions

Downloads

12

Readme

Tagged Routes

Routing using tagged unions

npm install tagged-routes

npm Build Status Greenkeeper badge

Example

import { createRoutes } from 'tagged-routes'

const { Route, getRouteForURL, getURLForRoute } = routeTable(
    {
        AppList: '/',
        AppMain: '/apps/:appId',
        AppSettings: '/apps/:appId/settings',
        Settings: '/settings/(.*)'
    },
    'NotFound'
)

console.log(getRouteForUrl('/apps/example?tab=hosting'))
/* => <AppMain ({
    routeParams: { appId: 'example' },
    queryParams: { tab: 'hosting' }
})> */

console.log(getRouteForUrl('/settings/can-be-anything'))
/* => <Settings ({
    routeParams: {},
    queryParams: {},
    routeSplat: 'can-be-anything'
})> */

console.log(getRouteForUrl('/bad/route-bad'))
/* => <NotFound ({
    routeParams: {},
    queryParams: {},
    routePath: '/bad/route-bad'
})> */

const exemplarRoute = Route.AppMain({
    routeParams: { appId: 'exemplar' },
    queryParams: { tab: 'billing' }
})

console.log(getURLForRoute(exemplarRoute))
// => /apps/exemplar?tab=billing

Documentation

createRoutes(routeTable, catchAllRouteKind: string, options?: object)

Accepts a routeTable object with route kinds (types of Route) as keys and valid path-to-regexp paths as values along with a catch-all route kind catchAllRouteKind. The options are passed to path-to-regexp and options.encode is passed to path-to-regexp.compile URL builders to override the encodeURIComponent default.

Returns an object containing the below Route union and functions.

Route

A tagmeme union, has a .match method and constructors for each route kind.

getRouteForURL(url: string): Route

Returns the route that matches the url, or the catch-all route if none match. The shape of route data is:

interface RouteData {
    routeParams: {[key: string]: string}
    queryParams: {[key: string]: string}
    routeSplat?: string // only if route has a splat "(.*)" segment
    routePath?: string  // only if catch-all route
}

getURLForRoute(route: Route): string

Returns the URL that matches the route.

Throws in development if:

  • a route requiring certain routeParams values is missing values.
  • the catch-all route does not have the routePath specified.