Skip to content

Commit

Permalink
Further modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
TyIsI committed Mar 31, 2019
1 parent cf65d4f commit e461cca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hookhub-slack-webhook-out",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e461cca

Please sign in to comment.