diff --git a/index.js b/index.js index a0ca3ac..b9b9f88 100644 --- a/index.js +++ b/index.js @@ -12,19 +12,28 @@ var configurable = function (newConfig) { config = newConfig } -router.use('/', function (req, res, next) { - res.hookhub.stack.push('hookhub-slack-webhook-out') -}) +// Functions +function stackRegistration (req, res, next) { + res.locals.hookhub.stack.push('hookhub-slack-webhook-out') + next() +} -/* Default handler. */ -router.use('/', function (req, res, next) { - if (!config) { throw new Error('Missing configuration') } +function defaultHandler (req, res, next) { + debug('Handling request') - if (!res.hookhub.doc) { throw new Error('Missing hookhub.doc') } + if (!config) { + throw new Error('Missing configuration') + } - debug('Handling request') + if (!res.locals.hookhub.doc) { + throw new Error('Missing hookhub.doc') + } - let post_body = generateMessage(res.hookhub.doc) + if (res.locals.hookhub.result.result !== 'OK') { + throw new Error('Broken flow') + } + + let post_body = generateMessage(res.locals.hookhub.doc) var post_options = { method: 'POST', @@ -34,19 +43,19 @@ router.use('/', function (req, res, next) { } rp(post_options).then(function (data) { - res.hookhub.result = { + res.locals.hookhub.result = { result: 'OK', message: data } next() }).catch(function (err) { - res.hookhub.result = { + res.locals.hookhub.result = { result: 'ERROR', message: err } - next('route') + next('error') }) -}) +} var generateMessage = function (hookhub_doc) { var slack_message = smb() @@ -81,5 +90,8 @@ var generateMessage = function (hookhub_doc) { return slack_message.json() } +// Routes +router.all('/', stackRegistration, defaultHandler) + module.exports = router module.exports.configurable = configurable diff --git a/package.json b/package.json index 7729b95..1703587 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hookhub-slack-webhook-out", - "version": "1.0.0", + "version": "1.0.1", "description": "", "main": "index.js", "scripts": {