import { IHttpResponse , Middleware } from './types' ; import { APIGatewayProxyEvent , APIGatewayProxyEventV2 , Context , } from 'aws-lambda' ; import { HttpError , isHttpError , isHttpResponse , buildResponseObject , } from './httpResponse' ; export async function funcQueueExecutor < Shared , ResponseDataType > ( { event , context , middlewares , } ) { let returnValue = { } ; let shared = { } as Shared ; for ( let middleware of middlewares ) { const result = await middleware ( { event , context , shared , } ) ; if ( result != null ) { returnValue = result ; break ; } } return returnValue ; } export const createTraceInfo = ( event , context ) => { if ( event . version === '2.0' ) { const typedEvent = event as APIGatewayProxyEventV2 ; return { endpoint : typedEvent . requestContext . domainName , routeKey : typedEvent . requestContext . routeKey , requestBody : typedEvent . body || '' , requestMethod : typedEvent . requestContext . http . method , requestId : event . requestContext . requestId , } ; } else { const typedEvent = event as APIGatewayProxyEvent ; return { endpoint : typedEvent . requestContext . domainName ?? '' + typedEvent . requestContext . path , requestBody : typedEvent . body || '' , requestMethod : typedEvent . requestContext . httpMethod , country : typedEvent . headers [ 'CloudFront-Viewer-Country' ] ?? '' , lambdaRequestId : context . awsRequestId , logStreamName : context . logStreamName , logGroupName : context . logGroupName , apiGatewayId : typedEvent . requestContext . requestId , } ; } } ; export const addTraceInfoToResponseBody = ( responseBody , event , context ) : { response : any ; debug : ReturnType < typeof createTraceInfo > ; } | ( object & { debug : ReturnType < typeof createTraceInfo > ; } ) => { const traceInfo = createTraceInfo ( event , context ) ; if ( typeof responseBody === 'string' || typeof responseBody === 'number' || typeof responseBody === 'boolean' || Array . isArray ( responseBody ) ) { return { response : responseBody , debug : traceInfo , } ; } return { ... responseBody , debug : traceInfo , } ; } ; export const logRequestInfo = ( event , context ) => { console . log ( 'Aws-Api-Gateway-Request-Id: ' , event . requestContext . requestId ) ; console . log ( 'EVENT: ' , event ) ; console . log ( 'CONTEXT: ' , context ) ; } ; export const transformResponseToHttpResponse = ( response , shouldAddErrorStatusCode ) : IHttpResponse => { let result = response ; if ( isHttpError ( response ) ) { result = response . toHttpResponse ( ) ; } else if ( ! isHttpResponse ( response ) ) { result = buildResponseObject ( { statusCode : shouldAddErrorStatusCode ? 400 : 200 , body : response , } ) ; } const isStatusCodeSet = typeof response . statusCode === 'number' ; if ( isStatusCodeSet ) { result . statusCode = response . statusCode as number ; } return result as IHttpResponse ; } ;