Skip to content

Commit b7aad44

Browse files
authored
Merge pull request #6 from iDanielBot/fix/stringify-headers
Fix/stringify headers
2 parents 0aecd63 + 0c152b1 commit b7aad44

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/openapi-lambda-adapters.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,43 @@ describe('Adapt axios request/response to AWS Lambda Proxy Event/Response', () =
141141
})
142142
expect(event.rawQueryString).toEqual('colors=red&colors=blue&number=0&boolean=false')
143143
})
144+
145+
it('converts axios call with non-string headers', () => {
146+
// given
147+
const axiosConfig: AxiosRequestConfig = {
148+
method: 'get',
149+
url: '/v1/users',
150+
headers: {
151+
'x-boolean': true,
152+
'x-number': 100,
153+
'x-zero': 0,
154+
'x-object': {
155+
key: 'value'
156+
},
157+
'x-array': ['a', 'b'],
158+
'x-null': null,
159+
'x-undefined': undefined,
160+
'x-string': 'string'
161+
} as unknown as Record<string, string>
162+
}
163+
const operation: Operation = {
164+
path: '/v1/users',
165+
method: HttpMethod.Get,
166+
responses: {}
167+
}
168+
169+
// then
170+
const event = convertAxiosToApiGw(axiosConfig, operation)
171+
172+
expect(event.headers['x-boolean']).toBe('true')
173+
expect(event.headers['x-number']).toBe('100')
174+
expect(event.headers['x-zero']).toBe('0')
175+
expect(event.headers['x-string']).toBe('string')
176+
expect(event.headers['x-object']).toBe('[object Object]')
177+
expect(event.headers['x-array']).toBe('a,b')
178+
expect(event.headers['x-null']).toBeUndefined()
179+
expect(event.headers['x-undefined']).toBeUndefined()
180+
})
144181
})
145182

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

src/openapi-lambda-adapters.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
5858
const urlSearchParams = new URLSearchParams()
5959
Object.entries(config.params ?? {}).forEach(([key, val]) => urlSearchParams.append(key, val.toString()))
6060

61+
const headers: Record<string, string> = {}
62+
for (const [key, val] of Object.entries(config.headers ?? {}).filter(([_key, val]) => val !== null && val !== undefined)) {
63+
headers[key] = val.toString()
64+
}
65+
6166
const lambdaPayload = {
6267
version: '2.0',
6368
routeKey: '$default',
6469
rawPath: config.url,
65-
headers: { ...(config.headers ?? {}) },
70+
headers,
6671
queryStringParameters: queryParams,
6772
rawQueryString: queryString.join('&'),
6873
pathParameters: pathParams,

0 commit comments

Comments
 (0)