UNPKG

2.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6// @server-side class Request {__CLEAR__}\nexports.default = Request;
7
8/**
9 * Wrapper for the ExpressJS request, exposing only the necessary minimum.
10 */
11class Request {
12 static get $dependencies() {
13 return [];
14 }
15
16 /**
17 * Initializes the request.
18 */
19 constructor() {
20 /**
21 * The current ExpressJS request object, or {@code null} if running at
22 * the client side.
23 *
24 * @type {?Express.Request}
25 */
26 this._request = null;
27 }
28
29 /**
30 * Initializes the request using the provided ExpressJS request object.
31 *
32 * @param {?Express.Request} request The ExpressJS request object
33 * representing the current request. Use {@code null} at the client
34 * side.
35 */
36 init(request) {
37 this._request = request;
38 }
39
40 /**
41 * Returns the path part of the URL to which the request was made.
42 *
43 * @return {string} The path to which the request was made.
44 */
45 getPath() {
46 return this._request ? this._request.originalUrl : '';
47 }
48
49 /**
50 * Returns the {@code Cookie} HTTP header value.
51 *
52 * @return {string} The value of the {@code Cookie} header.
53 */
54 getCookieHeader() {
55 return this._request ? this._request.get('Cookie') : '';
56 }
57
58 /**
59 * Returns uploaded file to server and meta information.
60 *
61 * @return {?Object<string, *>}
62 */
63 getFile() {
64 return this._request ? this._request.file : null;
65 }
66
67 /**
68 * Returns upaloaded files to server with their meta information.
69 *
70 * @return {?Object<string, *>}
71 */
72 getFiles() {
73 return this._request ? this._request.files : null;
74 }
75
76 /**
77 * Returns body of request.
78 *
79 * @return {?string}
80 */
81 getBody() {
82 return this._request ? this._request.body || null : null;
83 }
84
85 /**
86 * Returns the specified HTTP request header.
87 *
88 * @param {string} header
89 * @return {?string}
90 */
91 getHeader(header) {
92 return this._request ? this._request.get(header) || null : null;
93 }
94
95 /**
96 * Returns the remote IP address of the request.
97 *
98 * @return {?string}
99 */
100 getIP() {
101 return this._request ? this._request.ip : null;
102 }
103
104 /**
105 * Returns array of IP addresses specified in the “X-Forwarded-For”
106 * request header.
107 *
108 * @return {string[]}
109 */
110 getIPs() {
111 return this._request ? this._request.ips || [] : [];
112 }
113}
114exports.default = Request;
115
116typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/router/Request', [], function (_export, _context) {
117 'use strict';
118 return {
119 setters: [],
120 execute: function () {
121 _export('default', exports.default);
122 }
123 };
124});