Skip to content

Commit 40e6ab5

Browse files
committed
Cookie support in response
1 parent 279a018 commit 40e6ab5

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/lambda-wrapper.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ describe('Lambda Wrapper', () => {
130130
})
131131
const lambdaHandler = ApiGatewayHandler(router)
132132
const out = await lambdaHandler(proxyRequest, {})
133-
console.log(out)
134133
})
135134

136135
it('should handle next(error)', async () => {

src/response.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const EventEmitter = require('events');
22

33
const AWS_RES_BODY = Symbol('body')
44
const AWS_RES_HEADERS = Symbol('body')
5+
const AWS_RES_MULTI_VALUE_HEADERS = Symbol('body')
56
const 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
*

src/response.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ describe('Response object', () => {
4646
res.end()
4747
});
4848

49+
it('set cookies', done => {
50+
const res = new Response(null, out => {
51+
expect(out.multiValueHeaders).toEqual({
52+
'Set-Cookie': [
53+
'foo=1234; Path=/',
54+
'bar=5678; Path=/docs',
55+
'foo2=1234; Domain=example.com; Path=/',
56+
'foo3=1234; Expires=Fri, 25 Sep 2020 22:00:00 GMT; Path=/',
57+
'foo4=1234; Max-Age=456879; Path=/',
58+
'foo5=1234; Secure; SameSite=None; Path=/',
59+
'foo6=1234; HttpOnly; Path=/'
60+
]
61+
})
62+
done()
63+
});
64+
res.cookie('foo', '1234');
65+
res.cookie('bar', '5678', {path: '/docs'})
66+
res.cookie('foo2', '1234', {domain: 'example.com'})
67+
res.cookie('foo3', '1234', {expires: new Date(2020, 8, 26)})
68+
res.cookie('foo4', '1234', {maxAge: 456879})
69+
res.cookie('foo5', '1234', {secure: true, sameSite: 'None'})
70+
res.cookie('foo6', '1234', {httpOnly: true})
71+
res.end()
72+
});
73+
4974
it('can chain status method', done => {
5075
const res = new Response(null, out => {
5176
expect(out.statusCode).toBe(201);

0 commit comments

Comments
 (0)