Skip to content

Commit

Permalink
fix: address some diffs with lambda_proxy
Browse files Browse the repository at this point in the history
1) body is object not string; 2) ordering of props; 3)
queryStringParameters return {} instead of null; 4) pathParameters
return {} instead of null; 5) stageVariables return {} instead of null;
6) no requestContext.domainName and requestContext.domainPrefix
  • Loading branch information
theburningmonk committed May 8, 2019
1 parent e18eff3 commit c9669ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
62 changes: 43 additions & 19 deletions lib/deploy/events/apiGateway/methods.js
Expand Up @@ -6,45 +6,69 @@ const awsArnRegExs = require('../../../utils/arnRegularExpressions');

const LAMBDA_PROXY_REQUEST_TEMPLATE = `
#define( $loop )
{
{
#foreach($key in $map.keySet())
#set( $k = $util.escapeJavaScript($key) )
#set( $v = $util.escapeJavaScript($map.get($key)).replaceAll("\\\\'", "'") )
"$k":
#set( $k = $util.escapeJavaScript($key) )
#set( $v = $util.escapeJavaScript($map.get($key)).replaceAll("\\\\'", "'") )
"$k":
#if ("$v" == "")
null
#else
"$v"
#if( $foreach.hasNext ) , #end
#end
#if( $foreach.hasNext ) , #end
#end
}
}
#end
#define( $smInput )
{
"body": $body,
"httpMethod": "$context.httpMethod",
"path": "$context.path",
"resource": "$context.resourcePath",
"path": "$context.path",
"httpMethod": "$context.httpMethod",
#set( $map = $input.params().header )
"headers": $loop,
#if ($input.params().querystring.size() == 0)
"queryStringParameters": null,
#else
#set( $map = $input.params().querystring )
"queryStringParameters": $loop,
#end
#if ($input.params().path.size() == 0)
"pathParameters": null,
#else
#set( $map = $input.params().path )
"pathParameters": $loop,
#end
#if ($stageVariables.size() == 0)
"stageVariables": null,
#else
#set( $map = $stageVariables )
"stageVariables": $loop,
#end
"requestContext": {
"accountId": "\${AccountId}",
"resourceId": "$context.resourceId",
"stage": "$context.stage",
"requestId": "$context.requestId",
"resourcePath": "$context.resourcePath",
"httpMethod": "$context.httpMethod",
"extendedRequestId": "$context.extendedRequestId",
"requestTime": "$context.requestTime",
"path": "$context.path",
"accountId": "\${AccountId}",
"protocol": "$context.protocol",
"stage": "$context.stage",
"domainPrefix": "$context.domainPrefix",
"requestTimeEpoch": "$context.requestTimeEpoch",
"requestId": "$context.requestId",
#set( $map = $context.identity )
"identity": $loop,
"path": "$context.path",
"resourcePath": "$context.resourcePath",
"httpMethod": "$context.httpMethod",
"protocol": "$context.protocol",
"domainName": "$context.domainName",
"apiId": "$context.apiId"
},
#set( $map = $stageVariables )
"stageVariables": $loop
"body": "$util.escapeJavaScript("$body")",
"isBase64Encoded": false
}
#end
{
Expand All @@ -54,7 +78,7 @@ const LAMBDA_PROXY_REQUEST_TEMPLATE = `
}`;

const LAMBDA_PROXY_JSON_REQUEST_TEMPLATE = `
#set( $body = $input.json('$') )
#set( $body = $input.body )
${LAMBDA_PROXY_REQUEST_TEMPLATE}`;

const LAMBDA_PROXY_FORM_URL_ENCODED_REQUEST_TEMPLATE = `
Expand Down
2 changes: 1 addition & 1 deletion lib/deploy/events/apiGateway/methods.test.js
Expand Up @@ -239,7 +239,7 @@ describe('#methods()', () => {
.getMethodIntegration('stateMachine', undefined, httpWithRequestTemplate)
.Properties.Integration.RequestTemplates;
expect(requestTemplates['application/json']['Fn::Sub'][0])
.to.contain("#set( $body = $input.json('$') )");
.to.contain('#set( $body = $input.body )');
expect(requestTemplates['application/json']['Fn::Sub'][1])
.to.be.deep.equal({
StateMachineArn: { Ref: 'StateMachineStepFunctionsStateMachine' },
Expand Down

0 comments on commit c9669ab

Please sign in to comment.