Skip to content

Commit 2cb35ab

Browse files
committed
Routes can now be easily hidden from docs generator
1 parent c2ff434 commit 2cb35ab

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ to write additional text as the configuration itself serves as a living document
625625
- _'raw'_ - the payload is read and stored in _'request.rawBody'_ but not parsed.
626626
- _'parse'_ - the payload is read and stored in _'request.rawBody'_ and then parsed (JSON or form-encoded) and stored in _'request.payload'_.
627627
- `cache` - if the server `cache` option is enabled and the route method is 'GET', the route can be configured to use the cache as described in [Caching](#caching).
628+
- `docs` - if set to false then the route will be hidden from the documentation generator.
628629
- `pre` - an array with pre-handler methods as described in [Route Prerequisites](#route-prerequisites).
629630
- `auth` - authentication configuration
630631
- `mode` - the authentication mode. Defaults to _'required'_ is the `authentication` server option is set, otherwise _'none'_. Available options include:

lib/docs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ exports.handler = function (route, config) {
3232

3333
routes.push(request.server._match('get', path));
3434
routes.push(request.server._match('post', path));
35+
routes.push(request.server._match('delete', path));
36+
routes.push(request.server._match('put', path));
3537

3638
routes = routes.filter(function (item) {
3739

38-
return !!item;
40+
return item && item.config.docs !== false;
3941
});
4042

4143
if (!routes.length) {
@@ -48,7 +50,7 @@ exports.handler = function (route, config) {
4850
routes = request.server._routeTable();
4951
routes = routes.filter(function (item) {
5052

51-
return item && item.path !== route.path;
53+
return item && item.path !== route.path && item.config.docs !== false && item.method !== 'options';
5254
});
5355

5456
routes.sort(function (route1, route2) {

test/integration/docs.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,27 @@ describe('Docs Generator', function () {
2929

3030
function setupServer(done) {
3131

32-
_server = new Hapi.Server('0.0.0.0', 0);
32+
_server = new Hapi.Server();
3333
_server.addRoutes([
3434
{ method: 'GET', path: '/docs', handler: { docs: { routeTemplate: _routeTemplate, indexTemplate: _indexTemplate } } },
3535
{ method: 'GET', path: '/defaults', handler: { docs: true } },
3636
{ method: 'GET', path: '/test', config: { handler: handler, query: { param1: S().required() } } },
37-
{ method: 'POST', path: '/test', config: { handler: handler, query: { param2: S().valid('first', 'last') } } }
37+
{ method: 'POST', path: '/test', config: { handler: handler, query: { param2: S().valid('first', 'last') } } },
38+
{ method: 'GET', path: '/notincluded', config: { handler: handler, docs: false } }
3839
]);
3940

40-
_server.start(done);
41+
done();
4142
}
4243

4344
function setupServerWithoutPost(done) {
4445

45-
_serverWithoutPost = new Hapi.Server('0.0.0.0', 0);
46+
_serverWithoutPost = new Hapi.Server();
4647
_serverWithoutPost.addRoutes([
4748
{ method: 'GET', path: '/docs', handler: { docs: { routeTemplate: _routeTemplate, indexTemplate: _indexTemplate } } },
4849
{ method: 'GET', path: '/test', config: { handler: handler, query: { param1: S().required() } } }
4950
]);
5051

51-
_serverWithoutPost.start(done);
52+
done();
5253
}
5354

5455
function makeRequest(path, callback) {
@@ -88,7 +89,7 @@ describe('Docs Generator', function () {
8889

8990
makeRequest('/docs', function (res) {
9091

91-
expect(res).to.equal('/defaults|/defaults|/test|/test|/test|/test|');
92+
expect(res).to.equal('/defaults|/test|/test|');
9293
done();
9394
});
9495
});
@@ -102,6 +103,15 @@ describe('Docs Generator', function () {
102103
});
103104
});
104105

106+
it('the index does\'t include routes that are configured with docs disabled', function (done) {
107+
108+
makeRequest('/docs', function (res) {
109+
110+
expect(res).to.not.contain('/notincluded');
111+
done();
112+
});
113+
});
114+
105115
it('shows template when correct path is provided using defaults', function (done) {
106116

107117
makeRequest('/defaults?path=/test', function (res) {

0 commit comments

Comments
 (0)