All files / server-old/router generic.js

100% Statements 17/17
87.5% Branches 7/8
100% Functions 2/2
100% Lines 14/14

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 3226x 26x 26x     26x     36x   36x   36x     43x     36x     32x 32x 27x     27x   27x      
const join = require('../src/join');
const parse = require('./parse');
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 } = parse(all);
 
  const match = params(path || '');
 
  return async ctx => {
 
    // A route should be solved only once per request
    if (ctx.req.solved) return;
 
    // Only for the correct method
    if (method !== ctx.req.method) return;
 
    // Only do this if the correct path
    ctx.req.params = match(ctx.req.path);
    if (!ctx.req.params) return;
    ctx.params = ctx.req.params;
 
    // Perform this promise chain
    await join(middle)(ctx);
 
    ctx.req.solved = true;
  };
};