@@ -7,6 +7,13 @@ const NO_MATCHING_ACTION = request => {
7
7
}
8
8
} ;
9
9
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
+
10
17
function process ( proxyIntegrationConfig , event ) {
11
18
//validate config
12
19
if ( ! Array . isArray ( proxyIntegrationConfig . routes ) || proxyIntegrationConfig . routes . length < 1 ) {
@@ -20,9 +27,7 @@ function process(proxyIntegrationConfig, event) {
20
27
21
28
let headers = { } ;
22
29
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 ) ;
26
31
if ( event . httpMethod === 'OPTIONS' ) {
27
32
return Promise . resolve ( { statusCode : 200 , headers : headers , body : '' } ) ;
28
33
}
@@ -46,11 +51,19 @@ function process(proxyIntegrationConfig, event) {
46
51
try {
47
52
event . body = JSON . parse ( event . body ) ;
48
53
} 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
+ } ) ;
50
59
}
51
60
}
52
61
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
+ } ;
54
67
} ) . catch ( err => {
55
68
return convertError ( err , errorMapping , headers ) ;
56
69
} ) ;
@@ -64,7 +77,7 @@ function normalizeRequestPath(event) {
64
77
// ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
65
78
// if host is from amazonaws.com, then event.path is just '/resource-path':
66
79
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 ) ) {
68
81
// remove first path element:
69
82
const groups = / \/ [ ^ \/ ] + ( .* ) / . exec ( event . path ) || [ null , null ] ;
70
83
return groups [ 1 ] || '/' ;
@@ -74,15 +87,27 @@ function normalizeRequestPath(event) {
74
87
}
75
88
76
89
function 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
+ } ;
81
106
}
82
107
83
108
function findMatchingActionConfig ( httpMethod , httpPath , routeConfig ) {
84
109
const paths = { } ;
85
- const matchingMethodRoutes = routeConfig . routes . filter ( route => route . method == httpMethod ) ;
110
+ const matchingMethodRoutes = routeConfig . routes . filter ( route => route . method === httpMethod ) ;
86
111
for ( let route of matchingMethodRoutes ) {
87
112
if ( routeConfig . debug ) {
88
113
console . log ( `Examining route ${ route . path } to match ${ httpPath } ` ) ;
0 commit comments