Skip to content

Commit c32758c

Browse files
author
Daniel Bot
committed
fix: ignore null & undefined query params & headers, when converting axios calls to lambda invoke
1 parent 1ce6bd1 commit c32758c

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/openapi-lambda-adapters.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ describe('Adapt axios request/response to AWS Lambda Proxy Event/Response', () =
8080
expect(event.rawQueryString).toEqual('limit=20&offset=100')
8181
})
8282

83+
it('converts axios call with null query params', () => {
84+
// given
85+
const axiosConfig: AxiosRequestConfig = {
86+
method: 'get',
87+
url: '/v1/users',
88+
params: {
89+
limit: 20,
90+
offset: null,
91+
flag: undefined,
92+
}
93+
}
94+
const operation: Operation = {
95+
path: '/v1/users',
96+
method: HttpMethod.Get,
97+
responses: {}
98+
}
99+
100+
// then
101+
const event = convertAxiosToApiGw(axiosConfig, operation)
102+
expect(event.pathParameters).toEqual({})
103+
expect(event.queryStringParameters).toEqual({
104+
limit: '20',
105+
})
106+
expect(event.rawQueryString).toEqual('limit=20')
107+
})
108+
83109
it('converts axios call with both path & query params', () => {
84110
// given
85111
const axiosConfig: AxiosRequestConfig = {
@@ -178,6 +204,7 @@ describe('Adapt axios request/response to AWS Lambda Proxy Event/Response', () =
178204
expect(event.headers['x-null']).toBeUndefined()
179205
expect(event.headers['x-undefined']).toBeUndefined()
180206
})
207+
181208
})
182209

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

src/openapi-lambda-adapters.ts

Lines changed: 5 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]) => {
44+
const queryParams = Object.entries(config.params ?? {}).filter(entryValueExists).reduce<APIGatewayProxyEventQueryStringParameters>((queryParams, [key, val]) => {
4545
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(entryValueExists).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,11 +56,8 @@ 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> = {}
63-
for (const [key, val] of Object.entries(config.headers ?? {}).filter(([_key, val]) => val !== null && val !== undefined)) {
60+
for (const [key, val] of Object.entries(config.headers ?? {}).filter(entryValueExists)) {
6461
headers[key] = val.toString()
6562
}
6663

@@ -124,6 +121,8 @@ export const convertApiGwToAxios = (resp: APIGatewayProxyStructuredResultV2, axi
124121
return axiosResp
125122
}
126123

124+
const entryValueExists = (entry: [string, unknown]): boolean => entry[1] !== null && entry[1] !== undefined
125+
127126
class AxiosError extends Error {
128127
public readonly code: string
129128
public readonly config: AxiosRequestConfig

0 commit comments

Comments
 (0)