@@ -9,44 +9,47 @@ import { createMockServer } from './mockServer';
99
1010export function createMockV1Server (
1111 handler : Handler < APIGatewayProxyEvent , APIGatewayProxyResult > ,
12+ shouldBase64Encode : boolean ,
1213) {
13- return createMockServer ( handler , v1EventFromRequest ) ;
14+ return createMockServer ( handler , v1EventFromRequest ( shouldBase64Encode ) ) ;
1415}
1516
16- function v1EventFromRequest (
17- req : IncomingMessage ,
18- body : string ,
19- ) : APIGatewayProxyEvent {
20- const urlObject = url . parse ( req . url || '' , false ) ;
21- const searchParams = new URLSearchParams ( urlObject . search ?? '' ) ;
17+ function v1EventFromRequest ( shouldBase64Encode : boolean ) {
18+ return function ( req : IncomingMessage , body : string ) : APIGatewayProxyEvent {
19+ const urlObject = url . parse ( req . url || '' , false ) ;
20+ const searchParams = new URLSearchParams ( urlObject . search ?? '' ) ;
2221
23- const multiValueQueryStringParameters : Record < string , string [ ] > = { } ;
24- for ( const [ key ] of searchParams . entries ( ) ) {
25- const all = searchParams . getAll ( key ) ;
26- if ( all . length > 1 ) {
27- multiValueQueryStringParameters [ key ] = all ;
22+ const multiValueQueryStringParameters : Record < string , string [ ] > = { } ;
23+ for ( const [ key ] of searchParams . entries ( ) ) {
24+ const all = searchParams . getAll ( key ) ;
25+ if ( all . length > 1 ) {
26+ multiValueQueryStringParameters [ key ] = all ;
27+ }
2828 }
29- }
3029
31- // simplify the V1 event down to what our integration actually cares about
32- const event : Partial < APIGatewayProxyEvent > = {
33- // @ts -expect-error (version actually can exist on v1 events, this seems to be a typing error)
34- version : '1.0' ,
35- httpMethod : req . method ! ,
36- headers : Object . fromEntries (
37- Object . entries ( req . headers ) . map ( ( [ name , value ] ) => {
38- if ( Array . isArray ( value ) ) {
39- return [ name , value . join ( ',' ) ] ;
40- } else {
41- return [ name , value ] ;
42- }
43- } ) ,
44- ) ,
45- queryStringParameters : Object . fromEntries ( searchParams . entries ( ) ) ,
46- body,
47- multiValueQueryStringParameters,
48- multiValueHeaders : { } ,
49- } ;
30+ // simplify the V1 event down to what our integration actually cares about
31+ const event : Partial < APIGatewayProxyEvent > = {
32+ // @ts -expect-error (version actually can exist on v1 events, this seems to be a typing error)
33+ version : '1.0' ,
34+ httpMethod : req . method ! ,
35+ headers : Object . fromEntries (
36+ Object . entries ( req . headers ) . map ( ( [ name , value ] ) => {
37+ if ( Array . isArray ( value ) ) {
38+ return [ name , value . join ( ',' ) ] ;
39+ } else {
40+ return [ name , value ] ;
41+ }
42+ } ) ,
43+ ) ,
44+ queryStringParameters : Object . fromEntries ( searchParams . entries ( ) ) ,
45+ body : shouldBase64Encode
46+ ? Buffer . from ( body , 'utf8' ) . toString ( 'base64' )
47+ : body ,
48+ isBase64Encoded : shouldBase64Encode ,
49+ multiValueQueryStringParameters,
50+ multiValueHeaders : { } ,
51+ } ;
5052
51- return event as APIGatewayProxyEvent ;
53+ return event as APIGatewayProxyEvent ;
54+ } ;
5255}
0 commit comments