Skip to content

fabrix-app/spool-router

Repository files navigation

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