All files / server/router generic.js

37.5% Statements 6/16
0% Branches 0/6
50% Functions 1/2
46.15% Lines 6/13
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 3111x 11x     11x     25x   25x   25x                                      
const normalize = require('../utils/normalize');
const params = require('path-to-regexp-wrap')();
 
// Generic request handler
module.exports = (method, ...all) => {
 
  // Extracted or otherwise it'd shift once per call; also more performant
  const { path, middle } = normalize(all);
 
  const match = params(path);
 
  return async ctx => {
 
    // A route should be replied only once per request
    if (ctx.replied) return;
 
    // Only for the correct method
    if (method !== ctx.method) return;
 
    // Only do this if the correct path
    ctx.params = match(ctx.path);
    if (!ctx.params) return;
    ctx.params = ctx.params;
 
    // Perform this promise chain
    await ctx.utils.join(middle)(ctx);
 
    ctx.replied = true;
  };
};