Skip to content

Commit

Permalink
Merge pull request #29 from Y-LyN-10/master
Browse files Browse the repository at this point in the history
Hapi v17 Compatible
  • Loading branch information
danielb2 committed Dec 12, 2017
2 parents 91db5cc + 0a37298 commit da0f587
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 268 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,9 +1,9 @@
language: node_js

node_js:
- 4.0
- 4
- 5
- "8"
- "9"
- "node"

sudo: false

39 changes: 21 additions & 18 deletions README.md
Expand Up @@ -7,32 +7,34 @@ correctly. This can be difficult to see otherwise.

![image](images/screenshot.png)

## Usage
## Usage (Hapi v17)

``` javascript
var Blipp = require('blipp');
var Hapi = require('hapi');
'use strict';

var server = new Hapi.Server();
server.connection();
const Hapi = require('hapi');
const Blipp = require('blipp');

server.route({
const server = new Hapi.Server();

const init = async () => {

await server.register(Blipp);

server.route({
method: 'GET',
path: '/somepath',
config: {
auth: 'simple',
description: 'Description to display',
handler: function (request, reply) {
// ..
}
options: {
auth: false,
description: 'Description to display',
handler: (request, h) => 'Something'
}
});
});

await server.start();
}

server.register({ register: Blipp, options: {} }, function (err) {
server.start(function () {
// ..
});
});
init();
```

## Options
Expand All @@ -59,3 +61,4 @@ With showAuth:

* 1.x = hapi 7.x
* 2.x = hapi 8.x
* 3.x = hapi 17.x
112 changes: 47 additions & 65 deletions lib/index.js
@@ -1,58 +1,50 @@
// Load modules
var Chalk = require('chalk');
var Hoek = require('hoek');
var Joi = require('joi');
var Pkg = require('../package.json');
'use strict';

// Load modules
const Chalk = require('chalk');
const Hoek = require('hoek');
const Joi = require('joi');
const Pkg = require('../package.json');

// Declare internals
var internals = {
const internals = {
schema: {
showAuth: Joi.boolean().default(false),
showStart: Joi.boolean().default(true)
}
};

exports.register = function (server, options) {

exports.register = function (server, options, next) {

var result = Joi.validate(options, internals.schema);
const result = Joi.validate(options, internals.schema);
Hoek.assert(!result.error, result.error && result.error.annotate());
options = result.value;

server.expose('text', function () {

var info = this.info();
const info = this.info();
return internals.printableInfo(info, options);
});

server.expose('info', function () {
server.expose('info', () => {

return internals.getRouteInfo(server, options);
});

if (options.showStart) {
server.on('start', function () {
server.events.on('start', () => {

var out = server.plugins[Pkg.name].text();
const out = server.plugins[Pkg.name].text();
console.log(out);
});
}

return next();
};


exports.register.attributes = {
pkg: Pkg
};


internals.printableInfo = function (connections, options) {

var out = '';
for (var i = 0, il = connections.length; i < il; ++i) {
var connection = connections[i];
let out = '';
for (let i = 0; i < connections.length; ++i) {
const connection = connections[i];

out += internals.printableConnection(connection, options);
}
Expand All @@ -62,26 +54,27 @@ internals.printableInfo = function (connections, options) {

internals.printableConnection = function (connection, options) {

var out = internals.printableTitle(connection, options) + '\n';
let out = internals.printableTitle(connection, options) + '\n';
out += internals.printableRoutes(connection.routes, options);
return out;
};


internals.printableRoutes = function (routes, options) {

var out = '';
for (var i = 0, il = routes.length; i < il; ++i) {
var show = routes[i];
let out = '';
for (let i = 0; i < routes.length; ++i) {
const show = routes[i];

var method = internals.formatMethod(show.method);
var description = internals.formatDescription(show.description);
var auth = internals.formatAuth(show.auth);
var path = internals.formatPath(show.path);
const method = internals.formatMethod(show.method);
const description = internals.formatDescription(show.description);
const auth = internals.formatAuth(show.auth);
const path = internals.formatPath(show.path);

if (options.showAuth) {
out += [method, path, auth, description].join(' ') + '\n';
} else {
}
else {
out += [method, path, description].join(' ') + '\n';
}
}
Expand All @@ -92,49 +85,38 @@ internals.printableRoutes = function (routes, options) {

internals.printableTitle = function (connection) {

var title = Chalk.underline(connection.uri);
if (connection.labels.length) {
var labels = '[' + Chalk.magenta(connection.labels.join(', ')) + ']';
title += ' ' + labels;
}
const title = Chalk.underline(connection.uri);
return Chalk.cyan(title);
};


internals.getRouteInfo = function (server, options) {

var connections = [];
const routingTable = server.table();

var routingTable = server.table();
const connectionInfo = {
uri: server.info.uri,
routes: []
};

routingTable.forEach(function (connection) {
internals.connectionInfo(server, routingTable, options, connectionInfo);

var connectionInfo = {
uri: connection.info.uri,
labels: connection.labels,
routes: []
};

internals.connectionInfo(connection.table, options, connectionInfo);
connections.push(connectionInfo);
});

return connections;
return [connectionInfo];
};

internals.connectionInfo = function (routes, options, connectionInfo) {
internals.connectionInfo = function (server, routes, options, connectionInfo) {

for (var i = 0, il = routes.length; i < il; ++i) {
var route = routes[i];
for (let i = 0; i < routes.length; ++i) {
const route = routes[i];

var defaultStrategy = Hoek.reach(route, 'connection.auth.settings.default.strategies');
var authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false;
const defaultStrategy = Hoek.reach(server, 'auth.settings.default.strategies');
let authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false;

if (route.settings.auth === undefined) {
authStrategy = defaultStrategy ? String(defaultStrategy) : false;
}

var show = {
const show = {
method: route.method.toUpperCase(),
path: route.path,
description: route.settings.description || ''
Expand All @@ -147,10 +129,7 @@ internals.connectionInfo = function (routes, options, connectionInfo) {
connectionInfo.routes.push(show);
}

connectionInfo.routes.sort(function (a, b) {

return a.path.localeCompare(b.path);
});
connectionInfo.routes.sort((a, b) => a.path.localeCompare(b.path));
};


Expand All @@ -164,8 +143,7 @@ internals.formatPath = function (path) {

internals.ljust = function (string, amount) {

var padding = ' ';
var currentLength = string.length;
const padding = ' ';

while (string.length < amount) {
string = string + padding;
Expand All @@ -187,7 +165,8 @@ internals.formatAuth = function (auth) {

if (auth === false) {
auth = Chalk.red('none');
} else {
}
else {
auth = Chalk.green(auth);
}
auth = internals.ljust(auth, 20);
Expand All @@ -198,5 +177,8 @@ internals.formatDescription = function (description) {

description = description || '';
description = Chalk.yellow(description);

return description;
};

exports.pkg = Pkg;
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -18,14 +18,14 @@
"author": "Daniel Bretoi",
"license": "BSD",
"dependencies": {
"chalk": "0.5.x",
"hoek": "3.x.x",
"joi": "7.x.x"
"chalk": "^2.3.0",
"hoek": "^5.0.2",
"joi": "^13.0.1"
},
"devDependencies": {
"code": "1.x.x",
"hapi": "8.x.x",
"hapi-auth-basic": "2.x.x",
"lab": "5.x.x"
"code": "^5.1.2",
"hapi": "^17.0.1",
"hapi-auth-basic": "^5.0.0",
"lab": "^15.1.2"
}
}

0 comments on commit da0f587

Please sign in to comment.