Skip to content

Commit e0f4a34

Browse files
committed
refactor
1 parent 069dfb9 commit e0f4a34

File tree

6 files changed

+69
-12
lines changed

6 files changed

+69
-12
lines changed

lib/apiGateway/methods.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
module.exports = {
4-
getMethodResponses(http) {
4+
async getMethodResponses(http) {
55
const methodResponse = {
66
Properties: {
77
MethodResponses: [
@@ -25,7 +25,7 @@ module.exports = {
2525
origin = http.cors.origins.join(',')
2626
}
2727

28-
methodResponse.Properties.MethodResponses.forEach((val, i) => {
28+
methodResponse.Properties.MethodResponses.forEach(async (val, i) => {
2929
methodResponse.Properties.MethodResponses[i].ResponseParameters = {
3030
'method.response.header.Access-Control-Allow-Origin': `'${origin}'`
3131
}

lib/apiGateway/validate.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@ const BbPromise = require('bluebird')
44
const _ = require('lodash')
55

66
module.exports = {
7-
async serviceProxyValidate() {
7+
async validateServiceProxies() {
88
const events = []
99
await BbPromise.all(
1010
this.getAllServiceProxies().map(async (serviceProxy) => {
1111
Object.keys(serviceProxy).forEach(async (functionName) => {
1212
await this.checkAllowedService(functionName)
1313
const corsPreflight = []
14-
const http = {
15-
path: await this.getProxyPath(serviceProxy[functionName]),
16-
method: await this.getProxyMethod(serviceProxy[functionName]),
17-
streamName: serviceProxy[functionName].streamName
18-
}
14+
const http = serviceProxy[functionName]
15+
http.path = await this.getProxyPath(serviceProxy[functionName])
16+
http.method = await this.getProxyMethod(serviceProxy[functionName])
1917

2018
if (serviceProxy[functionName].cors) {
2119
http.cors = await this.getCors(serviceProxy[functionName])

lib/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const compileDeployment = require('serverless/lib/plugins/aws/package/compile/ev
1212
const getStackInfo = require('serverless/lib/plugins/aws/info/getStackInfo')
1313
const compileMethodsToKinesis = require('./package/kinesis/compileMethodsToKinesis')
1414
const compileIamRoleToKinesis = require('./package/kinesis/compileIamRoleToKinesis')
15+
const validateKinesisServiceProxy = require('./package/kinesis/validateKinesisServiceProxy')
16+
const compileKinesisServiceProxy = require('./package/kinesis/compileKinesisServiceProxy')
1517

1618
class ServerlessApigatewayServiceProxy {
1719
constructor(serverless, options) {
@@ -28,6 +30,8 @@ class ServerlessApigatewayServiceProxy {
2830
compileMethodsToKinesis,
2931
compileIamRoleToKinesis,
3032
compileDeployment,
33+
validateKinesisServiceProxy,
34+
compileKinesisServiceProxy,
3135
getStackInfo,
3236
validate,
3337
methods,
@@ -37,14 +41,15 @@ class ServerlessApigatewayServiceProxy {
3741
this.hooks = {
3842
'package:compileEvents': async () => {
3943
if (this.getAllServiceProxies().length > 0) {
40-
this.validated = await this.serviceProxyValidate()
44+
this.validated = await this.validateServiceProxies()
4145
await this.compileRestApi()
4246
await this.compileResources()
4347
if (await this.existsDeployment()) {
4448
await this.compileDeployment()
4549
}
46-
await this.compileIamRoleToKinesis()
47-
await this.compileMethodsToKinesis()
50+
51+
//Kinesis proxy
52+
await this.compileKinesisServiceProxy()
4853
}
4954
},
5055
'after:deploy:deploy': async () => {

lib/package/kinesis/compileIamRoleToKinesis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
2-
const BbPromise = require('bluebird')
32
const _ = require('lodash')
3+
const BbPromise = require('bluebird')
44

55
module.exports = {
66
async compileIamRoleToKinesis() {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
module.exports = {
3+
async compileKinesisServiceProxy() {
4+
await this.validateKinesisServiceProxy()
5+
await this.compileIamRoleToKinesis()
6+
await this.compileMethodsToKinesis()
7+
}
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
const BbPromise = require('bluebird')
4+
const _ = require('lodash')
5+
6+
module.exports = {
7+
async validateKinesisServiceProxy() {
8+
await BbPromise.all(
9+
this.validated.events.map(async (serviceProxy) => {
10+
if (serviceProxy.functionName == 'kinesis') {
11+
if (!_.has(serviceProxy.http, 'streamName')) {
12+
return BbPromise.reject(
13+
new this.serverless.classes.Error(
14+
'Missing "streamName" property in Kinesis Service Proxy'
15+
)
16+
)
17+
}
18+
19+
if (
20+
typeof serviceProxy.http.streamName != 'object' &&
21+
typeof serviceProxy.http.streamName != 'string'
22+
) {
23+
const errorMessage = [
24+
'You can only set "string" or the AWS intrinsic function',
25+
' "Ref" like { Ref: \'KinesisStreamResourceId\' }}',
26+
' as a streamName property'
27+
].join('')
28+
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
29+
}
30+
31+
if (
32+
typeof serviceProxy.http.streamName == 'object' &&
33+
!_.has(serviceProxy.http.streamName, 'Ref')
34+
) {
35+
const errorMessage = [
36+
'You can only set "string" or the AWS intrinsic function',
37+
' "Ref" like { Ref: \'KinesisStreamResourceId\' }}',
38+
' as a streamName property'
39+
].join('')
40+
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
41+
}
42+
}
43+
})
44+
)
45+
}
46+
}

0 commit comments

Comments
 (0)