Skip to content

Commit

Permalink
fix: Support multiple of same query string for LambdaProxyIntegration…
Browse files Browse the repository at this point in the history
…EventV2 (#1525)
  • Loading branch information
drace-rgare authored Sep 5, 2022
1 parent 70e989f commit 7416c1c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
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 @@ -12,6 +12,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 baseImage } from './baseImage.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(',') }
}, {})
}

0 comments on commit 7416c1c

Please sign in to comment.