1- import {
1+ import type {
22 APIGatewayProxyEventV2 , APIGatewayProxyStructuredResultV2 ,
33 APIGatewayProxyEventQueryStringParameters , APIGatewayProxyEventPathParameters ,
4+ APIGatewayProxyEventV2WithLambdaAuthorizer ,
45 Context
56} from 'aws-lambda'
67
@@ -30,7 +31,9 @@ const lambdaRunner = async (axiosConfig: AxiosRequestConfig, operation: Operatio
3031 . then ( ( resp ) => convertApiGwToAxios ( resp , axiosConfig ) )
3132}
3233
33- export const convertAxiosToApiGw = ( config : AxiosRequestConfig , operation : Operation , crtLambdaContext ?: Context ) : APIGatewayProxyEventV2 => {
34+ export interface LambdaRunnerAuthContext { 'lambda-invoke' : true , callerIdentity : string }
35+
36+ export const convertAxiosToApiGw = ( config : AxiosRequestConfig , operation : Operation , crtLambdaContext ?: Context ) : APIGatewayProxyEventV2WithLambdaAuthorizer < LambdaRunnerAuthContext > => {
3437 // extract path params
3538 // eg: for path template /v1/users/{id} & path url /v1/users/1108 -> will extract {'id': '1108'}
3639 const template = operation . path
@@ -61,7 +64,15 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
6164 headers [ key ] = val . toString ( )
6265 }
6366
64- const lambdaPayload = {
67+ // identify caller lambda
68+ const sourceIdentity = [ 'lambda-invoke' , crtLambdaContext ?. invokedFunctionArn ] . filter ( Boolean ) . join ( '-' )
69+
70+ // default to lambda-invoke user-agent
71+ if ( ! headers [ 'User-Agent' ] && ! headers [ 'user-agent' ] ) {
72+ headers [ 'User-Agent' ] = sourceIdentity
73+ }
74+
75+ const lambdaPayload : APIGatewayProxyEventV2WithLambdaAuthorizer < LambdaRunnerAuthContext > = {
6576 version : '2.0' ,
6677 routeKey : '$default' ,
6778 rawPath : config . url ,
@@ -75,7 +86,7 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
7586 authorizer : {
7687 lambda : {
7788 'lambda-invoke' : true ,
78- callerIdentity : crtLambdaContext ?. invokedFunctionArn ?? 'lambda-invoke-not-specified'
89+ callerIdentity : sourceIdentity
7990 }
8091 } ,
8192 domainName : 'lambda-invoke' ,
@@ -85,7 +96,7 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
8596 sourceIp : '' ,
8697 path : config . url ,
8798 protocol : 'HTTP/1.1' ,
88- userAgent : 'lambda-invoke'
99+ userAgent : headers [ 'user-agent' ] ?? headers [ 'User-Agent' ]
89100 } ,
90101 requestId : crtLambdaContext ?. awsRequestId ?? `lambda-invoke-${ uuidv4 ( ) } ` ,
91102 routeKey : '$default' ,
@@ -94,9 +105,11 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
94105 timeEpoch : Date . now ( )
95106 } ,
96107 body : config . data ? JSON . stringify ( config . data ) : '' ,
97- isBase64Encoded : false ,
98- httpMethod : config . method
99- } as APIGatewayProxyEventV2
108+ isBase64Encoded : false
109+ }
110+
111+ // for backwards compat with older event format
112+ Object . assign ( lambdaPayload , { httpMethod : config . method } )
100113
101114 debug ( 'lambdaRequest %o' , lambdaPayload )
102115
0 commit comments