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

route-config

v0.10.4

Published

A static route configuration

Downloads

590

Readme

🗺️ RouteConfig

Static route configuration helper

Installation

NPM:

$ npm install --save route-config

Yarn:

yarn add route-config

Import

In ES6: import { Config, RouteConfig } from 'route-config'

Use

Basic

import { RouteConfig } from 'route-config'
const routeConfig = new RouteConfig({
  namespaces: {
    posts: {
      path: '/posts',
      routes: {
        index: {},
        show: {
          path: '/:postId'
        }
      }
    }
  },
  routes: {
    home: { path: '/home' }
  }
})

routeConfig.url('home')
//=> "/home"

routeConfig.url('posts.show')
//=> "/posts/:postId"

API

new RouteConfig(map, options)

Params:

  • [map={ namespaces: {}, routes: {} }] (Object): Map of your routes
    • [config] (Object): config to applied on sub namspaces and sub routes
    • [fullPath=''] (String): if set then it's used to prefix all sub namespace and sub route path.
    • [id=''] (String): id is used internally to create normalized map.
    • [key=''] (String): if set prefix all sub keys with key
    • [namespaces={}] (Object): sub namespaces
    • [path=''] (String): root path
    • [routes={}] (Object): sub routes
  • [options={}] (Object):
    • [configs] (Object): options passed to ConfigManager constructor

Ex

const routeConfig = new RouteConfig({
  namespaces: {
    posts: {
      path: '/posts',
      routes: {
        show: { path: '/:postId' }
      }
    }
  },
  routes: {
    home: { path: '/' }
  }
})

routeConfig.getMap(options)

Return map Params:

  • [options={}] (Object):
    • [filter] (Function): used to filter namespaces and routes returned in map
    • [formatRoute=identity] (Function): used to format route returned in map

Ex

routeConfig.getMap()
//=>
// {
//   namespaces: {
//     posts: {
//       config: {},
//       fullPath: '/posts',
//       id: 'posts',
//       key: 'posts',
//       path: '/posts',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/posts/:postId',
//           id: 'posts.show',
//           key: 'show',
//           path: '/:postId'
//         }
//       }
//     }
//   },
//   routes: {
//     home: {
//       config: {},
//       fullPath: '/',
//       id: 'home',
//       key: 'home',
//       path: '/'
//     }
//   }
// }

routeConfig.getNormalizedMap(options)

Return normalized map. For more info see normalizr

Params:

  • [options={}] (Object):
    • [filter] (Function): used to filter namespaces and routes returned in map
    • [formatRoute=identity] (Function): used to format route returned in map
routeConfig.getNormalisedMap()
//=>
// {
//   entities: {
//     namespaces: {
//       posts: {
//         config: {},
//         fullPath: '/posts',
//         id: 'posts',
//         key: 'posts',
//         path: '/posts',
//         routes: ['posts.show']
//       }
//     },
//     routes: {
//       home: {
//         config: {},
//         fullPath: '/',
//         id: 'home',
//         key: 'home',
//         path: '/'
//       },
//       'posts.show': {
//         config: {},
//         fullPath: '/posts/:postId',
//         id: 'posts.show',
//         key: 'show',
//         path: '/:postId'
//       }
//     }
//   },
//   result: {
//     namespaces: ['posts'],
//     routes: ['home']
//   }
// }

routeConfig.getRoute(key)

Return route object

Params:

  • key (String): a string or dot notation string to find route or nested route

Ex

routeConfig.getRoute('home')
//=>
// {
//   config: {},
//   fullPath: '/',
//   id: 'home',
//   key: 'home',
//   path: '/'
// }

routeConfig.getRoute('posts.show') // nested route
//=>
// {
//   config: {},
//   fullPath: '/posts/:postId',
//   id: 'posts.show',
//   key: 'show',
//   path: '/:postId'
// }

routeConfig.merge(...maps)

Merge route config map with maps (modify map)

Ex

routeConfig.merge({
  routes: {
    contact: { path: '/contact' }
  }
})

routeConfig.getMap()
//=>
// {
//   namespaces: {
//     posts: {
//       config: {},
//       fullPath: '/posts',
//       id: 'posts',
//       key: 'posts',
//       path: '/posts',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/posts/:postId',
//           id: 'posts.show',
//           key: 'show',
//           path: '/:postId'
//         }
//       }
//     }
//   },
//   routes: {
//     contact: {
//       config: {},
//       fullPath: '/contact',
//       id: 'contact',
//       key: 'contact',
//       path: '/contact'
//     },
//     home: {
//       config: {},
//       fullPath: '/',
//       id: 'home',
//       key: 'home',
//       path: '/'
//     }
//   }
// }

routeConfig.namespace(namespace)

Returns the new namespace that is a instance of RouteConfig.

Params:

  • namespace (Object):
    • [config] (Object): config to applied on sub namspaces and sub routes
    • [fullPath] (String): if set then it's used to prefix all sub namespace and sub route path. Else sub namespace and sub route path are prefixed with parent full path and namespace path
    • [id] (String): id is used internally to create normalized map
    • key (String): namespace name used to get route inside namespace
    • [namespaces={}] (Object): sub namespaces
    • path (String): namespace path
    • [routes={}] (Object): sub routes

