@superhero/router

Event bus router

Usage no npm install needed!

<script type="module">
  import superheroRouter from 'https://cdn.skypack.dev/@superhero/router';
</script>

README

Router

Licence: MIT


npm version

A router that offers an hierarchy of dispatchers to an event bus.

Install

npm install @superhero/router

...or just set the dependency in your package.json file:

{
  "dependencies":
  {
    "@superhero/router": "*"
  }
}

Example

const
EventBus  = require('events');
bus       = new EventBus(),
debugging = true,
routes    =
[
  {
    name        :  'authentication',
    dispatchers : ['api/authentication']
  },
  {
    name        :  'authorization',
    dispatchers : ['api/authorization']
  },
  {
    name        :  'unauthorize',
    dispatchers : ['api/unauthorize']
  },
  {
    dispatchers : ['api/authorize'],
    children    :
    [
      {
        name        :  'foo',
        dispatchers : ['api/foo']
      },
      {
        name        :  'bar',
        acl         : ['admin','editor'], // optional acl validation, will
                                          // validate to first object sent as
                                          // parameter through the member
                                          // variable "role"
        dispatchers : ['api/bar']
      }
    ]
  }
];

require('@superhero/router')(bus, routes, debugging);

A dispatcher can break the dispatcher loop by throwing an object with the attributes name and context, which will be triggered as an event, or throw "nothing" (eg: false, 0, undefined...).