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

@fabrix/spool-router

v1.6.4

Published

Spool - Router for Fabrix

Downloads

213

Readme

spool-router

Gitter NPM version Build Status Test Coverage Dependency Status Follow @FabrixApp on Twitter

Spool Router. Aggregates all routes from config.routes to create a Fabrix Route which is easily translated to hapi.js route objects, Express.js routes, or your own!

Install

$ npm install @fabrix/spool-router --save

Usage

Load from your spool config. (This pack is included by default).

// config/main.ts
import { RouterSpool } from '@fabrix/spool-router'
export const main = {
  // ...
  spools: [
    RouterSpool
  ]
}

Configure

config.router

The Router takes a few Configuration values

// config/router.ts
export const router = {
  sortOrder: 'asc', // (asc | desc)
  default: '', // the default or home route
  catchAllRoute: '*', // the catch all handler route
  prefix: '/api/v1',
  debug: false // if the router needs to log all debugs
}
router.sortOrder

This will sort the routes based on the key (path) either ascending or descending. This is used in spools like Express where the order of routes matters.

router.prefix

This config is optional and can be left as '' or null. This will prefix each route with the specified prefix.

router.default

This config is optional and can be left as '' or null. This is '/' in express, but '' in Hapi

router.catchAllRoute

This config is optional and can be left as '' or null. This is '*' in express and Hapi

config.routes

The list of route objects to be compiled for use by the webserver.

// config/routes.ts
const routes = {
  '/example/test': {
    'GET': 'ExampleController.test'
  }
}

During initialization, for the above example, a route object will be compiled that takes the following form:

{
  // ...
  '/example/test': {
    'GET': {
      handler: 'ExampleController.test',
      config: {
        pre: [ ]
      }
    }
  }
  // ...
}

You can also refine this by explicitly defining the handler and config:

{
  // ...
  '/example/test': {
    'GET': {
      handler: 'ExampleController.get',
      config: {
        pre: [ 'ExamplePolicy.get' ]
      }
    },
    'POST': {
      handler: 'ExampleController.post',
      config: {
        pre: [ 'ExamplePolicy.post' ]
      }
    }
  }
  // ...
}

Which is useful for refining controller over different http methods on a route.

Prefixes
{
  // ...
  '/example/test': {
    'GET': 'ExampleController.test',
    config: {
      prefix: '/api/v2'
    }
  }
  // ...
}

The Configuration above, will give this route a prefix of /api/v2 instead of using the config.prefix that was specified

Optionally:

{
  // ...
  '/example/test': {
    'GET': 'ExampleController.test',
    config: {
      prefix: false
    }
  }
  // ...
}

The Configuration above, will ignore any prefix given to it.

Optionally:

{
  // ...
  '/example/test': {
    'GET': 'ExampleController.test',
    config: {
      prefix: 'customPrefixer.prefix'
    }
  }
  // ...
}

The configuration above will take the configuration of another config attribute, in this case: app.config.customPrefixer.prefix is set to /custom/endpoint so the resulting route prefix will be /custom/endpoint/example/test

Additionally, you can also provide 2 different prefixes for the same route with different methods.

{
  // ...
  '/example/test': {
    'GET': {
      handler: 'ExampleController.get',
      config: {
        prefix: '/api/v1'
        pre: [ 'ExamplePolicy.get' ]
      }
    },
    'POST': {
      handler: 'ExampleController.post',
      config: {
        prefix: '/api/v2'
        pre: [ 'ExamplePolicy.post' ]
      }
    }
  }
  // ...
}

The configuration above will produce 2 routes, one for GET /api/v1/example/test and one for POST /api/v2/example/test respecting their prefixes. This is useful for when one method may still be on an older API than the other or they need to be handled differently.

Finally, you can version your route with prefixes under development

{
  // ...
  '/example/test': {
    'GET': {
       versions: { 
           'ExampleController.get': {
              config: {
                 prefix: 'prefix.one',
                 pre: [ 'ExamplePolicy.get' ]
              }
          },
          'ExampleController.getTwo': {
                config: {
                 prefix: 'prefix.two',
                 pre: [ 'ExamplePolicy.get' ]
              }
           }
        }
    },
    'POST': {
      handler: 'ExampleController.post',
      config: {
        prefix: '/api/v2',
        pre: [ 'ExamplePolicy.post' ]
      }
    }
  }
  // ...
}

Tapestries and Policies

Support for tapestries and Policies is provided by spool-tapestries.

Compatible Spools

Contributing

We love contributions! Please see our Contribution Guide for more information.

License

MIT