Skip to content

Commit 11c0c98

Browse files
Add custom templates support
1 parent 8d4120e commit 11c0c98

File tree

2 files changed

+100
-23
lines changed

2 files changed

+100
-23
lines changed

lib/deploy/events/apiGateway/methods.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,33 @@ module.exports = {
109109
},
110110

111111
getIntegrationRequestTemplates(stateMachineName, customName, http) {
112+
const defaultRequestTemplates = this.getDefaultRequestTemplates(stateMachineName, customName);
113+
return Object.assign(
114+
defaultRequestTemplates,
115+
_.get(http, ['request', 'template'])
116+
);
117+
},
118+
119+
getDefaultRequestTemplates(stateMachineName, customName) {
112120
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, customName);
113121
return {
114-
'application/json': {
115-
'Fn::Join': [
116-
'', [
117-
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n",
118-
'{"input": "$body","name": "$context.requestId","stateMachineArn":"',
119-
{
120-
Ref: `${stateMachineLogicalId}`,
121-
},
122-
'"}',
123-
],
124-
],
125-
},
126-
'application/x-www-form-urlencoded': {
127-
'Fn::Join': [
128-
'', [
129-
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n",
130-
'{"input": "$body","name": "$context.requestId","stateMachineArn":"',
131-
{
132-
Ref: `${stateMachineLogicalId}`,
133-
},
134-
'"}',
135-
],
122+
'application/json': this.buildDefaultRequestTemplate(stateMachineLogicalId),
123+
'application/x-www-form-urlencoded': this.buildDefaultRequestTemplate(stateMachineLogicalId)
124+
}
125+
},
126+
127+
buildDefaultRequestTemplate(stateMachineLogicalId) {
128+
return {
129+
'Fn::Join': [
130+
'', [
131+
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n",
132+
'{"input": "$body","name": "$context.requestId","stateMachineArn":"',
133+
{
134+
Ref: `${stateMachineLogicalId}`,
135+
},
136+
'"}',
136137
],
137-
},
138+
],
138139
}
139140
},
140141

lib/deploy/events/apiGateway/methods.test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('#methods()', () => {
8282
.to.have.property('Integration');
8383
});
8484

85+
// Remove these 2 specs
8586
it('should set stateMachinelogical ID to RequestTemplates when customName is not set', () => {
8687
expect(serverlessStepFunctions.getMethodIntegration('stateMachine').Properties
8788
.Integration.RequestTemplates['application/json']['Fn::Join'][1][2].Ref)
@@ -115,6 +116,81 @@ describe('#methods()', () => {
115116
});
116117
});
117118

119+
describe('#getIntegrationRequestTemplates()', () => {
120+
it('should set stateMachinelogical ID in default templates when customName is not set', () => {
121+
expect(serverlessStepFunctions.getIntegrationRequestTemplates('stateMachine')
122+
['application/json']['Fn::Join'][1][2].Ref)
123+
.to.be.equal('StateMachineStepFunctionsStateMachine');
124+
});
125+
126+
it('should set custom stateMachinelogical ID in default templates when customName is set',
127+
() => {
128+
expect(serverlessStepFunctions.getIntegrationRequestTemplates('stateMachine', 'custom')
129+
['application/json']['Fn::Join'][1][2].Ref)
130+
.to.be.equal('Custom');
131+
});
132+
133+
it('should return the default template for application/json when one is not given', () => {
134+
const http_without_request_template = {
135+
path: 'foo/bar1',
136+
method: 'post',
137+
request: {
138+
template: {
139+
'application/x-www-form-urlencoded': 'custom template'
140+
}
141+
}
142+
}
143+
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', undefined, http_without_request_template)
144+
.Properties.Integration.RequestTemplates['application/json']['Fn::Join'][1][2].Ref)
145+
.to.be.equal('StateMachineStepFunctionsStateMachine');
146+
});
147+
148+
it('should return a custom template for application/json when one is given', () => {
149+
const http_with_request_template = {
150+
path: 'foo/bar1',
151+
method: 'post',
152+
request: {
153+
template: {
154+
'application/json': 'custom template'
155+
}
156+
}
157+
}
158+
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', undefined, http_with_request_template)
159+
.Properties.Integration.RequestTemplates['application/json'])
160+
.to.be.equal('custom template');
161+
});
162+
163+
it('should return the default template for application/x-www-form-urlencoded when one is not given', () => {
164+
const http_without_request_template = {
165+
path: 'foo/bar1',
166+
method: 'post',
167+
request: {
168+
template: {
169+
'application/json': 'custom template'
170+
}
171+
}
172+
}
173+
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', undefined, http_without_request_template)
174+
.Properties.Integration.RequestTemplates['application/x-www-form-urlencoded']['Fn::Join'][1][2].Ref)
175+
.to.be.equal('StateMachineStepFunctionsStateMachine');
176+
});
177+
178+
it('should return a custom template for application/x-www-form-urlencoded when one is given', () => {
179+
const http_with_request_template = {
180+
path: 'foo/bar1',
181+
method: 'post',
182+
request: {
183+
template: {
184+
'application/x-www-form-urlencoded': 'custom template'
185+
}
186+
}
187+
}
188+
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', undefined, http_with_request_template)
189+
.Properties.Integration.RequestTemplates['application/x-www-form-urlencoded'])
190+
.to.be.equal('custom template');
191+
});
192+
});
193+
118194
describe('#getMethodResponses()', () => {
119195
it('should return a corresponding methodResponses resource', () => {
120196
expect(serverlessStepFunctions.getMethodResponses().Properties)

0 commit comments

Comments
 (0)