Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support multiple of same query string for LambdaProxyIntegrationEventV2 #1525

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
lowerCaseKeys,
nullIfEmpty,
parseHeaders,
parseQueryStringParametersForPayloadV2,
} from '../../../utils/index.js'

const { isArray } = Array
const { parse } = JSON
const { assign, entries, fromEntries } = Object
const { assign, entries } = Object

// https://www.serverless.com/framework/docs/providers/aws/events/http-api/
// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
Expand Down Expand Up @@ -142,7 +143,7 @@ export default class LambdaProxyIntegrationEventV2 {
isBase64Encoded: false,
pathParameters: nullIfEmpty(pathParams),
queryStringParameters: this.#request.url.search
? fromEntries(Array.from(this.#request.url.searchParams))
? parseQueryStringParametersForPayloadV2(this.#request.url.searchParams)
: null,
rawPath: this.#request.url.pathname,
rawQueryString: this.#request.url.searchParams.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const tests = [
description: 'multiple parameters, same keys',
expected: { foo: 'foobar' },
expectedMulti: { foo: ['test', 'foobar'] },
expectedV2: { foo: 'test,foobar' },
param: 'foo=test&foo=foobar',
},

Expand All @@ -55,6 +56,7 @@ const tests = [
description: 'multiple parameters, same keys, same values',
expected: { foo: 'test' },
expectedMulti: { foo: ['test', 'test'] },
expectedV2: { foo: 'test,test' },
param: 'foo=test&foo=test',
},

Expand Down
22 changes: 22 additions & 0 deletions src/utils/__tests__/parseQueryStringParametersForPayloadV2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'node:assert'

// uses the same tests as parseMultiValueQueryStringParameters
import tests from './parseMultiValueQueryStringParameters.test.js'
import parseQueryStringParametersForPayloadV2 from '../parseQueryStringParametersForPayloadV2.js'
import { BASE_URL_PLACEHOLDER } from '../../config/index.js'

describe('parseQueryStringParametersForPayloadV2', () => {
tests.forEach(({ description, expected, expectedV2, param }) => {
const url = `/foo?${param}`
const { searchParams } = new URL(url, BASE_URL_PLACEHOLDER)

it(`should return ${description}`, () => {
const result = parseQueryStringParametersForPayloadV2(searchParams)
if (expectedV2) {
assert.deepEqual(result, expectedV2)
} else {
assert.deepEqual(result, expected)
}
})
})
})
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { default as parseHeaders } from './parseHeaders.js'
export { default as parseMultiValueHeaders } from './parseMultiValueHeaders.js'
export { default as parseMultiValueQueryStringParameters } from './parseMultiValueQueryStringParameters.js'
export { default as parseQueryStringParameters } from './parseQueryStringParameters.js'
export { default as parseQueryStringParametersForPayloadV2 } from './parseQueryStringParametersForPayloadV2.js'
export { default as splitHandlerPathAndName } from './splitHandlerPathAndName.js'
export { default as checkDockerDaemon } from './checkDockerDaemon.js'
export { default as checkGoVersion } from './checkGoVersion.js'
Expand Down
22 changes: 22 additions & 0 deletions src/utils/parseQueryStringParametersForPayloadV2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
*
* @description Instead of using `multiValueQueryStringParameters` API Gateway HTTP API combines
* duplicate query string keys with commas in the `queryStringParameters` field.
* https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
*
* @param { URLSearchParams } searchParams
*/
export default function parseQueryStringParametersForPayloadV2(searchParams) {
const keyValuePairs = Array.from(searchParams)

if (keyValuePairs.length === 0) {
return null
}

return keyValuePairs.reduce((previousValue, [key, value]) => {
if (!previousValue[key]) {
return { ...previousValue, [key]: value }
}
return { ...previousValue, [key]: [previousValue[key], value].join(',') }
}, {})
}