@@ -2,6 +2,7 @@ const EventEmitter = require('events');
22
33const AWS_RES_BODY = Symbol ( 'body' )
44const AWS_RES_HEADERS = Symbol ( 'body' )
5+ const AWS_RES_MULTI_VALUE_HEADERS = Symbol ( 'body' )
56const ON_FINISHED = Symbol ( 'body' )
67
78/**
@@ -20,6 +21,7 @@ class Response extends EventEmitter {
2021 this . statusCode = 200
2122 // Non-Express compatible props: Use symbols to avoid name clashes
2223 this [ AWS_RES_HEADERS ] = { }
24+ this [ AWS_RES_MULTI_VALUE_HEADERS ] = undefined
2325 this [ AWS_RES_BODY ] = ''
2426 this [ ON_FINISHED ] = onFinished
2527 }
@@ -40,6 +42,7 @@ class Response extends EventEmitter {
4042 headers : this [ AWS_RES_HEADERS ] ,
4143 body : bodyStr
4244 }
45+ if ( this [ AWS_RES_MULTI_VALUE_HEADERS ] ) apiGatewayResult . multiValueHeaders = this [ AWS_RES_MULTI_VALUE_HEADERS ]
4346 this [ ON_FINISHED ] ( apiGatewayResult )
4447 }
4548
@@ -152,6 +155,58 @@ class Response extends EventEmitter {
152155 return this ;
153156 }
154157
158+ /**
159+ * Set cookie
160+ *
161+ * @param {string } key Cookie name
162+ * @param {string } value Cookie value
163+ * @param {{domain: string, expires: Date, maxAge: number, path: string, sameSite: string, httpOnly:boolean, secure: boolean} } options
164+ */
165+ cookie ( key , value , options = { } ) {
166+ if ( ! options [ 'path' ] ) {
167+ options [ 'path' ] = '/'
168+ }
169+ let cookieStr = `${ key } =${ value } ` ;
170+ for ( const optKey in options ) {
171+ switch ( optKey . toLocaleLowerCase ( ) ) {
172+ case 'domain' :
173+ cookieStr += `; Domain=${ options [ optKey ] } `
174+ break
175+ case 'expires' :
176+ cookieStr += `; Expires=${ options [ optKey ] . toUTCString ( ) } `
177+ break
178+ case 'maxage' :
179+ cookieStr += `; Max-Age=${ options [ optKey ] } `
180+ break
181+ case 'path' :
182+ cookieStr += `; Path=${ options [ optKey ] } `
183+ break
184+ case 'samesite' :
185+ cookieStr += `; SameSite=${ options [ optKey ] } `
186+ break
187+ case 'httponly' :
188+ if ( options [ optKey ] ) {
189+ cookieStr += '; HttpOnly'
190+ }
191+ break
192+ case 'secure' :
193+ if ( options [ optKey ] ) {
194+ cookieStr += '; Secure'
195+ }
196+ break
197+ default :
198+ console . warn ( `Warning: Cookie paramterer ${ optKey } not supported` )
199+ }
200+ }
201+ if ( ! this [ AWS_RES_MULTI_VALUE_HEADERS ] ) {
202+ this [ AWS_RES_MULTI_VALUE_HEADERS ] = { 'Set-Cookie' : [ cookieStr ] } ;
203+ } else {
204+ const cookies = this [ AWS_RES_MULTI_VALUE_HEADERS ] [ 'Set-Cookie' ] || [ ]
205+ cookies . push ( cookieStr )
206+ }
207+ return this ;
208+ }
209+
155210 /**
156211 * Set status code for response
157212 *
0 commit comments