Skip to content

Commit cdbcd0a

Browse files
expose matchedPath (spring-media#49)
* expose matchedPath Co-authored-by: Eugene Kireev <[email protected]>
1 parent 64d832f commit cdbcd0a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/proxyIntegration.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
2+
23
import { ProcessMethod } from './EventProcessor'
34

45
type ProxyIntegrationParams = {
56
paths?: { [paramId: string]: string }
7+
routePath?: string
68
}
79
type ProxyIntegrationBody<T = unknown> = {
810
body: T
@@ -125,12 +127,14 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
125127
try {
126128
const actionConfig = findMatchingActionConfig(event.httpMethod, event.path, proxyIntegrationConfig) || {
127129
action: NO_MATCHING_ACTION,
130+
routePath: undefined,
128131
paths: undefined
129132
}
130133

131134
const proxyEvent: ProxyIntegrationEvent = event
132135

133136
proxyEvent.paths = actionConfig.paths
137+
proxyEvent.routePath = actionConfig.routePath
134138
if (event.body) {
135139
try {
136140
proxyEvent.body = JSON.parse(event.body)
@@ -202,7 +206,7 @@ const convertError = (error: ProxyIntegrationError | Error, errorMapping?: Proxy
202206
}
203207

204208
const findMatchingActionConfig = (httpMethod: string, httpPath: string, routeConfig: ProxyIntegrationConfig):
205-
Pick<ProxyIntegrationRoute, 'action'> & ProxyIntegrationParams | null => {
209+
ProxyIntegrationRoute & ProxyIntegrationParams | null => {
206210

207211
const paths: ProxyIntegrationParams['paths'] = {}
208212
const matchingMethodRoutes = routeConfig.routes.filter(route => route.method === httpMethod)
@@ -220,7 +224,8 @@ const findMatchingActionConfig = (httpMethod: string, httpPath: string, routeCon
220224
console.log(`Found matching route ${route.path} with paths`, paths)
221225
}
222226
return {
223-
action: route.action,
227+
...route,
228+
routePath: route.path,
224229
paths
225230
}
226231
}

test/proxyIntegration.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// helper for parameterized tests (http://blog.piotrturski.net/2015/04/jasmine-parameterized-tests.html)
2-
import { process as proxyIntegration, ProxyIntegrationConfig } from '../lib/proxyIntegration'
2+
import { ProxyIntegrationConfig, process as proxyIntegration } from '../lib/proxyIntegration'
3+
34
import { APIGatewayProxyEvent } from 'aws-lambda'
45

56
function forEach(arrayOfArrays: any) {
@@ -33,7 +34,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
3334
{ path: '/123', method: 'GET', action: actionSpy }
3435
]
3536
}, { httpMethod: 'GET', path: '/123' } as APIGatewayProxyEvent, context)
36-
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {} }, jasmine.anything())
37+
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {}, routePath: '/123' }, jasmine.anything())
3738
})
3839

3940
it('should select longer match', () => {
@@ -45,7 +46,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
4546
{ path: '/123', method: 'GET', action: actionSpy }
4647
]
4748
}, { httpMethod: 'GET', path: '/123' } as APIGatewayProxyEvent, context)
48-
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {} }, context)
49+
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {}, routePath: '/123' }, context)
4950
})
5051
forEach([
5152
['/:param'],
@@ -59,7 +60,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
5960
{ path: path, method: 'GET', action: actionSpy }
6061
]
6162
}, { httpMethod: 'GET', path: '/456' } as APIGatewayProxyEvent, context)
62-
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/456', paths: { param: '456' } }, context)
63+
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/456', routePath: path, paths: { param: '456' } }, context)
6364
})
6465
forEach([
6566
['/:param'],
@@ -73,7 +74,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
7374
{ path: path, method: 'GET', action: () => 'param' as any }
7475
]
7576
}, { httpMethod: 'GET', path: '/123' } as APIGatewayProxyEvent, context)
76-
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {} }, context)
77+
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', routePath: '/123', paths: {} }, context)
7778
})
7879
forEach([
7980
['/:param'],
@@ -90,6 +91,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
9091
expect(actionSpy).toHaveBeenCalledWith({
9192
httpMethod: 'GET',
9293
path: '/%2Fwirtschaft%2Farticle85883...tml',
94+
routePath: path,
9395
paths: { param: '/wirtschaft/article85883...tml' }
9496
}, context)
9597
})
@@ -106,6 +108,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
106108
expect(actionSpy).toHaveBeenCalledWith({
107109
httpMethod: 'GET',
108110
path: '/%2Fdeutschland-bewegt-sich%2F',
111+
routePath: path,
109112
paths: { param: '/deutschland-bewegt-sich/' }
110113
}, context)
111114
})
@@ -125,6 +128,7 @@ describe('proxyIntegration.routeHandler.selection', () => {
125128
expect(actionSpy).toHaveBeenCalledWith({
126129
httpMethod: 'GET',
127130
path: '/%2Fboerse%2FResources%2FImages%2Fcss%2Farrows_change-2.0.1.png?rfid=2013011501',
131+
routePath: path,
128132
paths: { param: '/boerse/Resources/Images/css/arrows_change-2.0.1.png?rfid=2013011501' }
129133
}, context)
130134
})
@@ -178,7 +182,7 @@ describe('proxyIntegration.routeHandler', () => {
178182
}, event as any, context as any)
179183

