Skip to content

Commit

Permalink
Merge 462c064 into dd5f762
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewglover committed Oct 11, 2016
2 parents dd5f762 + 462c064 commit b08d658
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
25 changes: 20 additions & 5 deletions lib/add_routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const STATIC_ROUTE_HANDLER = {
method: 'GET',
const { pickAll } = require('ramda');

const DEFAULT_OPTIONS = {
setStaticRoutes: true,
path: '/{param*}',
handler: {
directory: {
Expand All @@ -9,10 +11,23 @@ const STATIC_ROUTE_HANDLER = {
},
};

// addRoutes :: [Hapi.Route] -> Hapi.Server -> Hapi.Server
const addRoutes = (routes = []) => server => {

const getRouteConfig =
pickAll(['path', 'handler']);

const createStaticRouteHandler = (options) =>
(options.setStaticRoutes
? [Object.assign({ method: 'GET' }, getRouteConfig(options))]
: []);

// addRoutes :: ([Hapi.Route], Object) -> Hapi.Server -> Hapi.Server
const addRoutes = (routes = [], userOptions = {}) => server => {
if (!Array.isArray(routes)) throw new TypeError('Invalid route options');
server.route([STATIC_ROUTE_HANDLER, ...routes]);

const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);

server.route(createStaticRouteHandler(options).concat(routes));

return server;
};

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@matthewglover/hapi-wrapper",
"version": "0.2.3",
"version": "0.2.4",
"description": "Promise-based wrapper around Hapi",
"main": "index.js",
"scripts": {
"precommit": "npm run lint && nyc --reporter=lcov npm test && npm run coverage",
"coverage": "nyc check-coverage --lines 100, --functions 100, --branches 100",
"lint": "eslint './+(lib|test)/**/*.js'",
"test": "NODE_ENV=test ava --verbose"
"precommit": "npm run lint && nyc --reporter=lcov npm test && npm run coverage",
"test": "NODE_ENV=test ava --verbose",
"test-coverage-report": "nyc npm run test report --reporter=lcov"
},
"repository": {
"type": "git",
Expand All @@ -21,7 +22,8 @@
"homepage": "https://github.com/matthewglover/hapi-wrapper#readme",
"dependencies": {
"hapi": "^15.0.2",
"inert": "^4.0.2"
"inert": "^4.0.2",
"ramda": "^0.22.1"
},
"devDependencies": {
"ava": "^0.16.0",
Expand Down
28 changes: 28 additions & 0 deletions test/create_server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,31 @@ test('createServer and addRoutes sets up Inert to serve files from ./public fold

t.regex(reply.result, /Hello Hapi World!/);
});

// eslint-disable-next-line max-len
test('createServer and addRoutes (with custom path of /public/{param*}) sets up Inert to serve files from ./public folder at /public', async t => {
const server =
await createServer()
.then(setConnection())
.then(addRoutes(undefined, { path: '/public/{param*}' }))
.then(provisionInjectPromise);

const reply =
await server.injectPromise({ method: 'GET', url: '/public/test.txt' });

t.regex(reply.result, /Hello Hapi World!/);
});

test(`createServer and addRoutes can be setup to prevent Inert from serving
any static files with options { setStaticRoutes: false }`, async t => {
const server =
await createServer()
.then(setConnection())
.then(addRoutes(undefined, { setStaticRoutes: false }))
.then(provisionInjectPromise);

const reply =
await server.injectPromise({ method: 'GET', url: '/test.txt' });

t.is(reply.result.statusCode, 404);
});

0 comments on commit b08d658

Please sign in to comment.