Skip to content

Commit 4957202

Browse files
Added support to multiple query params.
1 parent 60bf623 commit 4957202

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/request.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ describe('Request object', () => {
2222
pathParameters: {},
2323
queryStringParameters: {
2424
a: '1',
25-
b: '2'
25+
b: '2',
26+
'c[]': 'name'
2627
},
2728
multiValueQueryStringParameters: {
2829
a: ['1'],
29-
b: ['1', '2']
30+
b: ['1', '2'],
31+
'c[]': ['-firstName', 'lastName']
3032
},
3133
stageVariables: {},
3234
requestContext: {},
@@ -43,7 +45,8 @@ describe('Request object', () => {
4345
it('should read first value of query parameter with multiple values', () => {
4446
const request = new Request(event)
4547

46-
expect(request.query.b).toEqual('1')
48+
expect(request.query.b).toEqual(['1', '2'])
49+
expect(request.query.c).toEqual(['-firstName', 'lastName'])
4750
})
4851

4952
it('should read header', () => {

src/request.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Request extends Readable {
1414
headers: { [k: string]: string }
1515
hostname: string | null
1616
method: string
17-
query: { [name: string]: string } | null
17+
query: { [name: string]: string | string[] } | null
1818
path: string
1919
url: string
2020
params: { [name: string]: string } | null
@@ -60,10 +60,11 @@ export class Request extends Readable {
6060
this.query = Object.keys(event.multiValueQueryStringParameters || {}).reduce(
6161
(queryParams, key) => {
6262
const value = event.multiValueQueryStringParameters![key] // cannot be null at this point
63-
queryParams[key] = value[0]
63+
const _key = key.replace(/\[\]/, '')
64+
queryParams[_key] = value.length > 1 ? value : value[0]
6465
return queryParams
6566
},
66-
{} as { [name: string]: string }
67+
{} as { [name: string]: string | string[] }
6768
)
6869

6970
this.path = event.path || ''

0 commit comments

Comments
 (0)