Skip to content

Commit da2959c

Browse files
committed
refactor(apig-validation): remove unnecessary usage of async await
1 parent dea5692 commit da2959c

File tree

2 files changed

+233
-226
lines changed

2 files changed

+233
-226
lines changed

lib/apiGateway/validate.js

Lines changed: 62 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,74 @@
11
'use strict'
22
const NOT_FOUND = -1
3-
const BbPromise = require('bluebird')
43
const _ = require('lodash')
54

65
module.exports = {
7-
async validateServiceProxies() {
8-
const events = []
6+
validateServiceProxies() {
97
const corsPreflight = {}
10-
await BbPromise.all(
11-
this.getAllServiceProxies().map(async (serviceProxy) => {
12-
const serviceName = this.getServiceName(serviceProxy)
13-
await this.checkAllowedService(serviceName)
14-
const http = serviceProxy[serviceName]
15-
http.path = await this.getProxyPath(serviceProxy[serviceName], serviceName)
16-
http.method = await this.getProxyMethod(serviceProxy[serviceName], serviceName)
17-
http.auth = await this.getAuth(serviceProxy[serviceName], serviceName)
18-
19-
await this.validateRequestParameters(serviceProxy[serviceName], serviceName)
20-
21-
if (serviceProxy[serviceName].cors) {
22-
http.cors = await this.getCors(serviceProxy[serviceName])
23-
24-
const cors = corsPreflight[http.path] || {}
25-
26-
cors.headers = _.union(http.cors.headers, cors.headers)
27-
cors.methods = _.union(http.cors.methods, cors.methods)
28-
cors.origins = _.union(http.cors.origins, cors.origins)
29-
cors.origin = http.cors.origin || '*'
30-
cors.allowCredentials = cors.allowCredentials || http.cors.allowCredentials
31-
32-
// when merging, last one defined wins
33-
if (_.has(http.cors, 'maxAge')) {
34-
cors.maxAge = http.cors.maxAge
35-
}
36-
37-
if (_.has(http.cors, 'cacheControl')) {
38-
cors.cacheControl = http.cors.cacheControl
39-
}
40-
41-
corsPreflight[http.path] = cors
8+
const events = this.getAllServiceProxies().map((serviceProxy) => {
9+
const serviceName = this.getServiceName(serviceProxy)
10+
this.checkAllowedService(serviceName)
11+
const http = serviceProxy[serviceName]
12+
http.path = this.getProxyPath(serviceProxy[serviceName], serviceName)
13+
http.method = this.getProxyMethod(serviceProxy[serviceName], serviceName)
14+
http.auth = this.getAuth(serviceProxy[serviceName], serviceName)
15+
16+
this.validateRequestParameters(serviceProxy[serviceName], serviceName)
17+
18+
if (serviceProxy[serviceName].cors) {
19+
http.cors = this.getCors(serviceProxy[serviceName])
20+
21+
const cors = corsPreflight[http.path] || {}
22+
23+
cors.headers = _.union(http.cors.headers, cors.headers)
24+
cors.methods = _.union(http.cors.methods, cors.methods)
25+
cors.origins = _.union(http.cors.origins, cors.origins)
26+
cors.origin = http.cors.origin || '*'
27+
cors.allowCredentials = cors.allowCredentials || http.cors.allowCredentials
28+
29+
// when merging, last one defined wins
30+
if (_.has(http.cors, 'maxAge')) {
31+
cors.maxAge = http.cors.maxAge
4232
}
4333

44-
events.push({ serviceName, http })
45-
})
46-
)
34+
if (_.has(http.cors, 'cacheControl')) {
35+
cors.cacheControl = http.cors.cacheControl
36+
}
37+
38+
corsPreflight[http.path] = cors
39+
}
40+
41+
return { serviceName, http }
42+
})
43+
4744
return {
4845
events,
4946
corsPreflight
5047
}
5148
},
5249

53-
async checkAllowedService(serviceName) {
50+
checkAllowedService(serviceName) {
5451
const allowedProxies = ['kinesis', 'sqs', 's3', 'sns']
5552
if (allowedProxies.indexOf(serviceName) === NOT_FOUND) {
5653
const errorMessage = [
5754
`Invalid APIG proxy "${serviceName}".`,
5855
` This plugin supported Proxies are: ${allowedProxies.join(', ')}.`
5956
].join('')
60-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
57+
throw new this.serverless.classes.Error(errorMessage)
6158
}
6259
},
6360

64-
async getProxyPath(proxy, serviceName) {
61+
getProxyPath(proxy, serviceName) {
6562
if (proxy.path && _.isString(proxy.path)) {
6663
return proxy.path.replace(/^\//, '').replace(/\/$/, '')
6764
}
6865

69-
return BbPromise.reject(
70-
new this.serverless.classes.Error(
71-
`Missing or invalid "path" property in ${serviceName} proxy`
72-
)
66+
throw new this.serverless.classes.Error(
67+
`Missing or invalid "path" property in ${serviceName} proxy`
7368
)
7469
},
7570

76-
async getProxyMethod(proxy, serviceName) {
71+
getProxyMethod(proxy, serviceName) {
7772
if (proxy.method && _.isString(proxy.method)) {
7873
const method = proxy.method.toLowerCase()
7974

@@ -83,18 +78,17 @@ module.exports = {
8378
`Invalid APIG method "${proxy.method}" in AWS service proxy.`,
8479
` AWS supported methods are: ${allowedMethods.join(', ')}.`
8580
].join('')
86-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
81+
throw new this.serverless.classes.Error(errorMessage)
8782
}
8883
return method
8984
}
90-
return BbPromise.reject(
91-
new this.serverless.classes.Error(
92-
`Missing or invalid "method" property in ${serviceName} proxy`
93-
)
85+
86+
throw new this.serverless.classes.Error(
87+
`Missing or invalid "method" property in ${serviceName} proxy`
9488
)
9589
},
9690

97-
async getCors(proxy) {
91+
getCors(proxy) {
9892
const headers = [
9993
'Content-Type',
10094
'X-Amz-Date',
@@ -123,7 +117,7 @@ module.exports = {
123117
' but not both at the same time to configure CORS.',
124118
' Please check the docs for more info.'
125119
].join('')
126-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
120+
throw new this.serverless.classes.Error(errorMessage)
127121
}
128122

129123
if (cors.headers) {
@@ -132,7 +126,7 @@ module.exports = {
132126
'CORS header values must be provided as an array.',
133127
' Please check the docs for more info.'
134128
].join('')
135-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
129+
throw new this.serverless.classes.Error(errorMessage)
136130
}
137131
} else {
138132
cors.headers = headers
@@ -149,7 +143,7 @@ module.exports = {
149143
if (_.has(cors, 'maxAge')) {
150144
if (!_.isInteger(cors.maxAge) || cors.maxAge < 1) {
151145
const errorMessage = 'maxAge should be an integer over 0'
152-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
146+
throw new this.serverless.classes.Error(errorMessage)
153147
}
154148
}
155149
} else {
@@ -159,7 +153,7 @@ module.exports = {
159153
return cors
160154
},
161155

162-
async getAuth(proxy, serviceName) {
156+
getAuth(proxy, serviceName) {
163157
const auth = {
164158
authorizationType: 'NONE'
165159
}
@@ -172,23 +166,21 @@ module.exports = {
172166
`Invalid APIG authorization type "${proxy.authorizationType}" in AWS service proxy.`,
173167
` AWS supported types are: ${allowedTypes.join(', ')}.`
174168
].join('')
175-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
169+
throw new this.serverless.classes.Error(errorMessage)
176170
}
177171

178172
auth.authorizationType = proxy.authorizationType
179173
} else {
180-
return BbPromise.reject(
181-
new this.serverless.classes.Error(
182-
`Invalid "authorizationType" property in ${serviceName} proxy`
183-
)
174+
throw new this.serverless.classes.Error(
175+
`Invalid "authorizationType" property in ${serviceName} proxy`
184176
)
185177
}
186178
}
187179

188180
if (!_.isUndefined(proxy.authorizerId)) {
189181
if (auth.authorizationType !== 'CUSTOM') {
190182
const errorMessage = `Expecting 'CUSTOM' authorization type when 'authorizerId' is set in service ${serviceName}`
191-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
183+
throw new this.serverless.classes.Error(errorMessage)
192184
}
193185

194186
auth.authorizerId = proxy.authorizerId
@@ -198,40 +190,34 @@ module.exports = {
198190
if (_.isArray(proxy.authorizationScopes)) {
199191
if (auth.authorizationType !== 'COGNITO_USER_POOLS') {
200192
const errorMessage = `Expecting 'COGNITO_USER_POOLS' authorization type when 'authorizationScopes' is set in service ${serviceName}`
201-
return BbPromise.reject(new this.serverless.classes.Error(errorMessage))
193+
throw new this.serverless.classes.Error(errorMessage)
202194
}
203195

204196
auth.authorizationScopes = proxy.authorizationScopes
205197
} else {
206-
return BbPromise.reject(
207-
new this.serverless.classes.Error(
208-
`Invalid "authorizationScopes" property in ${serviceName} proxy`
209-
)
198+
throw new this.serverless.classes.Error(
199+
`Invalid "authorizationScopes" property in ${serviceName} proxy`
210200
)
211201
}
212202
}
213203

214204
return auth
215205
},
216206

217-
async validateRequestParameters(proxy, serviceName) {
207+
validateRequestParameters(proxy, serviceName) {
218208
if (!_.isUndefined(proxy.requestParameters)) {
219209
if (serviceName !== 'sqs') {
220-
return BbPromise.reject(
221-
new this.serverless.classes.Error(
222-
'requestParameters property is only valid for "sqs" service proxy'
223-
)
210+
throw new this.serverless.classes.Error(
211+
'requestParameters property is only valid for "sqs" service proxy'
224212
)
225213
}
226214

227215
if (
228216
!_.isPlainObject(proxy.requestParameters) ||
229217
_.some(_.values(proxy.requestParameters), (v) => !_.isString(v))
230218
) {
231-
return BbPromise.reject(
232-
new this.serverless.classes.Error(
233-
'requestParameters property must be a string to string mapping'
234-
)
219+
throw new this.serverless.classes.Error(
220+
'requestParameters property must be a string to string mapping'
235221
)
236222
}
237223
}

0 commit comments

Comments
 (0)