Skip to content

Commit 044dee1

Browse files
Refactor getComposableStudioUrl to derive region instead of using a hardcoded region parameter
1 parent a5a045c commit 044dee1

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

packages/contentstack-config/src/utils/region-handler.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,36 @@ function validURL(str) {
1717
}
1818

1919
/**
20-
* Helper function to get composable studio URL for a region
20+
* Helper function to get composable studio URL from CMA endpoint
2121
* Since composableStudio endpoint is not yet in @contentstack/utils, we construct it manually
22-
* @param {string} region - Region identifier (e.g., 'na', 'eu', 'au', 'azure-na', etc.)
22+
* by extracting the region prefix from the CMA URL
23+
* @param {string} cmaUrl - Content Management API URL (e.g., 'https://eu-api.contentstack.com')
2324
* @returns {string} Composable Studio URL for the region
2425
*/
25-
function getComposableStudioUrl(region: string): string {
26-
const normalizedRegion = region.toLowerCase().trim().replace(/_/g, '-');
26+
function getComposableStudioUrl(cmaUrl: string): string {
27+
// Extract hostname from URL (e.g., "eu-api.contentstack.com")
28+
const match = cmaUrl.match(/https?:\/\/([^/]+)/);
29+
if (!match) {
30+
return 'https://composable-studio-api.contentstack.com';
31+
}
32+
33+
const hostname = match[1];
34+
35+
// For default NA region: api.contentstack.io or api.contentstack.com
36+
if (hostname === 'api.contentstack.io' || hostname === 'api.contentstack.com') {
37+
return 'https://composable-studio-api.contentstack.com';
38+
}
39+
40+
// For other regions: {region}-api.contentstack.com
41+
// Extract the region prefix before "-api"
42+
const prefixMatch = hostname.match(/^(.+?)-api\.contentstack\.(com|io)$/);
43+
const regionPrefix = prefixMatch ? prefixMatch[1] : '';
2744

28-
// For North America (default region), no prefix is needed
29-
if (normalizedRegion === 'na' || normalizedRegion === 'us' || normalizedRegion === 'aws-na') {
45+
if (!regionPrefix) {
3046
return 'https://composable-studio-api.contentstack.com';
3147
}
3248

33-
// For other regions, use the region as a prefix
34-
return `https://${normalizedRegion}-composable-studio-api.contentstack.com`;
49+
return `https://${regionPrefix}-composable-studio-api.contentstack.com`;
3550
}
3651

3752
/**
@@ -56,7 +71,7 @@ function getRegionObject(regionKey: string) {
5671
developerHubUrl: endpoints.developerHub,
5772
launchHubUrl: endpoints.launch,
5873
personalizeUrl: endpoints.personalizeManagement,
59-
composableStudioUrl: getComposableStudioUrl(regionKey),
74+
composableStudioUrl: getComposableStudioUrl(endpoints.contentManagement),
6075
};
6176
} catch (error) {
6277
return null;

0 commit comments

Comments
 (0)