fix: region extract from konnect control plane endpoint (#10093)

(cherry picked from commit 0cc0295207)
This commit is contained in:
Shelby
2026-06-16 10:17:20 -07:00
committed by Insomnia
parent c9847329d5
commit fa714b61be
2 changed files with 12 additions and 2 deletions

View File

@@ -34,6 +34,14 @@ describe('extractRegionFromEndpoint', () => {
expect(extractRegionFromEndpoint('https://def456.in.cp0.konghq.com')).toBe('in');
});
it('extracts "eu" from a EU control plane endpoint with bare "cp" subdomain', () => {
expect(extractRegionFromEndpoint('https://xyz789.eu.cp.konghq.com')).toBe('eu');
});
it('extracts "us" from a US control plane endpoint with bare "cp" subdomain', () => {
expect(extractRegionFromEndpoint('https://abc123.us.cp.konghq.com')).toBe('us');
});
it('defaults to "us" for a malformed URL', () => {
expect(extractRegionFromEndpoint('not-a-url')).toBe('us');
});

View File

@@ -57,15 +57,17 @@ export function sanitizeRoute(route: KonnectRoute): KonnectRoute {
/**
* Derives the Konnect region string from a control plane endpoint URL.
* e.g. "https://abc123.us.cp0.konghq.com" → "us"
* e.g. "https://abc123.eu.cp.konghq.com" → "eu"
* e.g. "https://abc123.sg.cp.konghq.com" → "sg"
* Falls back to "us" for unrecognised or malformed values.
*/
export function extractRegionFromEndpoint(endpoint: string): string {
try {
const hostname = new URL(endpoint).hostname;
const parts = hostname.split('.');
// Pattern: <id>.<region>.cp0.konghq.com
// Pattern: <id>.<region>.cp[N].konghq.com (e.g. cp0, cp, cp1)
if (parts.length >= 4 && parts[parts.length - 2] === 'konghq' && parts[parts.length - 1] === 'com') {
if (parts[parts.length - 3] === 'cp0') {
if (/^cp\d*$/.test(parts[parts.length - 3])) {
return parts[parts.length - 4];
}
console.warn(`[konnect] Unexpected endpoint hostname format, defaulting region to "us": ${hostname}`);