Skip to content

Commit b27df6d

Browse files
authored
Merge pull request #461 from contentstack/development
Development
2 parents 9781c8c + e3eaa9e commit b27df6d

File tree

9 files changed

+35
-843
lines changed

9 files changed

+35
-843
lines changed

lib/assets/regions.json

Lines changed: 0 additions & 211 deletions
This file was deleted.

lib/contentstack.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
*/
55
import packages from '../package.json'
66
import clonedeep from 'lodash/cloneDeep'
7-
import getUserAgent, { getRegionEndpoint } from './core/Util.js'
7+
import getUserAgent from './core/Util.js'
88
import contentstackClient from './contentstackClient.js'
99
import httpClient from './core/contentstackHTTPClient.js'
10+
const regionHostMap = {
11+
NA: 'api.contentstack.io',
12+
EU: 'eu-api.contentstack.com',
13+
AU: 'au-api.contentstack.com',
14+
AZURE_NA: 'azure-na-api.contentstack.com',
15+
AZURE_EU: 'azure-eu-api.contentstack.com',
16+
GCP_NA: 'gcp-na-api.contentstack.com',
17+
GCP_EU: 'gcp-eu-api.contentstack.com'
18+
}
1019

1120
/**
1221
* Create client instance
@@ -170,11 +179,18 @@ import httpClient from './core/contentstackHTTPClient.js'
170179
* @returns {ContentstackClient} Instance of ContentstackClient
171180
*/
172181
export function client (params = {}) {
173-
let defaultHostName = getRegionEndpoint('na')
182+
let defaultHostName
174183

175184
if (params.region) {
176-
params.region = params.region.toLowerCase()
177-
defaultHostName = getRegionEndpoint(params.region)
185+
const region = params.region.toUpperCase()
186+
if (!regionHostMap[region]) {
187+
throw new Error(`Invalid region '${params.region}' provided. Allowed regions are: ${Object.keys(regionHostMap).join(', ')}`)
188+
}
189+
defaultHostName = regionHostMap[region]
190+
} else if (params.host) {
191+
defaultHostName = params.host
192+
} else {
193+
defaultHostName = regionHostMap['NA']
178194
}
179195

180196
const defaultParameter = {

lib/core/Util.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { platform, release } from 'os'
2-
import regionHostMap from '../assets/regions.json'
32

43
const HOST_REGEX = /^(?!(?:(?:https?|ftp):\/\/|internal|localhost|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))(?:[\w-]+\.contentstack\.(?:io|com)(?::[^\/\s:]+)?|[\w-]+(?:\.[\w-]+)*(?::[^\/\s:]+)?)(?![\/?#])$/ // eslint-disable-line
54

@@ -237,14 +236,3 @@ export const validateAndSanitizeConfig = (config) => {
237236
url: config.url.trim() // Sanitize URL by removing whitespace
238237
}
239238
}
240-
241-
export const getRegionEndpoint = (region, service = 'contentManagement') => {
242-
const regionData = regionHostMap.regions.find(r =>
243-
r.id === region ||
244-
r.alias.some(alias => alias === region)
245-
)
246-
if (!regionData) {
247-
throw new Error(`Invalid region '${region}' provided. Allowed regions are: ${regionHostMap.regions.map(r => r.id).join(', ')}`)
248-
}
249-
return regionData.endpoints[service]?.replace(/^https?:\/\//, '')
250-
}

lib/core/contentstackHTTPClient.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'axios'
22
import clonedeep from 'lodash/cloneDeep'
33
import Qs from 'qs'
44
import { ConcurrencyQueue } from './concurrency-queue'
5-
import { getRegionEndpoint, isHost } from './Util'
5+
import { isHost } from './Util'
66

77
export default function contentstackHttpClient (options) {
88
const defaultConfig = {
@@ -68,28 +68,24 @@ export default function contentstackHttpClient (options) {
6868
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`
6969
}
7070
const baseURL = config.endpoint || `${protocol}://${hostname}:${port}${config.basePath}/{api-version}`
71+
let uiHostName = hostname
72+
let developerHubBaseUrl = hostname
7173

72-
let region = config.region || 'na'
73-
if (!config.region && config.host) {
74-
const hostRegionMatch = config.host.match(/^([a-z]+-?[a-z]*)-api\./)
75-
if (hostRegionMatch) {
76-
region = hostRegionMatch[1]
77-
}
74+
if (uiHostName?.endsWith('io')) {
75+
uiHostName = uiHostName.replace('io', 'com')
7876
}
7977

80-
let uiHostName, developerHubBaseUrl
81-
if (config.host && (config.host.startsWith('dev') || config.host.startsWith('stag'))) {
82-
uiHostName = config.host.replace('-api.', '-app.')
83-
const transformedHost = config.host
84-
.replace(/^dev\d+/, 'dev')
85-
.replace(/^stag\d+/, 'stag')
86-
developerHubBaseUrl = `https://${transformedHost.replace('-api.', '-developerhub-api.')}`
87-
} else {
88-
uiHostName = getRegionEndpoint(region, 'application')
89-
developerHubBaseUrl = `https://${getRegionEndpoint(region, 'developerHub')}`
78+
if (uiHostName) {
79+
uiHostName = uiHostName.replace('api', 'app')
9080
}
9181

9282
const uiBaseUrl = config.endpoint || `${protocol}://${uiHostName}`
83+
developerHubBaseUrl = developerHubBaseUrl
84+
?.replace('api', 'developerhub-api')
85+
.replace(/^dev\d+/, 'dev') // Replaces any 'dev1', 'dev2', etc. with 'dev'
86+
.replace('io', 'com')
87+
.replace(/^http/, '') // Removing `http` if already present
88+
.replace(/^/, 'https://') // Adds 'https://' at the start if not already there
9389

9490
// set ui host name
9591
const axiosOptions = {

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
"pre-commit": "npm run lint && husky install && husky && chmod +x .husky/pre-commit && ./.husky/pre-commit",
4545
"prepush": "npm run test:unit",
4646
"generate:docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --readme README.md --verbose",
47-
"husky-check": "npx husky && chmod +x .husky/pre-commit",
48-
"postinstall": "curl -s --max-time 30 --fail https://artifacts.contentstack.com/regions.json -o lib/assets/regions.json || echo 'Warning: Failed to download regions.json, using existing file if available'",
49-
"postupdate": "curl -s --max-time 30 --fail https://artifacts.contentstack.com/regions.json -o lib/assets/regions.json || echo 'Warning: Failed to download regions.json, using existing file if available'"
47+
"husky-check": "npx husky && chmod +x .husky/pre-commit"
5048
},
5149
"engines": {
5250
"node": ">=8.0.0"

test/unit/ContentstackClient-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ describe('Contentstack Client', () => {
273273
it('should prioritize tfa_token over mfaSecret', done => {
274274
mock.onPost('/user-session').reply(config => {
275275
const data = JSON.parse(config.data)
276-
console.log(data)
277276
expect(data.user).to.deep.equal({
278277
email: 'test@example.com',
279278
password: 'password123',

0 commit comments

Comments
 (0)