8000 GitHub - fabrix-app/spool-router at v1.6.0
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fabrix-app/spool-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 hapi.js route objects.

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)
  prefix: '/api/v1'
}
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.

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

Finally, 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.

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

Packages

No packages published

Contributors 2

  •  
  •  
0