Ex

const namespace = routeConfig.namespace({ key: 'authors', path: '/authors' })
namespace.route({ key: 'show', path: '/:authorId' })
namespace.getMap()
//=>
// {
//   config: {},
//   fullPath: '/authors',
//   id: 'authors',
//   key: 'authors',
//   namespaces: {},
//   path: '/authors',
//   routes: {
//     show: {
//       config: {},
//       fullPath: '/authors/:authorId',
//       id: 'authors.show',
//       key: 'show',
//       path: '/:authorId'
//     }
//   }
// }

routeConfig.getMap()
//=>
// {
//   namespaces: {
//     authors: {
//       config: {},
//       fullPath: '/authors',
//       id: 'authors',
//       key: 'authors',
//       namespaces: {},
//       path: '/authors',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/authors/:authorId',
//           id: 'authors.show',
//           key: 'show',
//           path: '/:authorId'
//         }
//       }
//     },
//     posts: {
//       config: {},
//       fullPath: '/posts',
//       id: 'posts',
//       key: 'posts',
//       path: '/posts',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/posts/:postId',
//           id: 'posts.show',
//           key: 'show',
//           path: '/:postId'
//         }
//       }
//     }
//   },
//   routes: {
//     contact: {
//       config: {},
//       fullPath: '/contact',
//       id: 'contact',
//       key: 'contact',
//       path: '/contact'
//     },
//     home: {
//       config: {},
//       fullPath: '/',
//       id: 'home',
//       key: 'home',
//       path: '/'
//     }
//   }
// }

routeConfig.route(route)

Returns routeConfig instance so you can chain call.

Params:

  • namespace (Object):
    • [config] (Object): route config
    • [fullPath] (String): route full path
    • [id] (String): id is used internally to create normalized map
    • key (String): route name
    • path (String): route path

Ex

routeConfig.route({ key: 'about', path: '/about' }).getMap()
//=>
// {
//   namespaces: {
//     authors: {
//       config: {},
//       fullPath: '/authors',
//       id: 'authors',
//       key: 'authors',
//       namespaces: {},
//       path: '/authors',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/authors/:authorId',
//           id: 'authors.show',
//           key: 'show',
//           path: '/:authorId'
//         }
//       }
//     },
//     posts: {
//       config: {},
//       fullPath: '/posts',
//       id: 'posts',
//       key: 'posts',
//       path: '/posts',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/posts/:postId',
//           id: 'posts.show',
//           key: 'show',
//           path: '/:postId'
//         }
//       }
//     }
//   },
//   routes: {
//     about: {
//       config: {},
//       fullPath: '/about',
//       id: 'about',
//       key: 'about',
//       path: '/about'
//     },
//     contact: {
//       config: {},
//       fullPath: '/contact',
//       id: 'contact',
//       key: 'contact',
//       path: '/contact'
//     },
//     home: {
//       config: {},
//       fullPath: '/',
//       id: 'home',
//       key: 'home',
//       path: '/'
//     }
// }

routeConfig.url(key, params, options)

Returns route url or null.

Params:

  • key (String): route key. You can use dot notation to get route url inside namspace
  • [params=undefined] (Object): params passed to path-to-regexp
  • [options=undefined] (Object): options passed to path-to-regexp

Ex

routeConfig.url('home')
//=> '/'

routeConfig.url('posts.show')
//=> /posts/:postId

routeConfig.url('posts.show', { postId: 1 })
//=> /posts/1

static RouteConfig.fromNormalizedMap(normalizedMap)

Returns new RouteConfig instance created from normalized map.

Params:

  • normalizedMap (Object): for the object format see getNormalizedMap
  • [options] (Object): options passed to RouteConfig constructor

Ex

const routeConfig = RouteConfig.fromNormalizedMap({
  entities: {
    namespaces: {
      posts: {
        config: {},
        fullPath: '/posts',
        id: 'posts',
        key: 'posts',
        path: '/posts',
        routes: ['posts.show']
      }
    },
    routes: {
      home: {
        config: {},
        fullPath: '/',
        id: 'home',
        key: 'home',
        path: '/'
      },
      'posts.show': {
        config: {},
        fullPath: '/posts/:postId',
        id: 'posts.show',
        key: 'show',
        path: '/:postId'
      }
    }
  },
  result: {
    namespaces: ['posts'],
    routes: ['home']
  }
})

routeConfig.getMap()
//=>
// {
//   namespaces: {
//     posts: {
//       config: {},
//       fullPath: '/posts',
//       id: 'posts',
//       key: 'posts',
//       path: '/posts',
//       routes: {
//         show: {
//           config: {},
//           fullPath: '/posts/:postId',
//           id: 'posts.show',
//           key: 'show',
//           path: '/:postId'
//         }
//       }
//     }
//   },
//   routes: {
//     home: {
//       config: {},
//       fullPath: '/',
//       id: 'home',
//       key: 'home',
//       path: '/'
//     }
//   }
// }