File tree Expand file tree Collapse file tree 4 files changed +26
-7
lines changed
Expand file tree Collapse file tree 4 files changed +26
-7
lines changed Original file line number Diff line number Diff line change 11import { APIGatewayProxyEventQueryStringParameters , APIGatewayProxyHandler } from "aws-lambda" ;
22import { getLettersForSupplier } from "../services/letter-operations" ;
33import { createLetterRepository } from "../infrastructure/letter-repo-factory" ;
4- import { assertNotEmpty } from "../utils/validation" ;
4+ import { assertNotEmpty , lowerCaseKeys } from "../utils/validation" ;
55import { ApiErrorDetail } from '../contracts/errors' ;
66import { lambdaConfig } from "../config/lambda-config" ;
77import pino from 'pino' ;
@@ -27,8 +27,9 @@ export const getLetters: APIGatewayProxyHandler = async (event) => {
2727
2828 try {
2929 assertNotEmpty ( event . headers , new Error ( "The request headers are empty" ) ) ;
30- correlationId = assertNotEmpty ( event . headers [ lambdaConfig . APIM_CORRELATION_HEADER ] , new Error ( "The request headers don't contain the APIM correlation id" ) ) ;
31- const supplierId = assertNotEmpty ( event . headers [ lambdaConfig . SUPPLIER_ID_HEADER ] , new ValidationError ( ApiErrorDetail . InvalidRequestMissingSupplierId ) ) ;
30+ const lowerCasedHeaders = lowerCaseKeys ( event . headers ) ;
31+ correlationId = assertNotEmpty ( lowerCasedHeaders [ lambdaConfig . APIM_CORRELATION_HEADER ] , new Error ( "The request headers don't contain the APIM correlation id" ) ) ;
32+ const supplierId = assertNotEmpty ( lowerCasedHeaders [ lambdaConfig . SUPPLIER_ID_HEADER ] , new ValidationError ( ApiErrorDetail . InvalidRequestMissingSupplierId ) ) ;
3233 const limitNumber = getLimitOrDefault ( event . queryStringParameters , maxLimit ) ;
3334
3435 const letters = await getLettersForSupplier (
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { ApiErrorDetail } from '../contracts/errors';
66import { ValidationError } from '../errors' ;
77import { mapErrorToResponse } from '../mappers/error-mapper' ;
88import { lambdaConfig } from "../config/lambda-config" ;
9- import { assertNotEmpty } from '../utils/validation' ;
9+ import { assertNotEmpty , lowerCaseKeys } from '../utils/validation' ;
1010import { mapToLetterDto } from '../mappers/letter-mapper' ;
1111
1212const letterRepo = createLetterRepository ( ) ;
@@ -16,8 +16,9 @@ export const patchLetter: APIGatewayProxyHandler = async (event) => {
1616
1717 try {
1818 assertNotEmpty ( event . headers , new Error ( 'The request headers are empty' ) ) ;
19- correlationId = assertNotEmpty ( event . headers [ lambdaConfig . APIM_CORRELATION_HEADER ] , new Error ( "The request headers don't contain the APIM correlation id" ) ) ;
20- const supplierId = assertNotEmpty ( event . headers [ lambdaConfig . SUPPLIER_ID_HEADER ] , new ValidationError ( ApiErrorDetail . InvalidRequestMissingSupplierId ) ) ;
19+ const lowerCasedHeaders = lowerCaseKeys ( event . headers ) ;
20+ correlationId = assertNotEmpty ( lowerCasedHeaders [ lambdaConfig . APIM_CORRELATION_HEADER ] , new Error ( "The request headers don't contain the APIM correlation id" ) ) ;
21+ const supplierId = assertNotEmpty ( lowerCasedHeaders [ lambdaConfig . SUPPLIER_ID_HEADER ] , new ValidationError ( ApiErrorDetail . InvalidRequestMissingSupplierId ) ) ;
2122 const letterId = assertNotEmpty ( event . pathParameters ?. id , new ValidationError ( ApiErrorDetail . InvalidRequestMissingLetterIdPathParameter ) ) ;
2223 const body = assertNotEmpty ( event . body , new ValidationError ( ApiErrorDetail . InvalidRequestMissingBody ) ) ;
2324
Original file line number Diff line number Diff line change 1- import { assertNotEmpty } from "../validation" ;
1+ import { assertNotEmpty , lowerCaseKeys } from "../validation" ;
22
33describe ( "assertNotEmpty" , ( ) => {
44 const error = new Error ( ) ;
@@ -52,3 +52,16 @@ describe("assertNotEmpty", () => {
5252 expect ( result ) . toBe ( arr ) ;
5353 } ) ;
5454} ) ;
55+
56+ describe ( "lowerCaseKeys" , ( ) => {
57+ it ( "lowers case on header keys" , ( ) => {
58+ const headers : Record < string , number > = { 'Aa_Bb-Cc' :1 , 'b' :2 } ;
59+ const result = lowerCaseKeys ( headers ) ;
60+ expect ( result ) . toEqual ( { 'aa_bb-cc' :1 , 'b' :2 } ) ;
61+ } ) ;
62+
63+ it ( "handles empty input" , ( ) => {
64+ const result = lowerCaseKeys ( { } ) ;
65+ expect ( result ) . toEqual ( { } ) ;
66+ } ) ;
67+ } ) ;
Original file line number Diff line number Diff line change @@ -16,3 +16,7 @@ export function assertNotEmpty<T>(
1616
1717 return value ;
1818}
19+
20+ export function lowerCaseKeys ( obj : Record < string , any > ) : Record < string , any > {
21+ return Object . fromEntries ( Object . entries ( obj ) . map ( ( [ k , v ] ) => [ k . toLowerCase ( ) , v ] ) ) ;
22+ }
You can’t perform that action at this time.
0 commit comments