forked from ryanmurakami/serverless-apigwy-binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (90 loc) · 3.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const BbPromise = require('bluebird')
class ApiGwyBinaryPlugin {
constructor (serverless, options) {
this.serverless = serverless
this.options = options
this.provider = this.serverless.getProvider('aws')
this.hooks = {
'after:aws:deploy:deploy:updateStack': this.configureApiGwy.bind(this)
}
serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', {
properties: {
contentHandling: { type: 'string' },
},
});
}
configureApiGwy () {
return BbPromise.bind(this)
.then(this.getFunctionsForContentHandling)
.then(this.setContentHandling)
}
setContentHandling (funcs) {
if (!Object.keys(funcs).length) {
return
}
const apiName = this.provider.naming.getApiGatewayName()
const apigateway = new this.provider.sdk.APIGateway({
region: this.options.region
})
const integrationResponse = {
statusCode: '200'
}
return apigateway
.getRestApis()
.promise()
.then((apis) => {
integrationResponse.restApiId = apis.items.find(api => api.name === apiName).id
return apigateway
.getResources({ restApiId: integrationResponse.restApiId })
.promise()
})
.then((resources) => {
const integrationPromises = []
Object.keys(funcs).forEach((fKey) => {
funcs[fKey].events.forEach((e) => {
if (e.http && e.http.contentHandling) {
integrationResponse.httpMethod = e.http.method.toUpperCase()
integrationResponse.contentHandling = e.http.contentHandling
integrationResponse.resourceId = resources.items.find(
r => r.path === `/${e.http.path}`).id
integrationPromises
.push(apigateway.putIntegrationResponse(integrationResponse).promise())
}
})
})
this.serverless.cli.log('Setting up content handling in AWS API Gateway (takes ~1 min)...')
return BbPromise.all(integrationPromises)
})
// AWS Limit createDeployment: 3 requests per minute per account
// 'Too Many Requests', error may occur as serverless calls this endpoint also.
// Wait 1 minute to get reliable deployment.
// http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html
.then(() => new BbPromise(resolve => setTimeout(() => resolve(), 60000)))
.then(() => {
this.serverless.cli.log('Deploying content handling updates to AWS API Gateway...')
return apigateway.createDeployment({
stageName: this.options.stage,
restApiId: integrationResponse.restApiId
}).promise()
})
.then((result) => {
if (result.id) {
this.serverless.cli.log('AWS API Gateway Deployed')
}
})
.catch(err => this.serverless.cli.log(err.message))
}
getFunctionsForContentHandling () {
const funcs = this.serverless.service.functions
const validFuncs = {}
Object.keys(funcs).forEach((fKey) => {
funcs[fKey].events.forEach((e) => {
if (e.http && e.http.contentHandling) {
validFuncs[fKey] = funcs[fKey]
}
})
})
return validFuncs
}
}
module.exports = ApiGwyBinaryPlugin