All files base-request.js

96.3% Statements 78/81
91.43% Branches 32/35
100% Functions 20/20
96.3% Lines 78/81

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 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    5x 134x       134x 134x 134x 134x 134x 134x 134x     5x 35x 706x       5x   5x   5x   5x   5x       5x       5x   5x 118x     118x 118x       7x   118x 112x   118x     5x 2x 2x 2x           5x 6x 6x 5x       19x     17x             5x 109x 37x 37x   72x   72x 72x 72x 5x   67x           5x   5x 20x 504x 504x       5x   5x   5x   5x   5x 15x 207x 223x   207x       5x       5x       5x   5x 110x 64x   110x     5x 223x 8x   215x 193x   22x     5x 134x     5x 134x    
'use strict';
 
var Request = function(builder) {
  Iif (!builder) {
    throw new Error('No builder supplied to constructor');
  }
 
  this.host = builder.host;
  this.port = builder.port;
  this.scheme = builder.scheme;
  this.queryParameters = builder.queryParameters;
  this.bodyParameters = builder.bodyParameters;
  this.headers = builder.headers;
  this.path = builder.path;
};
 
Request.prototype._getter = function(key) {
  return function() {
    return this[key];
  };
};
 
Request.prototype.getHost = Request.prototype._getter('host');
 
Request.prototype.getPort = Request.prototype._getter('port');
 
Request.prototype.getScheme = Request.prototype._getter('scheme');
 
Request.prototype.getPath = Request.prototype._getter('path');
 
Request.prototype.getQueryParameters = Request.prototype._getter(
  'queryParameters'
);
 
Request.prototype.getBodyParameters = Request.prototype._getter(
  'bodyParameters'
);
 
Request.prototype.getHeaders = Request.prototype._getter('headers');
 
Request.prototype.getURI = function() {
  Iif (!this.scheme || !this.host || !this.port) {
    throw new Error('Missing components necessary to construct URI');
  }
  var uri = this.scheme + '://' + this.host;
  if (
    (this.scheme === 'http' && this.port !== 80) ||
    (this.scheme === 'https' && this.port !== 443)
  ) {
    uri += ':' + this.port;
  }
  if (this.path) {
    uri += this.path;
  }
  return uri;
};
 
Request.prototype.getURL = function() {
  var uri = this.getURI();
  Eif (this.getQueryParameters()) {
    return uri + this.getQueryParameterString(this.getQueryParameters());
  } else {
    return uri;
  }
};
 
Request.prototype.getQueryParameterString = function() {
  var queryParameters = this.getQueryParameters();
  if (queryParameters) {
    return (
      '?' +
      Object.keys(queryParameters)
        .filter(function(key) {
          return queryParameters[key] !== undefined;
        })
        .map(function(key) {
          return key + '=' + queryParameters[key];
        })
        .join('&')
    );
  }
};
 
Request.prototype.execute = function(method, callback) {
  if (callback) {
    method(this, callback);
    return;
  }
  var _self = this;
 
  return new Promise(function(resolve, reject) {
    method(_self, function(error, result) {
      if (error) {
        reject(error);
      } else {
        resolve(result);
      }
    });
  });
};
 
var Builder = function() {};
 
Builder.prototype._setter = function(key) {
  return function(value) {
    this[key] = value;
    return this;
  };
};
 
Builder.prototype.withHost = Builder.prototype._setter('host');
 
Builder.prototype.withPort = Builder.prototype._setter('port');
 
Builder.prototype.withScheme = Builder.prototype._setter('scheme');
 
Builder.prototype.withPath = Builder.prototype._setter('path');
 
Builder.prototype._assigner = function(key) {
  return function() {
    for (var i = 0; i < arguments.length; i++) {
      this[key] = this._assign(this[key], arguments[i]);
    }
    return this;
  };
};
 
Builder.prototype.withQueryParameters = Builder.prototype._assigner(
  'queryParameters'
);
 
Builder.prototype.withBodyParameters = Builder.prototype._assigner(
  'bodyParameters'
);
 
Builder.prototype.withHeaders = Builder.prototype._assigner('headers');
 
Builder.prototype.withAuth = function(accessToken) {
  if (accessToken) {
    this.withHeaders({ Authorization: 'Bearer ' + accessToken });
  }
  return this;
};
 
Builder.prototype._assign = function(src, obj) {
  if (obj && Array.isArray(obj)) {
    return obj;
  }
  if (obj && Object.keys(obj).length > 0) {
    return Object.assign(src || {}, obj);
  }
  return src;
};
 
Builder.prototype.build = function() {
  return new Request(this);
};
 
module.exports.builder = function() {
  return new Builder();
};