All files / ima/router Request.js

53.85% Statements 7/13
9.09% Branches 2/22
45.45% Functions 5/11
53.85% Lines 7/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 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    5x             4x                         34x                     32x                 1x                 1x                                                                                                                       5x  
import ns from '../namespace';
 
ns.namespace('ima.router');
 
/**
 * Wrapper for the ExpressJS request, exposing only the necessary minimum.
 */
export default class Request {
  static get $dependencies() {
    return [];
  }
 
  /**
	 * Initializes the request.
	 */
  constructor() {
    /**
		 * The current ExpressJS request object, or {@code null} if running at
		 * the client side.
		 *
		 * @type {?Express.Request}
		 */
    this._request = null;
  }
 
  /**
	 * Initializes the request using the provided ExpressJS request object.
	 *
	 * @param {?Express.Request} request The ExpressJS request object
	 *        representing the current request. Use {@code null} at the client
	 *        side.
	 */
  init(request) {
    this._request = request;
  }
 
  /**
	 * Returns the path part of the URL to which the request was made.
	 *
	 * @return {string} The path to which the request was made.
	 */
  getPath() {
    return this._request ? this._request.originalUrl : '';
  }
 
  /**
	 * Returns the {@code Cookie} HTTP header value.
	 *
	 * @return {string} The value of the {@code Cookie} header.
	 */
  getCookieHeader() {
    return this._request ? this._request.get('Cookie') : '';
  }
 
  /**
	 * Returns uploaded file to server and meta information.
	 *
	 * @return {?Object<string, *>}
	 */
  getFile() {
    return this._request ? this._request.file : null;
  }
 
  /**
	 * Returns upaloaded files to server with their meta information.
	 *
	 * @return {?Object<string, *>}
	 */
  getFiles() {
    return this._request ? this._request.files : null;
  }
 
  /**
	 * Returns body of request.
	 *
	 * @return {?string}
	 */
  getBody() {
    return this._request ? this._request.body || null : null;
  }
 
  /**
	 * Returns the specified HTTP request header.
	 *
	 * @param {string} header
	 * @return {?string}
	 */
  getHeader(header) {
    return this._request ? this._request.get(header) || null : null;
  }
 
  /**
	 * Returns the remote IP address of the request.
	 *
	 * @return {?string}
	 */
  getIP() {
    return this._request ? this._request.ip : null;
  }
 
  /**
	 * Returns array of IP addresses specified in the “X-Forwarded-For”
	 * request header.
	 *
	 * @return {string[]}
	 */
  getIPs() {
    return this._request ? this._request.ips || [] : [];
  }
}
 
ns.ima.router.Request = Request;