Skip to content
This repository has been archived by the owner on Jan 31, 2025. It is now read-only.

To make the api more dynamic let's introduce defaulthandler #71

Merged
merged 4 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@
"maxparams": 5,
"maxdepth": 5,
"maxstatements": 50,
"maxcomplexity": 15
"maxcomplexity": 16
}
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Options:

- `api` - a valid Swagger 2.0 object.
- `handlers` - either a directory structure for route handlers or a premade object (see *Handlers Object* below).
- `defaulthandler` - a handler function appropriate to the target framework, if used this will be the default handler for all generated routes (see *Default handler* below).
- `basedir` - base directory to search for `handlers` path (defaults to `dirname` of caller).
- `schemas` - an array of `{name: string, schema: string|object}` representing additional schemas to add to validation.
- `security` - directory to scan for authorize handlers corresponding to `securityDefinitions`.
Expand Down Expand Up @@ -110,7 +111,7 @@ Or at the operation level:

These paths are relative to the `options.basedir` and are used as fallbacks for missing handlers from directory scan.

If the `options.handlers` is empty, then they will be used exclusively.
If the `options.handlers` and `options.defaulthandler` is empty, then they will be used exclusively.

### Handlers File

Expand Down Expand Up @@ -162,6 +163,20 @@ Example:

Handler keys in files do *not* have to be namespaced in this way.

### Default handler

The `options.defaulthandler` will set the handler function for all generated routes to one default handler.

``` javascript
var routes = builder({
api: require('./api.json'),
defaulthandler: function (req, reply) {
reply('something');
}
});
```


### Route Object

The `routes` array returned from the call to the builder will contain `route` objects. Each `route` has the following properties:
Expand Down
9 changes: 6 additions & 3 deletions lib/buildroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ var utils = require('./utils'),
* @returns {Array}
*/
function buildroutes(options) {
var api, routes, handlers, validator;
var api, routes, handlers, defaulthandler, validator;

api = options.api;
handlers = readhandlers(options.handlers);
defaulthandler = options.defaulthandler;
validator = validation(options);
routes = [];

Expand All @@ -39,7 +40,7 @@ function buildroutes(options) {
method: verb,
security: buildSecurity(options, api.securityDefinitions, operation.security || def.security),
validators: [],
handler : undefined,
handler : defaulthandler || undefined,
consumes: operation.consumes || api.consumes,
produces: operation.produces || api.produces,
json: operation.json || api.json,
Expand Down Expand Up @@ -73,7 +74,9 @@ function buildroutes(options) {
}
});

route.handler = handlers && (pathnames[0] ? matchpath('$' + verb, pathnames, handlers[pathnames[0]]) : handlers['$' + verb]);
if (!route.handler) {
route.handler = handlers && (pathnames[0] ? matchpath('$' + verb, pathnames, handlers[pathnames[0]]) : handlers['$' + verb]);
}

if (!route.handler) {
route.handler = operation['x-handler'] || def['x-handler'];
Expand Down
25 changes: 25 additions & 0 deletions test/test-routebuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ test('routebuilder', function (t) {
t.end();
});

t.test('build with defaulthandler', function (t) {
routes = buildroutes({
api: api,
basedir: path.join(__dirname, 'fixtures'),
defaulthandler: function () {},
schemaValidator: schemaValidator
});

t.strictEqual(routes.length, 6, 'added 6 routes.');

routes.forEach(function (route) {
t.ok(route.hasOwnProperty('method'), 'has method property.');
t.ok(route.hasOwnProperty('description'), 'has validate property.');
t.ok(route.hasOwnProperty('name'), 'has name property.');
t.ok(route.hasOwnProperty('path'), 'has path property.');
t.ok(route.hasOwnProperty('security'), 'has security property.');
t.ok(route.hasOwnProperty('validators'), 'has validators property.');
t.ok(route.hasOwnProperty('handler'), 'has handler property.');
t.ok(route.hasOwnProperty('produces'), 'has produces property.');
t.ok(route.hasOwnProperty('consumes'), 'has consumes property.');
});

t.end();
});

t.test('route validators', function (t) {
var route;

Expand Down