@@ -4,79 +4,14 @@ import { HttpRequest, HttpRequestOptions } from "servie-http";
44import { createBody } from "servie/dist/body/node" ;
55import { errorhandler } from "servie-errorhandler" ;
66import { finalhandler } from "servie-finalhandler" ;
7- import { mask } from "bit-string-mask" ;
7+ import {
8+ APIGatewayEvent as Event ,
9+ APIGatewayProxyHandler as Handler ,
10+ APIGatewayProxyResult as Result ,
11+ Context
12+ } from "aws-lambda" ;
813
9- /**
10- * AWS Lambda event object.
11- */
12- export interface Event {
13- path : string ;
14- httpMethod : string ;
15- body : string | null ;
16- isBase64Encoded ?: boolean ;
17- resource : string ;
18- headers : {
19- [ key : string ] : string ;
20- } | null ;
21- queryStringParameters : {
22- [ key : string ] : string ;
23- } | null ;
24- pathParameters : {
25- [ key : string ] : string ;
26- } | null ;
27- requestContext : {
28- identity : {
29- sourceIp : string ;
30- } ;
31- } ;
32- }
33-
34- /**
35- * AWS lambda context object.
36- *
37- * Reference: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
38- */
39- export interface Context {
40- getRemainingTimeInMillis : ( ) => number ;
41- callbackWaitsForEmptyEventLoop : boolean ;
42- functionName : string ;
43- functionVersion : string ;
44- invokedFunctionArn : string ;
45- memoryLimitInMB : string ;
46- awsRequestId : string ;
47- logGroupName : string ;
48- logStreamName : string ;
49- identity : { cognitoIdentityId : string ; cognitoIdentityPoolId : string } | null ;
50- clientContext : {
51- client : {
52- installation_id : string ;
53- app_title : string ;
54- app_version_name : string ;
55- app_version_code : string ;
56- app_package_name : string ;
57- } ;
58- Custom : any ;
59- env : {
60- platform_version : string ;
61- platform : string ;
62- make : string ;
63- model : string ;
64- locale : string ;
65- } ;
66- } | null ;
67- }
68-
69- /**
70- * Standard lambda HTTP response.
71- */
72- export interface Result {
73- body ?: string ;
74- statusCode ?: number ;
75- headers ?: {
76- [ key : string ] : string | string [ ] ;
77- } ;
78- isBase64Encoded ?: boolean ;
79- }
14+ export { Handler , Event , Context , Result } ;
8015
8116/**
8217 * Extends HTTP requests with AWS lambda context.
@@ -117,19 +52,15 @@ export interface Options {
11752/**
11853 * Create a server for handling AWS Lambda requests.
11954 */
120- export function createHandler ( app : App , options : Options = { } ) {
121- return function (
122- event : Event ,
123- context : Context ,
124- cb : ( err : Error | null , res : Result ) => void
125- ) : void {
55+ export function createHandler ( app : App , options : Options = { } ) : Handler {
56+ return function ( event , context , callback ) : void {
12657 const { httpMethod : method } = event ;
12758 const url = format ( {
12859 pathname : event . path ,
129- query : event . queryStringParameters
60+ query : event . multiValueQueryStringParameters
13061 } ) ;
13162 const isBinary = options . isBinary || ( ( ) => false ) ;
132- const headers = createHeaders ( event . headers ) ;
63+ const headers = createHeaders ( event . multiValueHeaders ) ;
13364 const rawBody = event . body
13465 ? Buffer . from ( event . body , event . isBase64Encoded ? "base64" : "utf8" )
13566 : undefined ;
@@ -165,7 +96,7 @@ export function createHandler(app: App, options: Options = {}) {
16596 . arrayBuffer ( )
16697 . then ( buffer => {
16798 const { statusCode } = res ;
168- const headers = toHeaders ( res . allHeaders ) ;
99+ const multiValueHeaders = toMultiValueHeaders ( res . allHeaders ) ;
169100 const isBase64Encoded = isBinary ( res ) ;
170101 const body = Buffer . from ( buffer ) . toString (
171102 isBase64Encoded ? "base64" : "utf8"
@@ -177,7 +108,12 @@ export function createHandler(app: App, options: Options = {}) {
177108 res . finished = true ;
178109 res . bytesTransferred = buffer ? buffer . byteLength : 0 ;
179110
180- return cb ( null , { statusCode, headers, body, isBase64Encoded } ) ;
111+ return callback ( null , {
112+ statusCode,
113+ multiValueHeaders,
114+ body,
115+ isBase64Encoded
116+ } ) ;
181117 } )
182118 . catch ( err => sendResponse ( mapError ( err ) ) ) ;
183119 }
@@ -203,21 +139,8 @@ export function createHandler(app: App, options: Options = {}) {
203139/**
204140 * Return a lambda compatible object of headers.
205141 */
206- function toHeaders ( headers : Headers ) {
207- const result = Object . create ( null ) ;
208- const obj = headers . asObject ( ) ;
209-
210- for ( const key of Object . keys ( obj ) ) {
211- const val = obj [ key ] ;
212-
213- if ( Array . isArray ( val ) ) {
214- for ( let i = 0 ; i < val . length ; i ++ ) {
215- result [ mask ( key , i ) ] = val [ i ] ;
216- }
217- } else {
218- result [ key ] = val ;
219- }
220- }
221-
142+ function toMultiValueHeaders ( headers : Headers ) {
143+ const result : NonNullable < Result [ "multiValueHeaders" ] > = Object . create ( null ) ;
144+ for ( const key of headers . keys ( ) ) result [ key ] = headers . getAll ( key ) ;
222145 return result ;
223146}
0 commit comments