Skip to content

Commit

Permalink
fix: Structure Multi-Value Params According to Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sternetj committed Nov 7, 2024
1 parent 72066b3 commit 1731849
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/adapters/helpers/lambdaEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ export const lambdaEvent = (config: AlphaOptions, relativeUrl?: string) => {
'http://fake',
querystringWithArraySupport,
);
const params = Object.assign({}, parts.query, config.params);
const multiValueQueryStringParameters: Record<string, any> = { ...params };
const params: Record<string, any> = Object.assign({}, parts.query, config.params);
let multiValueParams: Record<string, any[]> | null = null;

Object.keys(multiValueQueryStringParameters).forEach((key) => {
if (!Array.isArray(multiValueQueryStringParameters[key])) {
delete multiValueQueryStringParameters[key];
} else {
delete params[key];
}
});
const hasMultiValueParams = Object.values(params).some((value) => Array.isArray(value));

if (hasMultiValueParams) {
Object.entries(params).forEach(([key, value]) => {
multiValueParams = multiValueParams || {};
multiValueParams[key] = Array.isArray(value) ? value : [value];
params[key] = Array.isArray(value) ? value.join(',') : value;
});
}

const httpMethod = (config.method as string).toUpperCase();
const requestTime = new Date();
Expand Down Expand Up @@ -73,10 +75,7 @@ export const lambdaEvent = (config: AlphaOptions, relativeUrl?: string) => {
userArn: null,
},
},
multiValueQueryStringParameters:
Object.keys(multiValueQueryStringParameters).length > 0
? multiValueQueryStringParameters
: null,
multiValueQueryStringParameters: multiValueParams,
};

if (Buffer.isBuffer(event.body)) {
Expand Down
2 changes: 2 additions & 0 deletions test/lambda-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test('Can parse URLs with duplicate parameters', () => {
httpMethod: 'GET',
path: '/lifeomic/dstu3/Questionnaire',
queryStringParameters: {
_tag: 'http://lifeomic.com/fhir/questionnaire-type|survey-form,http://lifeomic.com/fhir/dataset|0bb18fef-4e2d-4b91-a623-09527265a8b3,http://lifeomic.com/fhir/primary|0343bfcf-4e2d-4b91-a623-095272783bf3',
pageSize: '25',
},
multiValueQueryStringParameters: {
Expand All @@ -39,6 +40,7 @@ test('Can parse URLs with duplicate parameters', () => {
'http://lifeomic.com/fhir/dataset|0bb18fef-4e2d-4b91-a623-09527265a8b3',
'http://lifeomic.com/fhir/primary|0343bfcf-4e2d-4b91-a623-095272783bf3',
],
pageSize: ['25'],
},
}));
assertRequestId(result);
Expand Down

0 comments on commit 1731849

Please sign in to comment.