16
16
import { HttpRequest , HttpResponse } from "@aws-sdk/protocol-http" ;
17
17
import { HeaderBag , QueryParameterBag } from "@aws-sdk/types" ;
18
18
import {
19
+ APIGatewayProxyEvent ,
19
20
APIGatewayProxyEventHeaders ,
21
+ APIGatewayProxyEventMultiValueHeaders ,
22
+ APIGatewayProxyEventMultiValueQueryStringParameters ,
20
23
APIGatewayProxyEventQueryStringParameters ,
21
24
APIGatewayProxyEventV2 ,
25
+ APIGatewayProxyResult ,
22
26
APIGatewayProxyResultV2 ,
23
27
} from "aws-lambda" ;
24
28
import { Readable } from "stream" ;
25
29
26
- export function convertEvent ( event : APIGatewayProxyEventV2 ) : HttpRequest {
30
+ export function convertEvent ( event : APIGatewayProxyEvent ) : HttpRequest ;
31
+ export function convertEvent ( event : APIGatewayProxyEventV2 ) : HttpRequest ;
32
+
33
+ export function convertEvent ( event : APIGatewayProxyEvent | APIGatewayProxyEventV2 ) : HttpRequest {
34
+ if ( isV2Event ( event ) ) {
35
+ return convertV2Event ( event ) ;
36
+ } else {
37
+ return convertV1Event ( event ) ;
38
+ }
39
+ }
40
+
41
+ function convertV1Event ( event : APIGatewayProxyEvent ) : HttpRequest {
42
+ return new HttpRequest ( {
43
+ method : event . httpMethod ,
44
+ headers : convertMultiValueHeaders ( event . multiValueHeaders ) ,
45
+ query : convertMultiValueQueryStringParameters ( event . multiValueQueryStringParameters ) ,
46
+ path : event . path ,
47
+ ...( event . body ? { body : Readable . from ( Buffer . from ( event . body , event . isBase64Encoded ? "base64" : "utf8" ) ) } : { } ) ,
48
+ } ) ;
49
+ }
50
+
51
+ function convertV2Event ( event : APIGatewayProxyEventV2 ) : HttpRequest {
27
52
return new HttpRequest ( {
28
53
method : event . requestContext . http . method ,
29
54
headers : convertHeaders ( event . headers ) ,
@@ -33,6 +58,7 @@ export function convertEvent(event: APIGatewayProxyEventV2): HttpRequest {
33
58
} ) ;
34
59
}
35
60
61
+ export const convertVersion2Response = convertResponse ;
36
62
export function convertResponse ( response : HttpResponse ) : APIGatewayProxyResultV2 {
37
63
return {
38
64
statusCode : response . statusCode ,
@@ -42,14 +68,69 @@ export function convertResponse(response: HttpResponse): APIGatewayProxyResultV2
42
68
} ;
43
69
}
44
70
71
+ export function convertVersion1Response ( response : HttpResponse ) : APIGatewayProxyResult {
72
+ return {
73
+ statusCode : response . statusCode ,
74
+ multiValueHeaders : convertResponseHeaders ( response . headers ) ,
75
+ body : response . body ,
76
+ isBase64Encoded : false ,
77
+ } ;
78
+ }
79
+ function convertResponseHeaders ( headers : HeaderBag ) {
80
+ const retVal : { [ key : string ] : string [ ] } = { } ;
81
+ for ( const [ key , val ] of Object . entries ( headers ) ) {
82
+ retVal [ key ] = val . split ( "," ) . map ( ( v ) => v . trim ( ) ) ;
83
+ }
84
+ return retVal ;
85
+ }
86
+
87
+ function isV2Event ( event : APIGatewayProxyEvent | APIGatewayProxyEventV2 ) : event is APIGatewayProxyEventV2 {
88
+ return hasVersion ( event ) && event . version === "2.0" ;
89
+ }
90
+
91
+ function hasVersion ( event : any ) : event is Record < "version" , string > {
92
+ return event . hasOwnProperty ( "version" ) ;
93
+ }
94
+
95
+ function convertMultiValueHeaders ( multiValueHeaders : APIGatewayProxyEventMultiValueHeaders | null ) {
96
+ const retVal : { [ key : string ] : string } = { } ;
97
+
98
+ if ( multiValueHeaders === null ) {
99
+ return retVal ;
100
+ }
101
+
102
+ for ( const [ key , val ] of Object . entries ( multiValueHeaders ) ) {
103
+ if ( val !== undefined ) {
104
+ retVal [ key ] = val . join ( ", " ) ;
105
+ }
106
+ }
107
+
108
+ return retVal ;
109
+ }
110
+
45
111
// TODO: this can be rewritten with arrow functions / Object.fromEntries / filter
46
112
// but first we need to split up generated client and servers so we can have different
47
113
// language version targets.
48
114
function convertHeaders ( headers : APIGatewayProxyEventHeaders ) : HeaderBag {
49
115
const retVal : { [ key : string ] : string } = { } ;
50
116
51
- for ( const key in headers ) {
52
- const val = headers [ key ] ;
117
+ for ( const [ key , val ] of Object . entries ( headers ) ) {
118
+ if ( val !== undefined ) {
119
+ retVal [ key ] = val ;
120
+ }
121
+ }
122
+
123
+ return retVal ;
124
+ }
125
+
126
+ function convertMultiValueQueryStringParameters ( params : APIGatewayProxyEventMultiValueQueryStringParameters | null ) {
127
+ if ( params === undefined || params === null ) {
128
+ return undefined ;
129
+ }
130
+
131
+ const retVal : { [ key : string ] : string [ ] } = { } ;
132
+
133
+ for ( const [ key , val ] of Object . entries ( params ) ) {
53
134
if ( val !== undefined ) {
54
135
retVal [ key ] = val ;
55
136
}
@@ -66,8 +147,7 @@ function convertQuery(params: APIGatewayProxyEventQueryStringParameters | undefi
66
147
67
148
const retVal : { [ key : string ] : string | string [ ] } = { } ;
68
149
69
- for ( const key in params ) {
70
- const val = params [ key ] ;
150
+ for ( const [ key , val ] of Object . entries ( params ) ) {
71
151
if ( val !== undefined ) {
72
152
if ( val . indexOf ( "," ) !== - 1 ) {
73
153
retVal [ key ] = val ;
0 commit comments