180184
expect(actionSpy).toHaveBeenCalledWith({
181-
httpMethod: 'GET', headers: jasmine.anything(), requestContext: jasmine.anything(), path: '/', paths: {}
185+
httpMethod: 'GET', headers: jasmine.anything(), requestContext: jasmine.anything(), path: '/', routePath: '/', paths: {}
182186
}, context)
183187
})
184188

@@ -197,7 +201,7 @@ describe('proxyIntegration.routeHandler', () => {
197201
}]
198202
}, event as any, context)
199203
expect(actionSpy).toHaveBeenCalledWith({
200-
httpMethod: 'GET', headers: jasmine.anything(), requestContext: jasmine.anything(), path: '/', paths: {}
204+
httpMethod: 'GET', headers: jasmine.anything(), requestContext: jasmine.anything(), path: '/', routePath: '/', paths: {}
201205
}, context)
202206
})
203207
it('should remove basepath from multi-slash-path if coming over custom domain name', () => {
@@ -219,6 +223,7 @@ describe('proxyIntegration.routeHandler', () => {
219223
headers: jasmine.anything(),
220224
requestContext: jasmine.anything(),
221225
path: '/123/456',
226+
routePath: '/123/456',
222227
paths: {}
223228
}, context)
224229
})
@@ -299,7 +304,7 @@ describe('proxyIntegration.routeHandler', () => {
299304
]
300305
}
301306
await proxyIntegration(routeConfig, { path, httpMethod: 'GET' } as APIGatewayProxyEvent, context)
302-
expect(spiedAction).toHaveBeenCalledWith({ path, httpMethod: 'GET', paths: expectedPathValues }, context)
307+
expect(spiedAction).toHaveBeenCalledWith({ path, httpMethod: 'GET', paths: expectedPathValues, routePath: pathConfig }, context)
303308
})
304309

305310

@@ -446,6 +451,7 @@ describe('proxyIntegration.proxyPath', () => {
446451
resource: '/{apiPath+}',
447452
paths: {},
448453
path: '/article/list',
454+
routePath: '/article/list',
449455
httpMethod: 'GET',
450456
pathParameters: { apiPath: '/article/list' }
451457
}, context)
@@ -485,6 +491,7 @@ describe('proxyIntegration.proxyPath', () => {
485491
resource: '/{apiPath+}',
486492
paths: {},
487493
path: '/article/list',
494+
routePath: '/article/list',
488495
httpMethod: 'GET',
489496
pathParameters: { apiPath: '/article/list' }
490497
}, context)
@@ -500,6 +507,7 @@ describe('proxyIntegration.proxyPath', () => {
500507
resource: '/{apiPath+}',
501508
paths: {},
502509
path: '/section/list',
510+
routePath: '/section/list',
503511
httpMethod: 'GET',
504512
pathParameters: { apiPath: '/section/list' }
505513
}, context)
@@ -603,6 +611,7 @@ const assertPathIsUnchanged = async (hostname: string) => {
603611
headers: jasmine.anything(),
604612
requestContext: jasmine.anything(),
605613
path: '/123/456',
614+
routePath: '/123/456',
606615
paths: {}
607616
}, context)
608617
}

0 commit comments

Comments
 (0)