UNPKG

@cxco/sdk-webhooks

Version:

DigitalCX Webhook library

31 lines (26 loc) 854 B
"use strict"; const ValidationResponse = require('./ValidationResponse'); /** * Middleware to add the validation response instance to the res.locals object * * @param {object} req * @param {object} res * @param {function} next */ module.exports = function addValidationSlotMiddleware(req, res, next) { if (typeof req.body !== 'object') { next(new TypeError('addValidationSlotMiddleware requires a parsed json body, install and use body-parser middleware')); return; } if (req.body.sessionId === undefined) { next(new Error('addValidationSlotMiddleware received a payload without sessionId, make sure it is only used on webhook routes')); return; } try { res.locals = res.locals || {}; res.locals.dcxValidationSlot = new ValidationResponse(req.body || {}); next(); } catch (e) { next(e); } };