Skip to content

Commit

Permalink
API: Take multiple values into account for the query (#9196)
Browse files Browse the repository at this point in the history
* Take multiple values into account

* make typescript happy
  • Loading branch information
Janpot authored and timneutkens committed Oct 26, 2019
1 parent 20978af commit ab0a864
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/next/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ export function getQueryParser({ url }: IncomingMessage) {

const query: { [key: string]: string | string[] } = {}
for (const [key, value] of params) {
query[key] = value

This comment has been minimized.

Copy link
@karvalyo

karvalyo Oct 27, 2019

прпрпрп

if (query[key]) {
if (Array.isArray(query[key])) {
;(query[key] as string[]).push(value)
} else {
query[key] = [query[key], value]
}
} else {
query[key] = value
}
}

return query
Expand Down
3 changes: 3 additions & 0 deletions test/integration/api-support/pages/api/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (req, res) => {
res.status(200).send(req.query)
}
17 changes: 17 additions & 0 deletions test/integration/api-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ function runTests (serverless = false) {
expect(data).toEqual({ message: 'Parsed body' })
})

it('should return empty query object', async () => {
const data = await fetchViaHTTP(appPort, '/api/query', null, {}).then(
res => res.ok && res.json()
)
expect(data).toEqual({})
})

it('should parse query correctly', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/query?a=1&b=2&a=3',
null,
{}
).then(res => res.ok && res.json())
expect(data).toEqual({ a: ['1', '3'], b: '2' })
})

it('should return empty cookies object', async () => {
const data = await fetchViaHTTP(appPort, '/api/cookies', null, {}).then(
res => res.ok && res.json()
Expand Down

0 comments on commit ab0a864

Please sign in to comment.