Summary
route-methods
creates a list of routes and their methods from a nested
structure. This makes it easier to create nested APIs.
route-methods
was originally created to work with autoroute-base libraries
like autoroute-express-promise
.
Installation
Install with npm
:
npm install --save route-methods
Project Status
- Beta
- Active (August 10, 2015)
Examples
enum method get post const normalRoute = route: '/yep/its/normal/:id' methods: methodget 'OK' methodpost 'Posted' // => [normalRoute] const simpleRoute = '/dogs': methodget 'many dogs' // =>// [{// route: '/dogs',// methods: [[method.get, 'many dogs']]// }] const complexRoute = '/cats': _methods: methodget 'all cats' mine: methodget 'my cat' methodpost 'a new cat' ':id': methodget 'a special cat' // =>// [// {route: '/cats', methods: [[method.get, 'all cats']]},// {route: '/cats/mine', methods: [[ method.get, 'my cat', method.post, 'a new cat' ]]},// {route: '/cats/:id', methods: [[ method.get, 'a special cat' ]]}// ] const newNestedRoute = '/hamsters': '/fish': methodpost 'hamsters bite' // =>// [{// route: '/fish',// methods: [[ method.post, 'hamsters bite' ]]}])// })
API
where RouteDefinition
is
interface RouteDefinition<T S> /** * Route name, e.g., route: "/api/myroute/:id" */ route: string /** * E.g., methods: [ * [method.get, req => myFunctionThatReturnsAPromiseGet(req.params["theIdName"])], * [method.delete, req => myFunctionThatReturnsAPromiseDelete(req.params["theIdName"])] * ] */ methods: T S
subRoutes <T, S>(routes: [string, any]): RouteDefinition<T, S>[]
where routes
Tuple, [route, child routes and/or route methods]
flattenAltRoute <T, S>(nestedRoutes: any): RouteDefinition<T, S>[]
where nestedRoutes
has the form seen the examples. Can be a mixture of the
different examples.
flattenRoute <T, S>(routeDefinition: any): RouteDefinition<T, S>[]
where routeDefinition
can be the same argument in flattenAltRoute
or the
normalRoute
seen the examples.