All files / modern index.js

100% Statements 9/9
100% Branches 2/2
100% Functions 4/4
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23      1x     1x     26x     19x 16x     12x     12x      
// Modern - Create a modern middleware from the old-style one
 
// Cleanly validate data
const validate = require('./validate');
 
// Pass it an old middleware and return a new one 'ctx => promise'
module.exports = middle => {
 
  // Validate it early so no requests need to be made
  validate.middleware(middle);
 
  // Create and return the modern middleware function
  return ctx => new Promise((resolve, reject) => {
    validate.context(ctx);
 
    // It can handle both success or errors. Pass the right ctx
    const next = err => err ? reject(err) : resolve(ctx);
 
    // Call the old middleware
    middle(ctx.req, ctx.res, next);
  });
}