Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions api/_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,43 @@ function stripProxyPrefix(pathname, prefixes) {
return pathname.slice(matchedPrefix.length) || '/';
}

function buildTargetPathFromQuery(incomingUrl, pathQueryParam) {
if (!pathQueryParam) {
return null;
}

const pathFromQuery = incomingUrl.searchParams.get(pathQueryParam);

if (!pathFromQuery) {
return '/';
}

incomingUrl.searchParams.delete(pathQueryParam);

return pathFromQuery.startsWith('/') ? pathFromQuery : `/${pathFromQuery}`;
}

function buildTargetUrl(requestUrl, options, targetBase) {
const incomingUrl = new URL(requestUrl, 'https://vercel.local');
const pathname = stripProxyPrefix(incomingUrl.pathname, normalizeProxyPrefixes(options));
const pathname =
buildTargetPathFromQuery(incomingUrl, options.pathQueryParam) ??
stripProxyPrefix(incomingUrl.pathname, normalizeProxyPrefixes(options));

const targetUrl = new URL(pathname || '/', targetBase);
targetUrl.search = incomingUrl.search;
targetUrl.search = incomingUrl.searchParams.toString();

return targetUrl;
}

async function proxyRequest(request, response, { envName, prefix, prefixes }) {
async function proxyRequest(request, response, { envName, pathQueryParam, prefix, prefixes }) {
let targetUrl;

try {
targetUrl = buildTargetUrl(request.url, { prefix, prefixes }, requireProxyTarget(envName));
targetUrl = buildTargetUrl(
request.url,
{ pathQueryParam, prefix, prefixes },
requireProxyTarget(envName),
);
} catch (error) {
response.statusCode = 500;
response.setHeader('content-type', 'application/json; charset=utf-8');
Expand Down
14 changes: 14 additions & 0 deletions api/_proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ describe('buildTargetUrl', () => {

expect(targetUrl.toString()).toBe('https://backend.example.com/be10/api/v1/scan');
});

it('builds the target path from a Vercel rewrite query parameter', () => {
const targetUrl = buildTargetUrl(
'/api/be3?path=api/v1/scan/subscribe&guest_uuid=guest-1',
{
pathQueryParam: 'path',
},
targetBase,
);

expect(targetUrl.toString()).toBe(
'https://backend.example.com/api/v1/scan/subscribe?guest_uuid=guest-1',
);
});
});
8 changes: 8 additions & 0 deletions api/be1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { proxyRequest } = require('./_proxy');

module.exports = function handler(request, response) {
return proxyRequest(request, response, {
envName: 'BE1_PROXY_TARGET_URL',
pathQueryParam: 'path',
});
};
8 changes: 8 additions & 0 deletions api/be3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { proxyRequest } = require('./_proxy');

module.exports = function handler(request, response) {
return proxyRequest(request, response, {
envName: 'BE3_PROXY_TARGET_URL',
pathQueryParam: 'path',
});
};
4 changes: 2 additions & 2 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"rewrites": [
{
"source": "/be1/:path*",
"destination": "/api/be1/:path*"
"destination": "/api/be1?path=:path*"
},
{
"source": "/be3/:path*",
"destination": "/api/be3/:path*"
"destination": "/api/be3?path=:path*"
}
]
}
Loading