Skip to content

Commit 24535ba

Browse files
committed
feat(eventbridge): add detail configuration
1 parent 9808b0d commit 24535ba

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/package/eventbridge/compileMethodsToEventBridge.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ module.exports = {
122122
return `$input.params().querystring.${http.source.queryStringParam}`
123123
}
124124

125+
if (http.source.bodyParam) {
126+
return `$util.parseJson($input.body).${http.source.bodyParam}`
127+
}
128+
125129
return `${http.source}`
126130
},
127131

@@ -134,23 +138,40 @@ module.exports = {
134138
return `$input.params().path.${http.detailType.pathParam}`
135139
}
136140

137-
if (http.source.queryStringParam) {
141+
if (http.detailType.queryStringParam) {
138142
return `$input.params().querystring.${http.detailType.queryStringParam}`
139143
}
140144

145+
if (http.detailType.bodyParam) {
146+
return `$util.parseJson($input.body).${http.detailType.bodyParam}`
147+
}
148+
141149
return `${http.detailType}`
142150
},
143151

152+
getEventBridgeDetail(http) {
153+
if (!_.has(http, 'detail')) {
154+
return '$util.escapeJavaScript($input.body)'
155+
}
156+
157+
if (http.detail.bodyParam) {
158+
return `$util.escapeJavaScript($util.parseJson($input.body).${http.detail.bodyParam})`
159+
}
160+
161+
return '$util.escapeJavaScript($input.body)'
162+
},
163+
144164
buildDefaultEventBridgeRequestTemplate(http) {
145165
const sourceParam = this.getEventBridgeSource(http)
146166
const detailTypeParam = this.getEventBridgeDetailType(http)
167+
const detailParam = this.getEventBridgeDetail(http)
147168

148169
return {
149170
'Fn::Sub': [
150171
'{"Entries":[{"Detail": "${Detail}","DetailType": "${DetailType}","EventBusName": "${EventBusName}","Source": "${Source}"}]}',
151172
{
152173
EventBusName: http.eventBusName,
153-
Detail: '$util.escapeJavaScript($input.body)',
174+
Detail: `${detailParam}`,
154175
DetailType: `${detailTypeParam}`,
155176
Source: `${sourceParam}`
156177
}

lib/package/eventbridge/compileMethodsToEventBridge.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,4 +901,33 @@ describe('#compileMethodsToEventBridge()', () => {
901901
.ApiGatewayMethodeventbridgePost.Properties.RequestParameters
902902
).to.be.deep.equal({ 'method.request.header.Custom-Header': true })
903903
})
904+
905+
it('should set detail when detail is provided as bodyParam', () => {
906+
const httpWithoutRequestTemplate = {
907+
path: 'foo',
908+
method: 'post',
909+
eventBusName: 'myEventBus',
910+
detailType: 'myDetailType',
911+
source: 'myEventSource',
912+
detail: {
913+
bodyParam: 'data.detail'
914+
}
915+
}
916+
917+
const resource = serverlessApigatewayServiceProxy.getEventBridgeMethodIntegration(
918+
httpWithoutRequestTemplate
919+
)
920+
921+
expect(
922+
resource.Properties.Integration.RequestTemplates['application/json']['Fn::Sub']
923+
).to.be.deep.equal([
924+
'{"Entries":[{"Detail": "${Detail}","DetailType": "${DetailType}","EventBusName": "${EventBusName}","Source": "${Source}"}]}',
925+
{
926+
EventBusName: 'myEventBus',
927+
Detail: '$util.escapeJavaScript($util.parseJson($input.body).data.detail)',
928+
DetailType: 'myDetailType',
929+
Source: 'myEventSource'
930+
}
931+
])
932+
})
904933
})

0 commit comments

Comments
 (0)