|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const BbPromise = require('bluebird') |
| 4 | +const _ = require('lodash') |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + async compileMethodsToSqs() { |
| 8 | + this.apiGatewayMethodLogicalIds = [] |
| 9 | + this.validated.events.forEach(async (event) => { |
| 10 | + if (event.functionName == 'sqs') { |
| 11 | + const resourceId = this.getResourceId(event.http.path) |
| 12 | + const resourceName = this.getResourceName(event.http.path) |
| 13 | + |
| 14 | + const template = { |
| 15 | + Type: 'AWS::ApiGateway::Method', |
| 16 | + Properties: { |
| 17 | + HttpMethod: event.http.method.toUpperCase(), |
| 18 | + RequestParameters: {}, |
| 19 | + AuthorizationType: 'NONE', |
| 20 | + ApiKeyRequired: Boolean(event.http.private), |
| 21 | + ResourceId: resourceId, |
| 22 | + RestApiId: this.provider.getApiGatewayRestApiId() |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + _.merge( |
| 27 | + template, |
| 28 | + await this.getSqsMethodIntegration(event.http), |
| 29 | + await this.getMethodResponses(event.http) |
| 30 | + ) |
| 31 | + |
| 32 | + const methodLogicalId = this.provider.naming.getMethodLogicalId( |
| 33 | + resourceName, |
| 34 | + event.http.method |
| 35 | + ) |
| 36 | + |
| 37 | + this.apiGatewayMethodLogicalIds.push(methodLogicalId) |
| 38 | + |
| 39 | + _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { |
| 40 | + [methodLogicalId]: template |
| 41 | + }) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + return BbPromise.resolve() |
| 46 | + }, |
| 47 | + |
| 48 | + async getSqsMethodIntegration(http) { |
| 49 | + let queueName = http.queueName |
| 50 | + if (typeof http.queueName == 'string') { |
| 51 | + queueName = `"${queueName}"` |
| 52 | + } |
| 53 | + const integration = { |
| 54 | + IntegrationHttpMethod: 'POST', |
| 55 | + Type: 'AWS', |
| 56 | + Credentials: { |
| 57 | + 'Fn::GetAtt': ['ApigatewayToSqsRole', 'Arn'] |
| 58 | + }, |
| 59 | + Uri: { |
| 60 | + 'Fn::Join': [ |
| 61 | + '', |
| 62 | + [ |
| 63 | + 'arn:aws:apigateway:', |
| 64 | + { |
| 65 | + Ref: 'AWS::Region' |
| 66 | + }, |
| 67 | + ':sqs:path//', |
| 68 | + { |
| 69 | + Ref: 'AWS::AccountId' |
| 70 | + }, |
| 71 | + '/', |
| 72 | + queueName |
| 73 | + ] |
| 74 | + ] |
| 75 | + }, |
| 76 | + RequestParameters: { |
| 77 | + 'integration.request.querystring.Action': "'SendMessage'", |
| 78 | + 'integration.request.querystring.MessageBody': 'method.request.body.message' |
| 79 | + }, |
| 80 | + RequestTemplates: { 'application/json': '{statusCode:200}' } |
| 81 | + } |
| 82 | + |
| 83 | + const integrationResponse = { |
| 84 | + IntegrationResponses: [ |
| 85 | + { |
| 86 | + StatusCode: 200, |
| 87 | + SelectionPattern: 200, |
| 88 | + ResponseParameters: {}, |
| 89 | + ResponseTemplates: {} |
| 90 | + }, |
| 91 | + { |
| 92 | + StatusCode: 400, |
| 93 | + SelectionPattern: 400, |
| 94 | + ResponseParameters: {}, |
| 95 | + ResponseTemplates: {} |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + |
| 100 | + if (http && http.cors) { |
| 101 | + let origin = http.cors.origin |
| 102 | + if (http.cors.origins && http.cors.origins.length) { |
| 103 | + origin = http.cors.origins.join(',') |
| 104 | + } |
| 105 | + |
| 106 | + integrationResponse.IntegrationResponses.forEach(async (val, i) => { |
| 107 | + integrationResponse.IntegrationResponses[i].ResponseParameters = { |
| 108 | + 'method.response.header.Access-Control-Allow-Origin': `'${origin}'` |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + _.merge(integration, integrationResponse) |
| 114 | + |
| 115 | + return { |
| 116 | + Properties: { |
| 117 | + Integration: integration |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments