|
| 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 | +}; |
0 commit comments