@@ -7,6 +7,13 @@ const NO_MATCHING_ACTION = request => {
77 }
88} ;
99
10+ function addCorsHeaders ( toAdd ) {
11+ toAdd [ "Access-Control-Allow-Origin" ] = "*" ;
12+ toAdd [ "Access-Control-Allow-Methods" ] = "GET,POST,PUT,DELETE,HEAD" ;
13+ toAdd [ "Access-Control-Allow-Headers" ] = "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token" ;
14+ return toAdd ;
15+ }
16+
1017function process ( proxyIntegrationConfig , event ) {
1118 //validate config
1219 if ( ! Array . isArray ( proxyIntegrationConfig . routes ) || proxyIntegrationConfig . routes . length < 1 ) {
@@ -20,9 +27,7 @@ function process(proxyIntegrationConfig, event) {
2027
2128 let headers = { } ;
2229 if ( proxyIntegrationConfig . cors ) {
23- headers [ "Access-Control-Allow-Origin" ] = "*" ;
24- headers [ "Access-Control-Allow-Methods" ] = "GET,POST,PUT,DELETE,HEAD" ;
25- headers [ "Access-Control-Allow-Headers" ] = "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token" ;
30+ addCorsHeaders ( headers ) ;
2631 if ( event . httpMethod === 'OPTIONS' ) {
2732 return Promise . resolve ( { statusCode : 200 , headers : headers , body : '' } ) ;
2833 }
@@ -46,11 +51,19 @@ function process(proxyIntegrationConfig, event) {
4651 try {
4752 event . body = JSON . parse ( event . body ) ;
4853 } catch ( parseError ) {
49- return Promise . resolve ( { statusCode : 400 , headers : headers , body : 'body is not a valid JSON' } ) ;
54+ return Promise . resolve ( {
55+ statusCode : 400 ,
56+ headers : headers ,
57+ body : 'body is not a valid JSON'
58+ } ) ;
5059 }
5160 }
5261 return Promise . resolve ( actionConfig . action ( event ) ) . then ( res => {
53- return { statusCode : 200 , headers : headers , body : JSON . stringify ( res ) } ;
62+ return {
63+ statusCode : 200 ,
64+ headers : headers ,
65+ body : JSON . stringify ( res )
66+ } ;
5467 } ) . catch ( err => {
5568 return convertError ( err , errorMapping , headers ) ;
5669 } ) ;
@@ -64,7 +77,7 @@ function normalizeRequestPath(event) {
6477 // ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
6578 // if host is from amazonaws.com, then event.path is just '/resource-path':
6679 const apiId = event . requestContext ? event . requestContext . apiId : null ; // the apiId that is the first part of the amazonaws.com-host
67- if ( ( apiId && event . headers && event . headers . Host && event . headers . Host . substring ( 0 , apiId . length ) != apiId ) ) {
80+ if ( ( apiId && event . headers && event . headers . Host && event . headers . Host . substring ( 0 , apiId . length ) !== apiId ) ) {
6881 // remove first path element:
6982 const groups = / \/ [ ^ \/ ] + ( .* ) / . exec ( event . path ) || [ null , null ] ;
7083 return groups [ 1 ] || '/' ;
@@ -74,15 +87,27 @@ function normalizeRequestPath(event) {
7487}
7588
7689function convertError ( error , errorMapping , headers ) {
77- if ( typeof error . reason === 'string' && typeof error . message === 'string' && errorMapping && errorMapping [ error . reason ] ) {
78- return { statusCode : errorMapping [ error . reason ] , body : error . message , headers : headers } ;
79- }
80- return { statusCode : 500 , body : `Generic error: ${ JSON . stringify ( error ) } ` } ;
90+ if ( typeof error . reason === 'string' && errorMapping && errorMapping [ error . reason ] ) {
91+ return { statusCode : errorMapping [ error . reason ] , body : JSON . stringify ( error . message ) , headers : headers } ;
92+ } else if ( typeof error . status === 'number' ) {
93+ return { statusCode : error . status , body : JSON . stringify ( error . message ) , headers : addCorsHeaders ( { } ) } ;
94+ }
95+ try {
96+ return {
97+ statusCode : 500 ,
98+ body : `Generic error: ${ JSON . stringify ( error ) } ` ,
99+ headers : addCorsHeaders ( { } )
100+ } ;
101+ } catch ( stringifyError ) { }
102+ return {
103+ statusCode : 500 ,
104+ body : `Generic error: ${ error . stack ? error . stack : error } `
105+ } ;
81106}
82107
83108function findMatchingActionConfig ( httpMethod , httpPath , routeConfig ) {
84109 const paths = { } ;
85- const matchingMethodRoutes = routeConfig . routes . filter ( route => route . method == httpMethod ) ;
110+ const matchingMethodRoutes = routeConfig . routes . filter ( route => route . method === httpMethod ) ;
86111 for ( let route of matchingMethodRoutes ) {
87112 if ( routeConfig . debug ) {
88113 console . log ( `Examining route ${ route . path } to match ${ httpPath } ` ) ;
0 commit comments