@flexshopper/hapi-listeners
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -5,9 +5,10 @@ 'use strict'; | ||
const Joi = require('joi'); | ||
const Async = require('async'); | ||
const internals = module.exports = {}; | ||
const defaults = { | ||
const globSchema = { | ||
options: Joi.object().keys({ | ||
cwd: Joi.string().default(process.cwd()), | ||
dirname: Joi.string().default('/test/artifacts'), | ||
dirname: Joi.string().default(''), | ||
}), | ||
@@ -20,38 +21,44 @@ info: ['plugin', 'info', require('../package.json').name], | ||
Joi.validate(options, defaults.options, (err, value) => { | ||
Async.auto({ | ||
options: (callback) => { | ||
Joi.validate(options, globSchema.options, callback); | ||
}, | ||
listeners: ['options', (data, callback) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
if (!data.options.dirname) { | ||
return callback(null, null); | ||
} | ||
const opts = Hoek.applyToDefaults(value, options); | ||
const opts = Hoek.applyToDefaults(data.options, options); | ||
var normalizedPath = require("path").join(opts.cwd + opts.dirname); | ||
let normalizedPath = require("path").join(opts.cwd + opts.dirname); | ||
let listeners = require("fs").readdirSync(normalizedPath); | ||
let listeners = require("fs").readdirSync(normalizedPath); | ||
for (const file of listeners) { | ||
for (let file of listeners) { | ||
const filepath = opts.cwd + opts.dirname + '/' + file; | ||
const listener = require(filepath); | ||
const filepath = opts.cwd + opts.dirname + '/' + file; | ||
const listener = require(filepath); | ||
if (typeof (listener.method) !== 'function') { | ||
return next(new Error('method must be a function')); | ||
} | ||
if (typeof (listener.method) !== 'function') { | ||
return next(new Error('method must be a function')); | ||
} | ||
const options = Hoek.reach(listener, 'options', { default: {} }); | ||
const options = Hoek.reach(listener, 'options', { default: {} }); | ||
server.method(listener.name, listener.method, options); | ||
} | ||
server.method(listener.name, listener.method, options); | ||
} | ||
server.on('tail', (request) => { | ||
const event = request.krakenEvent; | ||
server.methods[event.name](event.data); | ||
}); | ||
server.log(defaults.info, 'listeners loaded.'); | ||
return next(); | ||
}); | ||
return callback(null, server.methods); | ||
}], | ||
register: ['listeners', (data, callback) => { | ||
server.on('tail', (request) => { | ||
const event = request.krakenEvent; | ||
if (typeof (event) === 'object') { | ||
server.methods[event.name](event.data); | ||
} | ||
}); | ||
return callback(null, null); | ||
}], | ||
}, next); | ||
}; | ||
@@ -58,0 +65,0 @@ |
{ | ||
"name": "@flexshopper/hapi-listeners", | ||
"description": "Standard NPM package with Flexshopper coding standards and guidelines.", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Geyser Zamora <geyser.zamora@flexshopper.com>", | ||
@@ -57,3 +57,9 @@ "bugs": { | ||
"test": "./node_modules/.bin/gulp test" | ||
} | ||
}, | ||
"pre-commit": [ | ||
"npm outdated", | ||
"lint", | ||
"test", | ||
"fix" | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
# hapi-listeners [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] | ||
# @flexshopper/hapi-listeners [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] | ||
> | ||
@@ -9,3 +9,49 @@ | ||
## How use it | ||
- Install `hapi-listeners` npm package in your project our plugin. | ||
`npm i @flexshopper/hapi-listeners` | ||
### Registering | ||
manifest style: | ||
```javascript | ||
registrations: [ | ||
... | ||
{ | ||
plugin: { | ||
register: '@flexshopper/hapi-listeners', | ||
options: { | ||
dirname: '/lib/listeners' | ||
} | ||
} | ||
} | ||
]; | ||
``` | ||
Listener structure: | ||
```javascript | ||
module.exports = { | ||
name: 'eventName', | ||
method: (data) => { | ||
// any code that will execute when eventName is fired | ||
} | ||
}; | ||
``` | ||
Just by creating a kraken event in any route handler | ||
```javascript | ||
server.route({ | ||
method: 'GET', | ||
path: '/route', | ||
handler: (request, reply) => { | ||
request.krakenEvent = { | ||
name: 'eventName', | ||
data: {} | ||
}; | ||
} | ||
}) | ||
``` | ||
## Guidelines | ||
@@ -12,0 +58,0 @@ |
5924
68
71