11'use strict' ;
2-
2+ const NOT_FOUND = - 1 ;
33const _ = require ( 'lodash' ) ;
44
55module . exports = {
66 httpValidate ( ) {
77 const events = [ ] ;
8+ const corsPreflight = { } ;
89
910 _ . forEach ( this . getAllStateMachines ( ) , ( stateMachineName ) => {
1011 const stateMachineObj = this . getStateMachine ( stateMachineName ) ;
@@ -14,6 +15,20 @@ module.exports = {
1415 http . path = this . getHttpPath ( http , stateMachineName ) ;
1516 http . method = this . getHttpMethod ( http , stateMachineName ) ;
1617
18+ if ( http . cors ) {
19+ http . cors = this . getCors ( http ) ;
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+ corsPreflight [ http . path ] = cors ;
30+ }
31+
1732 events . push ( {
1833 stateMachineName,
1934 http,
@@ -24,6 +39,7 @@ module.exports = {
2439
2540 return {
2641 events,
42+ corsPreflight,
2743 } ;
2844 } ,
2945
@@ -85,4 +101,27 @@ module.exports = {
85101 ] . join ( '' ) ;
86102 throw new this . serverless . classes . Error ( errorMessage ) ;
87103 } ,
104+
105+ getCors ( http ) {
106+ const headers = [
107+ 'Content-Type' ,
108+ 'X-Amz-Date' ,
109+ 'Authorization' ,
110+ 'X-Api-Key' ,
111+ 'X-Amz-Security-Token' ,
112+ 'X-Amz-User-Agent' ,
113+ ] ;
114+
115+ const cors = {
116+ origins : [ '*' ] ,
117+ origin : '*' ,
118+ methods : [ 'OPTIONS' ] ,
119+ headers,
120+ allowCredentials : false ,
121+ } ;
122+
123+ cors . methods . push ( http . method . toUpperCase ( ) ) ;
124+
125+ return cors ;
126+ } ,
88127} ;
0 commit comments