From 74cffc0b8f0e141b03f45e55c390395a28212cb7 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Thu, 30 Oct 2025 15:16:23 +0530 Subject: [PATCH 1/4] Fixes OAuth authentication issues when using custom hosts without specifying region --- lib/core/contentstackHTTPClient.js | 25 +++++++++++++++++++++++-- test/sanity-check/api/oauth-test.js | 6 ------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/core/contentstackHTTPClient.js b/lib/core/contentstackHTTPClient.js index 9a3e2a68..d995680f 100644 --- a/lib/core/contentstackHTTPClient.js +++ b/lib/core/contentstackHTTPClient.js @@ -68,9 +68,30 @@ export default function contentstackHttpClient (options) { config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}` } const baseURL = config.endpoint || `${protocol}://${hostname}:${port}${config.basePath}/{api-version}` - const region = config.region || 'na' + + let region = config.region || 'na' + if (!config.region && config.host) { + const hostRegionMatch = config.host.match(/^([a-z]+-?[a-z]*)-api\./) + if (hostRegionMatch) { + region = hostRegionMatch[1] + } + } + const uiHostName = getRegionEndpoint(region, 'application') - const developerHubBaseUrl = getRegionEndpoint(region, 'developerHub').replace(/^/, 'https://') + + let developerHubBaseUrl + if (config.host && (config.host.startsWith('dev') || config.host.startsWith('stag'))) { + developerHubBaseUrl = config.host + .replace('api', 'developerhub-api') + .replace(/^dev\d+/, 'dev') + .replace(/^stag\d+/, 'stag') + .replace(/^http:\/\//, '') + .replace(/^https:\/\//, '') + .replace(/^/, 'https://') + } else { + developerHubBaseUrl = getRegionEndpoint(region, 'developerHub').replace(/^/, 'https://') + } + const uiBaseUrl = config.endpoint || `${protocol}://${uiHostName}` // set ui host name diff --git a/test/sanity-check/api/oauth-test.js b/test/sanity-check/api/oauth-test.js index 4f7677d4..c06dc556 100644 --- a/test/sanity-check/api/oauth-test.js +++ b/test/sanity-check/api/oauth-test.js @@ -21,12 +21,6 @@ const oauthClient = client.oauth({ redirectUri: process.env.REDIRECT_URI }) -// Override developerHubBaseUrl if DEV_HUB_HOST is provided -if (process.env.DEV_HUB_HOST) { - oauthClient.axiosInstance.defaults.developerHubBaseUrl = `https://${process.env.DEV_HUB_HOST}` - oauthClient.developerHubBaseUrl = `https://${process.env.DEV_HUB_HOST}` -} - describe('OAuth Authentication API Test', () => { it('should login with credentials', done => { client.login({ email: process.env.EMAIL, password: process.env.PASSWORD }, { include_orgs: true, include_orgs_roles: true, include_stack_roles: true, include_user_settings: true }).then((response) => { From a75e1182881f07abf08e95ef7659fc7c7a47aec0 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Thu, 30 Oct 2025 15:22:29 +0530 Subject: [PATCH 2/4] Fix lint --- lib/core/contentstackHTTPClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/contentstackHTTPClient.js b/lib/core/contentstackHTTPClient.js index d995680f..53c4a8a2 100644 --- a/lib/core/contentstackHTTPClient.js +++ b/lib/core/contentstackHTTPClient.js @@ -82,7 +82,7 @@ export default function contentstackHttpClient (options) { let developerHubBaseUrl if (config.host && (config.host.startsWith('dev') || config.host.startsWith('stag'))) { developerHubBaseUrl = config.host - .replace('api', 'developerhub-api') + .replace('api', 'developerhub-api') .replace(/^dev\d+/, 'dev') .replace(/^stag\d+/, 'stag') .replace(/^http:\/\//, '') From 3c6ffebbc805c7c889e2bc5b31bcc2c9a44f1113 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Thu, 30 Oct 2025 16:11:50 +0530 Subject: [PATCH 3/4] Fix ui host for oauth --- lib/core/contentstackHTTPClient.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/core/contentstackHTTPClient.js b/lib/core/contentstackHTTPClient.js index 53c4a8a2..38920d50 100644 --- a/lib/core/contentstackHTTPClient.js +++ b/lib/core/contentstackHTTPClient.js @@ -77,19 +77,16 @@ export default function contentstackHttpClient (options) { } } - const uiHostName = getRegionEndpoint(region, 'application') - - let developerHubBaseUrl + let uiHostName, developerHubBaseUrl if (config.host && (config.host.startsWith('dev') || config.host.startsWith('stag'))) { - developerHubBaseUrl = config.host - .replace('api', 'developerhub-api') + const transformedHost = config.host .replace(/^dev\d+/, 'dev') .replace(/^stag\d+/, 'stag') - .replace(/^http:\/\//, '') - .replace(/^https:\/\//, '') - .replace(/^/, 'https://') + uiHostName = transformedHost.replace('-api.', '-app.') + developerHubBaseUrl = `https://${transformedHost.replace('-api.', '-developerhub-api.')}` } else { - developerHubBaseUrl = getRegionEndpoint(region, 'developerHub').replace(/^/, 'https://') + uiHostName = getRegionEndpoint(region, 'application') + developerHubBaseUrl = `https://${getRegionEndpoint(region, 'developerHub')}` } const uiBaseUrl = config.endpoint || `${protocol}://${uiHostName}` From 1c46d489fca1950054e330fe35252a2c9f3b7729 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Thu, 30 Oct 2025 16:46:03 +0530 Subject: [PATCH 4/4] Fix ui host for oauth --- lib/core/contentstackHTTPClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/contentstackHTTPClient.js b/lib/core/contentstackHTTPClient.js index 38920d50..adb09788 100644 --- a/lib/core/contentstackHTTPClient.js +++ b/lib/core/contentstackHTTPClient.js @@ -79,10 +79,10 @@ export default function contentstackHttpClient (options) { let uiHostName, developerHubBaseUrl if (config.host && (config.host.startsWith('dev') || config.host.startsWith('stag'))) { + uiHostName = config.host.replace('-api.', '-app.') const transformedHost = config.host .replace(/^dev\d+/, 'dev') .replace(/^stag\d+/, 'stag') - uiHostName = transformedHost.replace('-api.', '-app.') developerHubBaseUrl = `https://${transformedHost.replace('-api.', '-developerhub-api.')}` } else { uiHostName = getRegionEndpoint(region, 'application')