All files / copacetic/lib middleware.js

100% Statements 10/10
81.81% Branches 9/11
100% Functions 3/3
100% Lines 10/10

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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                  1x 6x 2x 1x   1x       6x 5x               3x               1x 1x      
/**
 * @constructs Express middleware to serve the health of your dependencies
 * @param {Copacetic} copacetic
 * @param {Integer} [interval] How regular services will be checked
 * @param {Array<Object>} [dependencies] An explicit set of dependencies to be polled
 * @param {Boolean} [parallel=true] Check in sequence, or parallel
 * @param {String} [schedule=start] Schedule the next check as soon as a poll starts, or wait
 * until it ends
 */
module.exports = ({ copacetic, interval, dependencies, parallel, schedule, verbose = true }) => {
  if (interval) {
    if (dependencies) {
      copacetic.poll({ dependencies, interval, parallel, schedule })
    } else {
      copacetic.pollAll({ interval, parallel, schedule })
    }
  }
 
  if (verbose) {
    return (req, res) => {
      /**
       * Health information about your service
       * @typedef {Object} Copacetic~HealthResponse
       * @property {String} Name - the name of your service
       * @property {Boolean} Healthy
       * @property {Array<HealthInfo>}
       */
      return res.json({
        name: copacetic.name || 'service',
        healthy: copacetic.isHealthy,
        dependencies: copacetic.healthInfo
      })
    }
  }
 
  return (req, res) => {
    return res.sendStatus(copacetic.isHealthy ? 200 : 503)
  }
}