Skip to content

Commit a2a4c14

Browse files
committed
refactor
1 parent e17f89a commit a2a4c14

File tree

5 files changed

+66
-57
lines changed

5 files changed

+66
-57
lines changed

lib/apiGateway/methods.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
module.exports = {
4+
getMethodResponses(http) {
5+
const methodResponse = {
6+
Properties: {
7+
MethodResponses: [
8+
{
9+
ResponseParameters: {},
10+
ResponseModels: {},
11+
StatusCode: 200
12+
},
13+
{
14+
ResponseParameters: {},
15+
ResponseModels: {},
16+
StatusCode: 400
17+
}
18+
]
19+
}
20+
}
21+
22+
if (http && http.cors) {
23+
let origin = http.cors.origin
24+
if (http.cors.origins && http.cors.origins.length) {
25+
origin = http.cors.origins.join(',')
26+
}
27+
28+
methodResponse.Properties.MethodResponses.forEach((val, i) => {
29+
methodResponse.Properties.MethodResponses[i].ResponseParameters = {
30+
'method.response.header.Access-Control-Allow-Origin': `'${origin}'`
31+
}
32+
})
33+
}
34+
35+
return methodResponse
36+
}
37+
}

lib/apiGateway/validate.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
}
1919

2020
if (serviceProxy[functionName].cors) {
21-
http.cors = this.getCors(serviceProxy[functionName])
21+
http.cors = await this.getCors(serviceProxy[functionName])
2222

2323
const cors = corsPreflight[http.path] || {}
2424

@@ -64,7 +64,7 @@ module.exports = {
6464
return BbPromise.reject(new this.serverless.classes.Error('Invalid service proxy syntax'))
6565
},
6666

67-
getProxyMethod(proxy) {
67+
async getProxyMethod(proxy) {
6868
if (typeof proxy.method === 'string') {
6969
const method = proxy.method.toLowerCase()
7070

@@ -74,14 +74,14 @@ module.exports = {
7474
`Invalid APIG method "${proxy.method}" in AWS service proxy.`,
7575
` AWS supported methods are: ${allowedMethods.join(', ')}.`
7676
].join('')
77-
throw new this.serverless.classes.Error(errorMessage)
77+
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
7878
}
7979
return method
8080
}
8181
return BbPromise.reject(new this.serverless.classes.Error('Invalid service proxy syntax'))
8282
},
8383

84-
getCors(proxy) {
84+
async getCors(proxy) {
8585
const headers = [
8686
'Content-Type',
8787
'X-Amz-Date',
@@ -110,7 +110,7 @@ module.exports = {
110110
' but not both at the same time to configure CORS.',
111111
' Please check the docs for more info.'
112112
].join('')
113-
throw new this.serverless.classes.Error(errorMessage)
113+
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
114114
}
115115

116116
if (cors.headers) {
@@ -119,7 +119,7 @@ module.exports = {
119119
'CORS header values must be provided as an array.',
120120
' Please check the docs for more info.'
121121
].join('')
122-
throw new this.serverless.classes.Error(errorMessage)
122+
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
123123
}
124124
} else {
125125
cors.headers = headers

lib/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ const _ = require('lodash')
55

66
const utils = require('./utils')
77
const validate = require('./apiGateway/validate')
8+
const methods = require('./apiGateway/methods')
89
const compileRestApi = require('serverless/lib/plugins/aws/package/compile/events/apiGateway/lib/restApi')
910
const compileResources = require('serverless/lib/plugins/aws/package/compile/events/apiGateway/lib/resources')
1011
const compileDeployment = require('serverless/lib/plugins/aws/package/compile/events/apiGateway/lib/deployment')
1112
const getStackInfo = require('serverless/lib/plugins/aws/info/getStackInfo')
12-
const compileKinesisMethods = require('./package/kinesis/methods')
13-
const compileToKinesisIamRole = require('./package/kinesis/compileToKinesisIamRole')
13+
const compileMethodsToKinesis = require('./package/kinesis/compileMethodsToKinesis')
14+
const compileIamRoleToKinesis = require('./package/kinesis/compileIamRoleToKinesis')
1415

1516
class ServerlessApigatewayServiceProxy {
1617
constructor(serverless, options) {
@@ -24,11 +25,12 @@ class ServerlessApigatewayServiceProxy {
2425
this,
2526
compileRestApi,
2627
compileResources,
27-
compileKinesisMethods,
28-
compileToKinesisIamRole,
28+
compileMethodsToKinesis,
29+
compileIamRoleToKinesis,
2930
compileDeployment,
3031
getStackInfo,
3132
validate,
33+
methods,
3234
utils
3335
)
3436

@@ -41,8 +43,8 @@ class ServerlessApigatewayServiceProxy {
4143
if (await this.existsDeployment()) {
4244
await this.compileDeployment()
4345
}
44-
await this.compileKinesisMethods()
45-
await this.compileToKinesisIamRole()
46+
await this.compileIamRoleToKinesis()
47+
await this.compileMethodsToKinesis()
4648
}
4749
},
4850
'after:deploy:deploy': async () => {

lib/package/kinesis/compileToKinesisIamRole.js renamed to lib/package/kinesis/compileIamRoleToKinesis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const BbPromise = require('bluebird')
33
const _ = require('lodash')
44

55
module.exports = {
6-
async compileToKinesisIamRole() {
6+
async compileIamRoleToKinesis() {
77
await BbPromise.all(
88
this.getAllServiceProxies().map(async (serviceProxy) => {
99
Object.keys(serviceProxy).forEach(async (serviceName) => {

lib/package/kinesis/methods.js renamed to lib/package/kinesis/compileMethodsToKinesis.js

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

66
module.exports = {
7-
async compileKinesisMethods() {
7+
async compileMethodsToKinesis() {
88
this.apiGatewayMethodLogicalIds = []
99
this.validated.events.forEach((event) => {
1010
const resourceId = this.getResourceId(event.http.path)
@@ -22,7 +22,11 @@ module.exports = {
2222
}
2323
}
2424

25-
_.merge(template, this.getMethodIntegration(event.http), this.getMethodResponses(event.http))
25+
_.merge(
26+
template,
27+
this.getKinesisMethodIntegration(event.http),
28+
this.getMethodResponses(event.http)
29+
)
2630

2731
const methodLogicalId = this.provider.naming.getMethodLogicalId(
2832
resourceName,
@@ -39,7 +43,7 @@ module.exports = {
3943
return BbPromise.resolve()
4044
},
4145

42-
getMethodIntegration(http) {
46+
getKinesisMethodIntegration(http) {
4347
const integration = {
4448
IntegrationHttpMethod: 'POST',
4549
Type: 'AWS',
@@ -59,7 +63,7 @@ module.exports = {
5963
]
6064
},
6165
PassthroughBehavior: 'NEVER',
62-
RequestTemplates: this.getIntegrationRequestTemplates(http)
66+
RequestTemplates: this.getKinesisIntegrationRequestTemplates(http)
6367
}
6468

6569
const integrationResponse = {
@@ -101,19 +105,19 @@ module.exports = {
101105
}
102106
},
103107

104-
getIntegrationRequestTemplates(http) {
105-
const defaultRequestTemplates = this.getDefaultRequestTemplates(http)
108+
getKinesisIntegrationRequestTemplates(http) {
109+
const defaultRequestTemplates = this.getDefaultKinesisRequestTemplates(http)
106110
return Object.assign(defaultRequestTemplates, _.get(http, ['request', 'template']))
107111
},
108112

109-
getDefaultRequestTemplates(http) {
113+
getDefaultKinesisRequestTemplates(http) {
110114
return {
111-
'application/json': this.buildDefaultRequestTemplate(http),
112-
'application/x-www-form-urlencoded': this.buildDefaultRequestTemplate(http)
115+
'application/json': this.buildDefaultKinesisRequestTemplate(http),
116+
'application/x-www-form-urlencoded': this.buildDefaultKinesisRequestTemplate(http)
113117
}
114118
},
115119

116-
buildDefaultRequestTemplate(http) {
120+
buildDefaultKinesisRequestTemplate(http) {
117121
let streamName
118122
if (typeof http.streamName == 'object') {
119123
streamName = http.streamName
@@ -135,39 +139,5 @@ module.exports = {
135139
]
136140
]
137141
}
138-
},
139-
140-
getMethodResponses(http) {
141-
const methodResponse = {
142-
Properties: {
143-
MethodResponses: [
144-
{
145-
ResponseParameters: {},
146-
ResponseModels: {},
147-
StatusCode: 200
148-
},
149-
{
150-
ResponseParameters: {},
151-
ResponseModels: {},
152-
StatusCode: 400
153-
}
154-
]
155-
}
156-
}
157-
158-
if (http && http.cors) {
159-
let origin = http.cors.origin
160-
if (http.cors.origins && http.cors.origins.length) {
161-
origin = http.cors.origins.join(',')
162-
}
163-
164-
methodResponse.Properties.MethodResponses.forEach((val, i) => {
165-
methodResponse.Properties.MethodResponses[i].ResponseParameters = {
166-
'method.response.header.Access-Control-Allow-Origin': `'${origin}'`
167-
}
168-
})
169-
}
170-
171-
return methodResponse
172142
}
173143
}

0 commit comments

Comments
 (0)