forked from scottcorgan/express-boom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (23 loc) · 839 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var boom = require('boom');
var helperMethods = ['wrap', 'create'];
module.exports = function () {
return function (req, res, next) {
if (res.boom) throw new Error('boom already exists on response object');
res.boom = {};
Object.keys(boom).forEach(function (key) {
if (typeof boom[key] !== 'function') return;
if (helperMethods.indexOf(key) !== -1) {
res.boom[key] = function () {
return boom[key].apply(boom, arguments);
};
} else {
res.boom[key] = function () {
var boomed = boom[key].apply(boom, arguments);
var boomedPayloadAndAdditionalResponse = Object.assign(boomed.output.payload, arguments[1])
return res.status(boomed.output.statusCode).send(boomedPayloadAndAdditionalResponse);
};
}
});
next();
};
};