Skip to content

Commit aca6ab9

Browse files
author
Sebastian Sauerer
committed
fix: filter out undefinded/null values from query params
1 parent 455a4db commit aca6ab9

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/openapi-lambda-adapters.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,32 @@ describe('Adapt axios request/response to AWS Lambda Proxy Event/Response', () =
178178
expect(event.headers['x-null']).toBeUndefined()
179179
expect(event.headers['x-undefined']).toBeUndefined()
180180
})
181+
182+
it('ignores query params with undefined/null values', () => {
183+
// given
184+
const axiosConfig: AxiosRequestConfig = {
185+
method: 'get',
186+
url: '/v1/users',
187+
params: {
188+
limit: 20,
189+
offset: null,
190+
flag: undefined
191+
}
192+
}
193+
const operation: Operation = {
194+
path: '/v1/users',
195+
method: HttpMethod.Get,
196+
responses: {}
197+
}
198+
199+
// then
200+
const event = convertAxiosToApiGw(axiosConfig, operation)
201+
expect(event.pathParameters).toEqual({})
202+
expect(event.queryStringParameters).toEqual({
203+
limit: '20'
204+
})
205+
expect(event.rawQueryString).toEqual('limit=20')
206+
})
181207
})
182208

183209
describe('Api GW Proxy Response to Axios Response', () => {

src/openapi-lambda-adapters.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
4141
}
4242

4343
// extract query params -> convert each value to ta string
44-
const queryParams = Object.entries(config.params ?? {}).reduce<APIGatewayProxyEventQueryStringParameters>((queryParams, [key, val]) => {
45-
queryParams[key] = val?.toString()
44+
const queryParams = Object.entries(config.params ?? {}).filter(([_key, val]) => val !== null && val !== undefined).reduce<APIGatewayProxyEventQueryStringParameters>((queryParams, [key, val]) => {
45+
queryParams[key] = val.toString()
4646
return queryParams
4747
}, {})
4848

4949
const queryString: string[] = []
50-
Object.entries(config.params ?? {}).forEach(([key, val]) => {
50+
Object.entries(config.params ?? {}).filter(([_key, val]) => val !== null && val !== undefined).forEach(([key, val]) => {
5151
if (val && Array.isArray(val)) {
5252
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
5353
queryString.push(...val.map((entry) => `${key}=${entry.toString()}`))
@@ -56,9 +56,6 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
5656
}
5757
})
5858

59-
const urlSearchParams = new URLSearchParams()
60-
Object.entries(config.params ?? {}).forEach(([key, val]) => urlSearchParams.append(key, val.toString()))
61-
6259
const headers: Record<string, string> = {}
6360
for (const [key, val] of Object.entries(config.headers ?? {}).filter(([_key, val]) => val !== null && val !== undefined)) {
6461
headers[key] = val.toString()

0 commit comments

Comments
 (0)