@@ -7,9 +7,14 @@ const NO_MATCHING_ACTION = request => {
77 }
88} ;
99
10- function route ( proxyIntegrationConfig , event ) {
10+ function process ( proxyIntegrationConfig , event ) {
11+ //validate config
12+ if ( ! Array . isArray ( proxyIntegrationConfig . routes ) || proxyIntegrationConfig . routes . length < 1 ) {
13+ throw new Error ( 'proxyIntegration.routes must not be empty' ) ;
14+ }
15+
1116 // detect if it's an http-call at all:
12- if ( ! event . httpMethod ) {
17+ if ( ! event . httpMethod || ! event . path ) {
1318 return null ;
1419 }
1520 const headers = Object . assign ( {
@@ -18,19 +23,12 @@ function route(proxyIntegrationConfig, event) {
1823 if ( proxyIntegrationConfig . cors ) {
1924 headers [ "Access-Control-Allow-Origin" ] = "*" ;
2025 }
26+
2127 // assure necessary values have sane defaults:
22- event . path = event . path || '' ;
2328 const errorMapping = proxyIntegrationConfig . errorMapping || { } ;
2429 errorMapping [ 'NO_MATCHING_ACTION' ] = 404 ;
25- proxyIntegrationConfig . routes = proxyIntegrationConfig . routes || [ ] ;
26- // ugly hack: if host is from 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
27- // if host is from amazonaws.com, then event.path is just '/resource-path':
28- const apiId = event . requestContext ? event . requestContext . apiId : null ; // the apiId that is the first part of the amazonaws.com-host
29- if ( ( apiId && event . headers && event . headers . Host && event . headers . Host . substring ( 0 , apiId . length ) != apiId ) ) {
30- // remove first path element:
31- const groups = / \/ [ ^ \/ ] + ( .* ) / . exec ( event . path ) || [ null , null ] ;
32- event . path = groups [ 1 ] || '/' ;
33- }
30+
31+ event . path = normalizeRequestPath ( event ) ;
3432
3533 try {
3634 const actionConfig = findMatchingActionConfig ( event . httpMethod , event . path , proxyIntegrationConfig ) || { action : NO_MATCHING_ACTION } ;
@@ -49,6 +47,18 @@ function route(proxyIntegrationConfig, event) {
4947 }
5048}
5149
50+ function normalizeRequestPath ( event ) {
51+ // ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
52+ // if host is from amazonaws.com, then event.path is just '/resource-path':
53+ const apiId = event . requestContext ? event . requestContext . apiId : null ; // the apiId that is the first part of the amazonaws.com-host
54+ if ( ( apiId && event . headers && event . headers . Host && event . headers . Host . substring ( 0 , apiId . length ) != apiId ) ) {
55+ // remove first path element:
56+ const groups = / \/ [ ^ \/ ] + ( .* ) / . exec ( event . path ) || [ null , null ] ;
57+ return groups [ 1 ] || '/' ;
58+ }
59+
60+ return event . path ;
61+ }
5262
5363function convertError ( error , errorMapping , headers ) {
5464 if ( typeof error . reason === 'string' && typeof error . message === 'string' && errorMapping && errorMapping [ error . reason ] ) {
@@ -95,4 +105,4 @@ function extractPathNames(pathExpression) {
95105 return pathNames && pathNames . length > 0 ? pathNames . slice ( 1 ) : null ;
96106}
97107
98- module . exports = route ;
108+ module . exports = process ;
0 commit comments