11const ReadableStream = require ( 'stream' ) . Readable ;
2+ const accepts = require ( 'accepts' ) ;
3+ const typeis = require ( 'type-is' ) ;
24
35/**
46 *
57 */
68class Request extends ReadableStream {
79 constructor ( event ) {
810 super ( ) ;
11+
912 this . headers = Object . keys ( event . headers ) . reduce ( ( headers , key ) => {
1013 headers [ key . toLowerCase ( ) ] = event . headers [ key ] ;
1114 return headers ;
1215 } , { } ) ;
16+
1317 this . hostname = this . headers . host
1418 this . method = event . httpMethod ;
1519 this . query = event . queryStringParameters ;
@@ -24,17 +28,178 @@ class Request extends ReadableStream {
2428 this . xhr = ( this . get ( 'X-Requested-With' ) || '' ) . toLowerCase ( ) === 'xmlhttprequest' ;
2529
2630 this . event = event ;
31+ this . accept = accepts ( this ) ;
2732
2833 this . push ( event . body ) ;
2934 this . push ( null ) ;
3035 }
3136
37+ /**
38+ * Check if the given `type(s)` is acceptable, returning
39+ * the best match when true, otherwise `undefined`, in which
40+ * case you should respond with 406 "Not Acceptable".
41+ *
42+ * The `type` value may be a single MIME type string
43+ * such as "application/json", an extension name
44+ * such as "json", a comma-delimited list such as "json, html, text/plain",
45+ * an argument list such as `"json", "html", "text/plain"`,
46+ * or an array `["json", "html", "text/plain"]`. When a list
47+ * or array is given, the _best_ match, if any is returned.
48+ *
49+ * Examples:
50+ *
51+ * // Accept: text/html
52+ * req.accepts('html');
53+ * // => "html"
54+ *
55+ * // Accept: text/*, application/json
56+ * req.accepts('html');
57+ * // => "html"
58+ * req.accepts('text/html');
59+ * // => "text/html"
60+ * req.accepts('json, text');
61+ * // => "json"
62+ * req.accepts('application/json');
63+ * // => "application/json"
64+ *
65+ * // Accept: text/*, application/json
66+ * req.accepts('image/png');
67+ * req.accepts('png');
68+ * // => undefined
69+ *
70+ * // Accept: text/*;q=.5, application/json
71+ * req.accepts(['html', 'json']);
72+ * req.accepts('html', 'json');
73+ * req.accepts('html, json');
74+ * // => "json"
75+ *
76+ * @param {String|Array } type(s)
77+ * @return {String|Array|Boolean }
78+ * @public
79+ */
80+ accepts ( ) {
81+ return this . accept . types . apply ( this . accept , arguments ) ;
82+ }
83+
84+ /**
85+ * Check if the given `encoding`s are accepted.
86+ *
87+ * @param {String } ...encoding
88+ * @return {String|Array }
89+ * @public
90+ */
91+ acceptsEncodings ( ) {
92+ return this . accept . encodings . apply ( this . accept , arguments ) ;
93+ }
94+
95+ /**
96+ * Check if the given `charset`s are acceptable,
97+ * otherwise you should respond with 406 "Not Acceptable".
98+ *
99+ * @param {String } ...charset
100+ * @return {String|Array }
101+ * @public
102+ */
103+ acceptsCharsets ( ) {
104+ return this . accept . charsets . apply ( this . accept , arguments ) ;
105+ }
106+
107+ /**
108+ * Check if the given `lang`s are acceptable,
109+ * otherwise you should respond with 406 "Not Acceptable".
110+ *
111+ * @param {String } ...lang
112+ * @return {String|Array }
113+ * @public
114+ */
115+ acceptsLanguages ( ) {
116+ return this . accept . languages . apply ( this . accept , arguments ) ;
117+ }
118+
119+ /**
120+ * Return request header.
121+ *
122+ * The `Referrer` header field is special-cased,
123+ * both `Referrer` and `Referer` are interchangeable.
124+ *
125+ * Examples:
126+ *
127+ * req.get('Content-Type');
128+ * // => "text/plain"
129+ *
130+ * req.get('content-type');
131+ * // => "text/plain"
132+ *
133+ * req.get('Something');
134+ * // => undefined
135+ *
136+ * Aliased as `req.header()`.
137+ *
138+ * @param {String } name
139+ * @return {String }
140+ * @public
141+ */
32142 get ( key ) {
33- return this . headers [ key . toLowerCase ( ) ] ;
143+ return this . header ( key ) ;
144+ }
145+
146+ header ( name ) {
147+ if ( ! name ) {
148+ throw new TypeError ( 'name argument is required to req.get' ) ;
149+ }
150+
151+ if ( typeof name !== 'string' ) {
152+ throw new TypeError ( 'name must be a string to req.get' ) ;
153+ }
154+
155+ const lc = name . toLowerCase ( ) ;
156+
157+ switch ( lc ) {
158+ case 'referer' :
159+ case 'referrer' :
160+ return this . headers . referrer
161+ || this . headers . referer ;
162+ default :
163+ return this . headers [ lc ] ;
164+ }
34165 }
35166
36- is ( mimeType ) {
37- return this . get ( 'Content-Type' ) && this . get ( 'Content-Type' ) . indexOf ( mimeType ) > - 1 ;
167+ /**
168+ * Check if the incoming request contains the "Content-Type"
169+ * header field, and it contains the give mime `type`.
170+ *
171+ * Examples:
172+ *
173+ * // With Content-Type: text/html; charset=utf-8
174+ * req.is('html');
175+ * req.is('text/html');
176+ * req.is('text/*');
177+ * // => true
178+ *
179+ * // When Content-Type is application/json
180+ * req.is('json');
181+ * req.is('application/json');
182+ * req.is('application/*');
183+ * // => true
184+ *
185+ * req.is('html');
186+ * // => false
187+ *
188+ * @param {String|Array } types...
189+ * @return {String|false|null }
190+ * @public
191+ */
192+ is ( types ) {
193+ var arr = types ;
194+
195+ // support flattened arguments
196+ if ( ! Array . isArray ( types ) ) {
197+ arr = new Array ( arguments . length ) ;
198+ for ( var i = 0 ; i < arr . length ; i ++ ) {
199+ arr [ i ] = arguments [ i ] ;
200+ }
201+ }
202+ return typeis ( this , arr ) ;
38203 }
39204}
40205
0 commit comments