Skip to content

Commit 4ca0adb

Browse files
authored
refactor: make content type header case-insensitive (#135)
* refactor: make content type header case-insensitive * format * fix * fix * make test more clear
1 parent de56887 commit 4ca0adb

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

lambda/parse-request-body.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { bodySchema, CONTENT_TYPES, headersSchema } from './schema';
1717

1818
export function parseRequestBody(body: string, headers: IncomingHttpHeaders) {
1919
const headersResult = headersSchema.parse(headers);
20-
const contentType = headersResult['content-type'] || headersResult['Content-Type'];
20+
const contentType = headersResult['content-type'];
2121
switch (contentType) {
2222
case CONTENT_TYPES.JSON:
2323
return JSON.parse(body);

lambda/proxy.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ describe('proxy', () => {
123123
const event: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
124124
...baseEvent,
125125
headers: {
126-
...baseEvent.headers,
127-
'content-type': undefined,
128-
'Content-Type': 'application/json'
126+
Accept: '*/*',
127+
'Content-Type': 'application/json',
128+
Host: 'some-api.us-west-2.amazonaws.com'
129129
},
130130
body: JSON.stringify(VALID_PUSH_PAYLOAD),
131131
pathParameters: {

lambda/proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ export async function handler(event: APIGatewayProxyWithLambdaAuthorizerEvent<an
6161
}
6262

6363
console.error('An unknown error occurred.', error);
64-
return { statusCode: 500, body: 'Internal server error' };
64+
return { statusCode: 500, body: `Internal server error: ${error}` };
6565
}
6666
}

lambda/schema.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from 'zod';
2+
import mapKeys from 'lodash.mapkeys';
23

34
export const urlSchema = z.string().url();
45

@@ -11,19 +12,16 @@ export const CONTENT_TYPES = {
1112
URL_ENCODED: 'application/x-www-form-urlencoded'
1213
} as const;
1314

14-
export const headersSchema = z
15-
.object({
16-
'content-type': z.string().optional(),
17-
'Content-Type': z.string().optional()
18-
})
19-
.refine(
20-
headers => {
21-
return headers['content-type'] || headers['Content-Type'];
22-
},
23-
{
24-
message: 'Missing Content-Type header'
15+
export const headersSchema = z.preprocess(
16+
obj => {
17+
if (obj && typeof obj == 'object') {
18+
return mapKeys(obj, (_, key) => key.toLowerCase());
2519
}
26-
);
20+
},
21+
z.object({
22+
'content-type': z.nativeEnum(CONTENT_TYPES)
23+
})
24+
);
2725

2826
export const axiosErrorSchema = z.object({
2927
response: z.object({

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dependencies": {
1111
"@types/node": "20.1.1",
1212
"axios": "1.6.1",
13+
"lodash.mapkeys": "4.6.0",
1314
"query-string": "7.1.3",
1415
"typescript": "5.2.2",
1516
"zod": "3.22.4"
@@ -19,6 +20,7 @@
1920
"@swc/jest": "0.2.29",
2021
"@types/aws-lambda": "8.10.107",
2122
"@types/jest": "29.5.8",
23+
"@types/lodash.mapkeys": "4.6.9",
2224
"jest": "29.7.0",
2325
"prettier": "3.1.0"
2426
},

0 commit comments

Comments
 (0)