@@ -9,33 +9,36 @@ module.exports = {
99 const corsPreflight = { }
1010 await BbPromise . all (
1111 this . getAllServiceProxies ( ) . map ( async ( serviceProxy ) => {
12- Object . keys ( serviceProxy ) . forEach ( async ( functionName ) => {
13- await this . checkAllowedService ( functionName )
14- const http = serviceProxy [ functionName ]
15- http . path = await this . getProxyPath ( serviceProxy [ functionName ] )
16- http . method = await this . getProxyMethod ( serviceProxy [ functionName ] )
17-
18- if ( serviceProxy [ functionName ] . cors ) {
19- http . cors = await this . getCors ( serviceProxy [ functionName ] )
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
32- }
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+
18+ if ( serviceProxy [ serviceName ] . cors ) {
19+ http . cors = await 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
32+ }
3333
34- corsPreflight [ http . path ] = cors
34+ if ( _ . has ( http . cors , 'cacheControl' ) ) {
35+ cors . cacheControl = http . cors . cacheControl
3536 }
3637
37- events . push ( { functionName, http } )
38- } )
38+ corsPreflight [ http . path ] = cors
39+ }
40+
41+ events . push ( { serviceName, http } )
3942 } )
4043 )
4144 return {
@@ -55,15 +58,20 @@ module.exports = {
5558 }
5659 } ,
5760
58- async getProxyPath ( proxy ) {
59- if ( typeof proxy . path === 'string' ) {
61+ async getProxyPath ( proxy , serviceName ) {
62+ if ( proxy . path && typeof proxy . path === 'string' ) {
6063 return proxy . path . replace ( / ^ \/ / , '' ) . replace ( / \/ $ / , '' )
6164 }
62- return BbPromise . reject ( new this . serverless . classes . Error ( 'Invalid service proxy syntax' ) )
65+
66+ return BbPromise . reject (
67+ new this . serverless . classes . Error (
68+ `Missing or invalid "path" property in ${ serviceName } proxy`
69+ )
70+ )
6371 } ,
6472
65- async getProxyMethod ( proxy ) {
66- if ( typeof proxy . method === 'string' ) {
73+ async getProxyMethod ( proxy , serviceName ) {
74+ if ( proxy . method && typeof proxy . method === 'string' ) {
6775 const method = proxy . method . toLowerCase ( )
6876
6977 const allowedMethods = [ 'get' , 'post' , 'put' , 'patch' , 'options' , 'head' , 'delete' , 'any' ]
@@ -76,7 +84,11 @@ module.exports = {
7684 }
7785 return method
7886 }
79- return BbPromise . reject ( new this . serverless . classes . Error ( 'Invalid service proxy syntax' ) )
87+ return BbPromise . reject (
88+ new this . serverless . classes . Error (
89+ `Missing or invalid "method" property in ${ serviceName } proxy`
90+ )
91+ )
8092 } ,
8193
8294 async getCors ( proxy ) {
@@ -130,6 +142,13 @@ module.exports = {
130142 if ( cors . methods . indexOf ( proxy . method . toUpperCase ( ) ) === NOT_FOUND ) {
131143 cors . methods . push ( proxy . method . toUpperCase ( ) )
132144 }
145+
146+ if ( _ . has ( cors , 'maxAge' ) ) {
147+ if ( ! _ . isInteger ( cors . maxAge ) || cors . maxAge < 1 ) {
148+ const errorMessage = 'maxAge should be an integer over 0'
149+ return BbPromise . reject ( new this . serverless . classes . Error ( errorMessage ) )
150+ }
151+ }
133152 } else {
134153 cors . methods . push ( proxy . method . toUpperCase ( ) )
135154 }
0 commit comments