Skip to content

Commit

Permalink
refactor: make content type header case-insensitive (#135)
Browse files Browse the repository at this point in the history
* refactor: make content type header case-insensitive

* format

* fix

* fix

* make test more clear
  • Loading branch information
danadajian authored Nov 17, 2023
1 parent de56887 commit 4ca0adb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lambda/parse-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { bodySchema, CONTENT_TYPES, headersSchema } from './schema';

export function parseRequestBody(body: string, headers: IncomingHttpHeaders) {
const headersResult = headersSchema.parse(headers);
const contentType = headersResult['content-type'] || headersResult['Content-Type'];
const contentType = headersResult['content-type'];
switch (contentType) {
case CONTENT_TYPES.JSON:
return JSON.parse(body);
Expand Down
6 changes: 3 additions & 3 deletions lambda/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ describe('proxy', () => {
const event: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
...baseEvent,
headers: {
...baseEvent.headers,
'content-type': undefined,
'Content-Type': 'application/json'
Accept: '*/*',
'Content-Type': 'application/json',
Host: 'some-api.us-west-2.amazonaws.com'
},
body: JSON.stringify(VALID_PUSH_PAYLOAD),
pathParameters: {
Expand Down
2 changes: 1 addition & 1 deletion lambda/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export async function handler(event: APIGatewayProxyWithLambdaAuthorizerEvent<an
}

console.error('An unknown error occurred.', error);
return { statusCode: 500, body: 'Internal server error' };
return { statusCode: 500, body: `Internal server error: ${error}` };
}
}
22 changes: 10 additions & 12 deletions lambda/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';
import mapKeys from 'lodash.mapkeys';

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

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

export const headersSchema = z
.object({
'content-type': z.string().optional(),
'Content-Type': z.string().optional()
})
.refine(
headers => {
return headers['content-type'] || headers['Content-Type'];
},
{
message: 'Missing Content-Type header'
export const headersSchema = z.preprocess(
obj => {
if (obj && typeof obj == 'object') {
return mapKeys(obj, (_, key) => key.toLowerCase());
}
);
},
z.object({
'content-type': z.nativeEnum(CONTENT_TYPES)
})
);

export const axiosErrorSchema = z.object({
response: z.object({
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"@types/node": "20.1.1",
"axios": "1.6.1",
"lodash.mapkeys": "4.6.0",
"query-string": "7.1.3",
"typescript": "5.2.2",
"zod": "3.22.4"
Expand All @@ -19,6 +20,7 @@
"@swc/jest": "0.2.29",
"@types/aws-lambda": "8.10.107",
"@types/jest": "29.5.8",
"@types/lodash.mapkeys": "4.6.9",
"jest": "29.7.0",
"prettier": "3.1.0"
},
Expand Down

0 comments on commit 4ca0adb

Please sign in to comment.