All files / lib response-handler.js

100% Statements 9/9
100% Branches 6/6
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 101x 4x 1x 1x 1x 1x 3x 2x    
module.exports = function responseHandler (req, res, statusCode, payload) {
  if (req.query.callback) {
    let sanitizedCallback = req.query.callback.replace(/[^\w\d\.\(\)\[\]]/g, '') // eslint-disable-line
    res.set('Content-Type', 'application/javascript')
    res.status(statusCode)
    res.send(`${sanitizedCallback}(${JSON.stringify(payload)})`)
  } else if (req.query && req.query.f === 'pjson') res.set('Content-type', 'application/json; charset=utf-8').status(statusCode).send(JSON.stringify(payload, null, 2))
  else res.status(statusCode).json(payload)
}