all files / src/ Request.js

100% Statements 61/61
100% Branches 44/44
100% Functions 22/22
100% Lines 61/61
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181                    137× 137× 137× 137× 137× 137× 137×   137× 136×                   134×           17× 11× 11×                                 24×   24×       187×             38×   38× 114×         114×     114× 15× 15×     114× 16× 16×     114× 29×     114× 29×       38× 24×     14×       12×                     22×       214×       393×       16×             16×     14×             69×   69× 78×               94×       15×        
'use strict';
 
var _ = require('underscore'),
    Class = require('class.extend'),
    DEFAULT_OPTS;
 
DEFAULT_OPTS = {
   logRequest: true,
};
 
module.exports = Class.extend({
 
   init: function(evt, context, opts) {
      this._started = new Date().getTime();
      this._event = evt;
      this._context = context;
      this._query = this._event.queryStringParameters || {};
      this._pathParams = this._event.pathParameters || {};
      this._headers = this._event.headers || {};
      this._opts = _.extend({}, DEFAULT_OPTS, opts);
 
      if (this._opts.logRequest) {
         this.logRequest(_.extend({
            event: 'api-request',
            path: evt.path,
            queryParams: this._query,
         }, this._opts.additionalRequestLoggingData));
      }
   },
 
   logRequest: function(reqObj) {
      // eslint-disable-next-line no-console
      console.log(JSON.stringify(reqObj));
   },
 
   _parseBody: function(throwError) {
      // By default we just squash any errors while parsing the body, but if the user
      // wants an error to be thrown, they can pass a flag to indicate this.
      if (this._getContentTypeEssence() === 'application/json') {
         try {
            return JSON.parse(this._event.body);
         } catch(err) {
            if (throwError) {
               throw err;
            }
            return null;
         }
      }
 
      return null;
   },
 
   /**
    * Returns the type and subtype, e.g. 'application/json', of the MIME type found in the
    * Content-Type header.
    *
    * @see https://mimesniff.spec.whatwg.org/#mime-type-essence
    * @return {string} The essence of the MIME or `undefined` if the Content-Type header
    * is not found
    */
   _getContentTypeEssence: function() {
      var contentType = this.header('Content-Type');
 
      return _.isString(contentType) ? contentType.replace(/;.*/, '').trim().toLowerCase() : undefined;
   },
 
   getEvent: function() {
      return this._event;
   },
 
   getContext: function() {
      return this._context;
   },
 
   validateQueryParams: function(rules) {
      var invalidFields = [];
 
      _.each(rules, function(rule, name) {
         var isValid = true,
             has = this.hasQueryParam(name),
             val = this.query(name),
             asNumber = Number(val);
 
         if (has && !_.isUndefined(rule.pattern) && !rule.pattern.test(val)) {
            this._query[name] = undefined;
            isValid = false;
         }
 
         if (has && !_.isUndefined(rule.min) && (_.isNaN(asNumber) || asNumber < rule.min)) {
            this._query[name] = undefined;
            isValid = false;
         }
 
         if (has && !_.isUndefined(rule.max) && (_.isNaN(asNumber) || asNumber > rule.max)) {
            this._query[name] = undefined;
            isValid = false;
         }
 
         if (rule.required && (!this.hasQueryParam(name) || _.isEmpty(this.query(name)))) {
            isValid = false;
         }
 
         if (isValid === false) {
            invalidFields.push(name);
         }
      }.bind(this));
 
      if (!_.isEmpty(invalidFields)) {
         return { isValid: false, msg: 'Invalid fields: ' + invalidFields.join(', ') };
      }
 
      return { isValid: true };
   },
 
   hasPathParam: function(k) {
      return !_.isUndefined(this.pathParam(k));
   },
 
   renamePathParam: function(from, to) {
      // This is helpful in a scenario where you have two different types of operations
      // that use a path param at the same location in the URL, so APIGW only allows you
      // to use a single name for the param, but in the code it would make more sense if
      // the param were named something different.
      this._pathParams[to] = this._pathParams[from];
      delete this._pathParams[from];
   },
 
   pathParam: function(k) {
      return this._pathParams[k];
   },
 
   hasQueryParam: function(k) {
      return !_.isUndefined(this.query(k));
   },
 
   query: function(k) {
      return this._query[k];
   },
 
   context: function(k) {
      return this._context[k];
   },
 
   body: function() {
      return this._event.body;
   },
 
   parsedBody: function(throwError) {
      if (_.isUndefined(this._parsedBody)) {
         this._parsedBody = this._parseBody(throwError);
      }
 
      return this._parsedBody;
   },
 
   isBase64Encoded: function() {
      return !!this._event.isBase64Encoded;
   },
 
   header: function(k) {
      var userKey = (k || '').toLowerCase();
 
      return _.find(this._headers, function(val, key) {
         return key.toLowerCase() === userKey;
      });
   },
 
   path: function() {
      return this._event.path;
   },
 
   method: function() {
      return this._event.httpMethod;
   },
 
   started: function() {
      return this._started;
   },
 
});