All files / server/plugins/parser init.js

100% Statements 29/29
56.25% Branches 9/16
100% Functions 3/3
100% Lines 28/28
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 41 42 43 44 45 46 47 48 49 50 51 52 5313x   55x 55x 330x     55x     55x 55x 55x 110x 110x         55x 54x 54x       55x 55x 55x       55x 55x 55x       55x 55x 55x       55x 55x 55x 55x   55x 55x      
module.exports = ctx => {
 
  const modern = ctx.utils.modern;
  const options = ctx.options.parser;
  const parser = ctx.plugins.filter(p => p.name === 'parser')[0];
 
  // TODO: fix it so this is not needed
  parser.before = [];
 
  // "Method parser"
  Eif (options.method) {
    const methods = typeof options.method === 'string' ? [options.method] : options.method;
    methods.forEach(one => {
      const method = require('method-override')(one);
      parser.before.push(modern(method));
    });
  }
 
  // Body Parser
  if (options.body) {
    const body = require('body-parser').urlencoded(options.body);
    parser.before.push(modern(body));
  }
 
  // JSON parser
  Eif (options.json) {
    const json = require('body-parser').json(options.json);
    parser.before.push(modern(json));
  }
 
  // Text parser
  Eif (options.text) {
    const text = require('body-parser').text(options.text);
    parser.before.push(modern(text));
  }
 
  // Data parser
  Eif (options.data) {
    const data = require('express-data-parser')(options.data);
    parser.before.push(modern(data));
  }
 
  // Data parser
  Eif (options.cookie) {
    let secret = options.cookie.secret;
    Eif (typeof secret !== 'string') {
      secret = ctx.options.secret;
    }
    const cookie = require('cookie-parser')(secret);
    parser.before.push(modern(cookie));
  }
};