1
- import {
1
+ import type {
2
2
APIGatewayProxyEventV2 , APIGatewayProxyStructuredResultV2 ,
3
3
APIGatewayProxyEventQueryStringParameters , APIGatewayProxyEventPathParameters ,
4
+ APIGatewayProxyEventV2WithLambdaAuthorizer ,
4
5
Context
5
6
} from 'aws-lambda'
6
7
@@ -30,7 +31,9 @@ const lambdaRunner = async (axiosConfig: AxiosRequestConfig, operation: Operatio
30
31
. then ( ( resp ) => convertApiGwToAxios ( resp , axiosConfig ) )
31
32
}
32
33
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 > => {
34
37
// extract path params
35
38
// eg: for path template /v1/users/{id} & path url /v1/users/1108 -> will extract {'id': '1108'}
36
39
const template = operation . path
@@ -61,7 +64,15 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
61
64
headers [ key ] = val . toString ( )
62
65
}
63
66
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 > = {
65
76
version : '2.0' ,
66
77
routeKey : '$default' ,
67
78
rawPath : config . url ,
@@ -75,7 +86,7 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
75
86
authorizer : {
76
87
lambda : {
77
88
'lambda-invoke' : true ,
78
- callerIdentity : crtLambdaContext ?. invokedFunctionArn ?? 'lambda-invoke-not-specified'
89
+ callerIdentity : sourceIdentity
79
90
}
80
91
} ,
81
92
domainName : 'lambda-invoke' ,
@@ -85,7 +96,7 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
85
96
sourceIp : '' ,
86
97
path : config . url ,
87
98
protocol : 'HTTP/1.1' ,
88
- userAgent : 'lambda-invoke'
99
+ userAgent : headers [ 'user-agent' ] ?? headers [ 'User-Agent' ]
89
100
} ,
90
101
requestId : crtLambdaContext ?. awsRequestId ?? `lambda-invoke-${ uuidv4 ( ) } ` ,
91
102
routeKey : '$default' ,
@@ -94,9 +105,11 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera
94
105
timeEpoch : Date . now ( )
95
106
} ,
96
107
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 } )
100
113
101
114
debug ( 'lambdaRequest %o' , lambdaPayload )
102
115
0 commit comments