Skip to content

Commit

Permalink
refactor: make content type header case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian committed Nov 16, 2023
1 parent de56887 commit 14a0b5b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 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
1 change: 0 additions & 1 deletion lambda/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ describe('proxy', () => {
...baseEvent,
headers: {
...baseEvent.headers,
'content-type': undefined,
'Content-Type': 'application/json'
},
body: JSON.stringify(VALID_PUSH_PAYLOAD),
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}` };
}
}
17 changes: 4 additions & 13 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,9 @@ 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.object({
'content-type': z.nativeEnum(CONTENT_TYPES)
}).transform(obj => mapKeys(obj, (_, key) => key.toLowerCase()));

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 14a0b5b

Please sign in to comment.