1+ /**
2+ * Response Object
3+ */
14class Response {
2- constructor ( callback ) {
5+ /**
6+ * Response object constructor
7+ *
8+ * @param {Request } req Request object for this Response
9+ * @param {function } callback AWS Lambda callback function
10+ */
11+ constructor ( req , callback ) {
12+ this . req = req ;
313 this . callback = callback ;
414 this . responseObj = {
515 statusCode : 200 ,
@@ -8,26 +18,76 @@ class Response {
818 } ;
919 }
1020
11- set ( key , value ) {
12- this . responseObj . headers [ key ] = value ;
13- }
14- status ( status ) {
15- this . responseObj . statusCode = status ;
21+ /**
22+ * Ends the response process.
23+ */
24+ end ( ) {
25+ this . callback ( null , this . responseObj ) ;
1626 }
1727
18- send ( body ) {
19- this . responseObj . body = body ;
20- this . end ( ) ;
28+ /**
29+ * Get response header value for given key
30+ *
31+ * @param {string } key Header key to get
32+ */
33+ get ( key ) {
34+ return this . responseObj . headers [ key . toLowerCase ( ) ] ;
2135 }
2236
37+ /**
38+ * Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().
39+ *
40+ * The parameter can be any JSON type, including object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON.
41+ *
42+ * ```js
43+ * res.json(null)
44+ * res.json({ user: 'tobi' })
45+ * res.status(500).json({ error: 'message' })
46+ * ```
47+ * @param {any } body Any type of oject
48+ */
2349 json ( body ) {
2450 this . responseObj . body = JSON . stringify ( body ) ;
2551 this . set ( 'Content-Type' , 'application/json' ) ;
2652 this . end ( ) ;
2753 }
2854
29- end ( ) {
30- this . callback ( null , this . responseObj ) ;
55+ /**
56+ * Sends the HTTP response.
57+ *
58+ * @param {any } body
59+ */
60+ send ( body ) {
61+ this . responseObj . body = body ;
62+ this . end ( ) ;
63+ }
64+
65+ /**
66+ * Set response header
67+ *
68+ * @param {string } key Header key
69+ * @param {string } value Header value
70+ */
71+ set ( key , value ) {
72+ this . responseObj . headers [ key . toLowerCase ( ) ] = value ;
73+ }
74+
75+ /**
76+ * Set status code for response
77+ *
78+ * @param {integer } status Status code. Ex: 200, 201, 400, 404, 500 etc.
79+ */
80+ status ( status ) {
81+ this . responseObj . statusCode = status ;
82+ }
83+
84+ /**
85+ * Sets the Content-Type HTTP header
86+ *
87+ * @param {string } type Mime type will be set as Content-Type response header
88+ */
89+ type ( type ) {
90+ this . set ( 'Content-Type' , type ) ;
3191 }
3292}
3393
0 commit comments