Skip to content

Commit f73109e

Browse files
committed
add request template
1 parent 88ad2bc commit f73109e

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
'use strict';
2+
3+
const BbPromise = require('bluebird');
4+
const _ = require('lodash');
5+
6+
module.exports = {
7+
8+
compileMethods() {
9+
this.apiGatewayMethodLogicalIds = [];
10+
11+
this.pluginhttpValidated.events.forEach((event) => {
12+
const resourceId = this.getResourceId(event.http.path);
13+
const resourceName = this.getResourceName(event.http.path);
14+
const requestParameters = (event.http.request && event.http.request.parameters) || {};
15+
16+
const template = {
17+
Type: 'AWS::ApiGateway::Method',
18+
Properties: {
19+
HttpMethod: event.http.method.toUpperCase(),
20+
RequestParameters: requestParameters,
21+
ResourceId: resourceId,
22+
RestApiId: { Ref: this.apiGatewayRestApiLogicalId },
23+
},
24+
};
25+
26+
if (event.http.private) {
27+
template.Properties.ApiKeyRequired = true;
28+
}
29+
30+
_.merge(template,
31+
this.getMethodIntegration(event.http, event.stateMachineName)
32+
// this.getMethodResponses(event.http)
33+
);
34+
35+
const methodLogicalId = this.provider.naming
36+
.getMethodLogicalId(resourceName, event.http.method);
37+
38+
this.apiGatewayMethodLogicalIds.push(methodLogicalId);
39+
40+
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
41+
[methodLogicalId]: template,
42+
});
43+
});
44+
45+
return BbPromise.resolve();
46+
},
47+
48+
getMethodIntegration(http, stateMachineName) {
49+
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName);
50+
const integration = {
51+
IntegrationHttpMethod: 'POST',
52+
Type: 'AWS',
53+
Credentials: {
54+
'Fn::GetAtt': [
55+
'APIAssumeRolePolicyDocument',
56+
'Arn',
57+
],
58+
},
59+
Uri: {
60+
'Fn::Join': [
61+
'',
62+
[
63+
'arn:aws:apigateway:',
64+
{
65+
Ref: 'AWS::Region',
66+
},
67+
':states:action/StartExecution',
68+
],
69+
],
70+
},
71+
PassthroughBehavior: 'NEVER',
72+
RequestTemplates: {
73+
'application/json': {
74+
'Fn::Join': [
75+
'', [
76+
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n",
77+
'{"input": "$body","name": "$context.requestId","stateMachineArn":"',
78+
{
79+
Ref: `${stateMachineLogicalId}`,
80+
},
81+
'"}',
82+
],
83+
],
84+
},
85+
},
86+
IntegrationResponses: [
87+
{
88+
StatusCode: 200,
89+
SelectionPattern: '',
90+
ResponseParameters: {},
91+
ResponseTemplates: {},
92+
},
93+
{
94+
StatusCode: 400,
95+
SelectionPattern: '[\\s\\S]*\\[400\\][\\s\\S]*',
96+
ResponseParameters: {},
97+
ResponseTemplates: {},
98+
},
99+
],
100+
};
101+
102+
return {
103+
Properties: {
104+
Integration: integration,
105+
},
106+
};
107+
},
108+
};

lib/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const compileActivities = require('./deploy/stepFunctions/compileActivities');
66
const compileIamRole = require('./deploy/stepFunctions/compileIamRole');
77
const httpValidate = require('./deploy/events/apiGateway/validate');
88
const httpResources = require('./deploy/events/apiGateway/resources');
9+
const httpMethods = require('./deploy/events/apiGateway/methods');
910
const coreCompileRestApi =
1011
require('serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/restApi');
1112
const yamlParser = require('./yamlParser');
@@ -39,6 +40,7 @@ class ServerlessStepFunctions {
3940
coreCompileRestApi,
4041
httpValidate,
4142
httpResources,
43+
httpMethods,
4244
yamlParser,
4345
naming
4446
);
@@ -90,15 +92,15 @@ class ServerlessStepFunctions {
9092
.then(this.compileStateMachines)
9193
.then(this.compileActivities),
9294
'deploy:compileEvents': () => {
93-
// this.coreValidated = this.awsCompileApigEvents.validate();
9495
this.pluginhttpValidated = this.httpValidate();
9596

9697
if (this.pluginhttpValidated.events.length === 0) {
9798
return BbPromise.resolve();
9899
} else if (!this.isCoreHttpevents()) {
99100
return BbPromise.bind(this)
100101
.then(this.compileRestApi)
101-
.then(this.compileResources);
102+
.then(this.compileResources)
103+
.then(this.compileMethods);
102104
}
103105
return BbPromise.bind(this)
104106
.then(this.compileResources);

0 commit comments

Comments
 (0)