Skip to content

Commit

Permalink
feat: add support for es6 module exports
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhobbel committed Dec 3, 2018
1 parent 306d918 commit 844a7ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ exports.register = (server, options) => {

debug(`[endpoint][${version}] Scanning endpoint file %s`, endpoint);

const route = require(endpoint);
const routeFile = require(endpoint);
const route = routeFile.default || routeFile;
const routes = Array.isArray(route) ? route : [route];

const prefixPath = prefixRegex.exec(endpoint)[1];
Expand Down
10 changes: 10 additions & 0 deletions test/endpoints/v1/es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// simulate export default { }
module.exports.default = {
method: 'GET',
path: '/es6',
handler: () => ({
message: 'Hi, this is an es6 route'
})
};
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ describe('Versioning', () => {
expect(response.result.message).to.equal('This is route 1');
});

it('should load routes out of an es6 module', async () => {

const response = await server.inject({
method: 'GET',
url: '/v1/es6'
});

expect(response.statusCode).to.equal(200);
expect(response.result.message).to.equal('Hi, this is an es6 route');
});

});

describe(' -> header', () => {
Expand Down

0 comments on commit 844a7ad

Please sign in to comment.