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

broutes

v2.0.0

Published

Named routing for javascript applications

Downloads

51

Readme

broutes

A simple router DSL for defining named routes in frontend javascript applications. Uses path-to-regexp v1.7.0 internally and is fully compatible with react-router.

Installation

npm:

npm i broutes

yarn:

yarn add broutes

Example Usage

import { composeRoutes } from "broutes";

let routes = composeRoutes(r => {
  r.route('/tests');
  r.route('/foo/:slug', { name: 'foo', defaultParams: { slug: 'bar' } });
  r.resources('/orders');

  r.namedScope('/api', r => {
    r.scope('/:version', r => {
      r.route('/users');
    }, { defaultParams: { version: 'v1' }});
  });
});

routes.testsPath(); //=> '/tests'
routes.fooPath(); //=> '/foo/bar'
routes.fooPath({ slug: 'baz' }); //=> '/foo/baz'

routes.orders.indexPath(); //=> '/orders'
routes.orders.showPath({ id: 123 }); //=> '/orders/123'
routes.orders.newPath(); //=> '/orders/new'
routes.orders.showPath.raw; //=> '/orders/:id'

routes.api.usersPath(); //=> '/api/v1/users'
routes.api.usersPath({ version: 'v2' }); //=> '/api/v2/users'
routes.api.usersPath({}, { query: { foo: 'bar' } }); //=> '/api/v1/users?foo=bar'

API

composeRoutes()

composeRoutes(routesBuilder[, options])

Builds and returns new routes object containing named path builders for defined routes.

  • routesBuilder: a builder function to which an object containing {route, scope, namedScope, resources} will be passed.
  • options:
    • toQueryString: a function used to build query strings for all routes. A simple non-strict encodeURIComponent-based function is used by default.

r.route()

r.route(path[, options])

Adds new named route to routes object, which then can be built by calling {routeName}Path() function. You can also access route's raw path(with all named placeholders in place) via {routeName}Path.raw.

r.route() will throw if route with the same name was already defined.

  • path: route's path.
  • options:
    • name: explicitly defines name of this route. By default, the route name will be inferred from the path and set to camelCase, so that r.route("/path-to-thing") or r.route("/path_to_thing") are both named pathToThing.
    • defaultParams: object containing default named parameters for this route's path. See below for example usage.

Example

let routes = composeRoutes(r => {
  r.route("/my-orders");
  r.route("/users", {name: "clients"});
  r.route("/users/:slug", {name: "client", defaultParams: {slug: "john"}});
});

routes.myOrdersPath(); //=> "/my-orders"

routes.clientsPath(); //=> "/users"

routes.clientPath(); //=> "/users/john"
routes.clientPath({slug: "joe"}); //=> "/users/joe"
routes.clientPath.raw; //=> "/users/:slug"

r.scope()

r.scope(path, scopeBuilder[, options])

Defines new scope for all contained routes. Scopes can also be nested within each other.

  • path: scope's path. This path will be added to each route within the scope.
  • scopeBuilder: builder function which has the same API as the composeRoutes' builder function.
  • options:
    • defaultParams: object containing default named parameters for paths in this scope.

r.namedScope()

r.namedScope(path, scopeBuilder[, options])

Defines new named scope for routes, i.e adds a namespace within routes object(or within another named scope).

  • path: scope's path. This path will be added to each route within the scope.
  • scopeBuilder: builder function which has the same API as the composeRoutes' builder function.
  • options:
    • name: explicitly defines name of this named scope. By default, the scope name will be inferred from the path and set to camelCase.
    • defaultParams: object containing default named parameters for paths in this scope.

r.resources()

r.resources(path, scopeBuilder[, options])

A simple shortcut to define commonly used routes. A call to r.resources('/users') is exactly equalent to:

r.namedScope('/users', (r) => {
  r.route('/', { name: 'index' });
  r.route('/:id', { name: 'show' });
  r.route('/new');
});
  • path: path for the resources
  • scopeBuilder: builder function which has the same API as the composeRoutes' builder function.
  • options:
    • name: explicitly defines name of this resource and the namedScope it's contained in.
    • defaultParams: object containing default named parameters for paths in this scope.

License

MIT