Skip to content

Commit 422bac1

Browse files
fossamagnabrettstack
authored andcommitted
fix: support nested routes and custom domains (#555)
1 parent d9be538 commit 422bac1

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

__tests__/unit.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const apiGatewayEventSource = eventSources.getEventSource({ eventSourceName: 'AW
1010

1111
test('getPathWithQueryStringParams: no params', () => {
1212
const event = {
13+
resource: '/',
1314
path: '/foo/bar'
1415
}
1516
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
@@ -18,6 +19,7 @@ test('getPathWithQueryStringParams: no params', () => {
1819

1920
test('getPathWithQueryStringParams: 1 param', () => {
2021
const event = {
22+
resource: '/',
2123
path: '/foo/bar',
2224
multiValueQueryStringParameters: {
2325
bizz: 'bazz'
@@ -29,6 +31,7 @@ test('getPathWithQueryStringParams: 1 param', () => {
2931

3032
test('getPathWithQueryStringParams: to be url-encoded param', () => {
3133
const event = {
34+
resource: '/',
3235
path: '/foo/bar',
3336
multiValueQueryStringParameters: {
3437
redirect_uri: 'http://lvh.me:3000/cb'
@@ -40,6 +43,7 @@ test('getPathWithQueryStringParams: to be url-encoded param', () => {
4043

4144
test('getPathWithQueryStringParams: 2 params', () => {
4245
const event = {
46+
resource: '/',
4347
path: '/foo/bar',
4448
multiValueQueryStringParameters: {
4549
bizz: 'bazz',
@@ -52,6 +56,7 @@ test('getPathWithQueryStringParams: 2 params', () => {
5256

5357
test('getPathWithQueryStringParams: array param', () => {
5458
const event = {
59+
resource: '/',
5560
path: '/foo/bar',
5661
multiValueQueryStringParameters: {
5762
bizz: [
@@ -64,8 +69,52 @@ test('getPathWithQueryStringParams: array param', () => {
6469
expect(pathWithQueryStringParams).toEqual('/foo/bar?bizz=bazz&bizz=buzz')
6570
})
6671

72+
test('getPathWithQueryStringParams: pathParameters.proxy', () => {
73+
const event = {
74+
resource: '/foo/{proxy+}',
75+
path: '/foo/123',
76+
pathParameters: {
77+
proxy: '123'
78+
}
79+
}
80+
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
81+
expect(pathWithQueryStringParams).toEqual('/123')
82+
})
83+
84+
test('getPathWithQueryStringParams: customDomain and no path parameters', () => {
85+
const event = {
86+
resource: '/foo',
87+
path: '/baz/foo',
88+
requestContext: {
89+
customDomain: {
90+
basePathMatched: 'baz'
91+
}
92+
}
93+
}
94+
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
95+
expect(pathWithQueryStringParams).toEqual('/')
96+
})
97+
98+
test('getPathWithQueryStringParams: customDomain and path parameters', () => {
99+
const event = {
100+
resource: '/foo/{proxy+}',
101+
path: '/baz/foo/123',
102+
pathParameters: {
103+
proxy: '123'
104+
},
105+
requestContext: {
106+
customDomain: {
107+
basePathMatched: 'baz'
108+
}
109+
}
110+
}
111+
const pathWithQueryStringParams = serverlessExpressEventSourcesUtils.getPathWithQueryStringParams({ event })
112+
expect(pathWithQueryStringParams).toEqual('/123')
113+
})
114+
67115
function getReqRes (multiValueHeaders = {}) {
68116
const event = {
117+
resource: '/',
69118
path: '/foo',
70119
httpMethod: 'GET',
71120
body: 'Hello serverless!',

src/event-sources/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
const url = require('url')
22

3+
function getDefalutStripBasePath (event) {
4+
const basePathMatched = event?.requestContext?.customDomain?.basePathMatched || ''
5+
const resource = event?.pathParameters?.proxy ? '' : event.resource
6+
return basePathMatched ? `/${basePathMatched}${resource}` : resource
7+
}
8+
39
function getPathWithQueryStringParams ({
410
event,
511
query = event.multiValueQueryStringParameters,
612
// NOTE: Use `event.pathParameters.proxy` if available ({proxy+}); fall back to `event.path`
713
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path,
814
// NOTE: Strip base path for custom domains
9-
stripBasePath = '',
15+
stripBasePath = getDefalutStripBasePath(event),
1016
replaceRegex = new RegExp(`^${stripBasePath}`)
1117
}) {
18+
const pathname = path.replace(replaceRegex, '')
1219
return url.format({
13-
pathname: path.replace(replaceRegex, ''),
20+
pathname: pathname.startsWith('/') ? pathname : `/${pathname}`,
1421
query
1522
})
1623
}

0 commit comments

Comments
 (0)