Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Commit dd32faf

Browse files
EthanDavisEthan Davis
andauthored
Added Flag to Disable Base Path Removal (#72)
* Added Flag to Disable Base Path Removal Added property `removeBasePath` to `proxyIntegrationConfig` This will allow users to disable the default functionality of removing the `/basepath` from incoming requests. If this looks okay I'll update documentation accordingly. * Added unit test to check remove basepath boolean. Updated parameter to deafult to true in order to not break original functionality Co-authored-by: Ethan Davis <[email protected]>
1 parent 938b4de commit dd32faf

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/proxyIntegration.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export type ProxyIntegrationError = {
4040
export interface ProxyIntegrationConfig {
4141
onError?: ErrorHandler
4242
cors?: CorsOptions | boolean
43-
routes: ProxyIntegrationRoute[]
43+
routes: ProxyIntegrationRoute[],
44+
removeBasePath?: boolean,
4445
debug?: boolean
4546
errorMapping?: ProxyIntegrationErrorMapping
4647
defaultHeaders?: APIGatewayProxyResult['headers']
@@ -119,7 +120,7 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
119120
console.log(`proxy path with event path: ${event.path}`)
120121
}
121122
} else {
122-
event.path = normalizeRequestPath(event)
123+
event.path = normalizeRequestPath(event, proxyIntegrationConfig.removeBasePath)
123124
}
124125

125126
try {
@@ -161,8 +162,8 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
161162
console.log('Error while evaluating matching action handler', error)
162163

163164
if (proxyIntegrationConfig.onError) {
164-
const promise = proxyIntegrationConfig.onError(error, event, context)
165-
Promise.resolve(promise).then(result => {
165+
const promise = proxyIntegrationConfig.onError(error, event, context)
166+
Promise.resolve(promise).then(result => {
166167
if (result != undefined) {
167168
return { headers, ...result }
168169
}
@@ -175,15 +176,15 @@ export const process: ProcessMethod<ProxyIntegrationConfig, APIGatewayProxyEvent
175176
}
176177
}
177178

178-
const normalizeRequestPath = (event: APIGatewayProxyEvent): string => {
179+
const normalizeRequestPath = (event: APIGatewayProxyEvent, removeBasePath: boolean = true): string => {
179180
if (isLocalExecution(event)) {
180181
return event.path
181182
}
182-
183183
// ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/'
184184
// if host is from amazonaws.com, then event.path is just '/resource-path':
185185
const apiId = event.requestContext ? event.requestContext.apiId : null // the apiId that is the first part of the amazonaws.com-host
186-
if ((apiId && event.headers && event.headers.Host && event.headers.Host.substring(0, apiId.length) !== apiId)) {
186+
if ((apiId && event.headers && event.headers.Host && event.headers.Host.substring(0, apiId.length) !== apiId)
187+
&& removeBasePath) {
187188
// remove first path element:
188189
const groups = /\/[^\/]+(.*)/.exec(event.path) || [null, null]
189190
return groups[1] || '/'

test/proxyIntegration.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,25 @@ describe('proxyIntegration.routeHandler', () => {
228228
paths: {}
229229
}, context)
230230
})
231+
it('should not remove basepath from root path if coming over custom domain name and removeBasePath is false', () => {
232+
const actionSpy = jasmine.createSpy('action')
233+
const event = {
234+
httpMethod: 'GET', path: '/shortcut-itemsdev',
235+
headers: { Host: 'api.ep.welt.de' },
236+
requestContext: { apiId: 'blabla' }
237+
}
238+
proxyIntegration({
239+
removeBasePath: false,
240+
routes: [{
241+
method: 'GET',
242+
path: '/shortcut-itemsdev',
243+
action: actionSpy
244+
}]
245+
}, event as any, context)
246+
expect(actionSpy).toHaveBeenCalledWith({
247+
httpMethod: 'GET', headers: jasmine.anything(), requestContext: jasmine.anything(), path: '/shortcut-itemsdev', routePath: '/shortcut-itemsdev', paths: {}
248+
}, context)
249+
})
231250
it('should not change path if not coming over custom domain name', async () => {
232251
await assertPathIsUnchanged('blabla.execute-api.eu-central-1.amazonaws.com')
233252
})

0 commit comments

Comments
 (0)