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
211 changes: 0 additions & 211 deletions lib/assets/regions.json

This file was deleted.

24 changes: 20 additions & 4 deletions lib/contentstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
*/
import packages from '../package.json'
import clonedeep from 'lodash/cloneDeep'
import getUserAgent, { getRegionEndpoint } from './core/Util.js'
import getUserAgent from './core/Util.js'
import contentstackClient from './contentstackClient.js'
import httpClient from './core/contentstackHTTPClient.js'
const regionHostMap = {
NA: 'api.contentstack.io',
EU: 'eu-api.contentstack.com',
AU: 'au-api.contentstack.com',
AZURE_NA: 'azure-na-api.contentstack.com',
AZURE_EU: 'azure-eu-api.contentstack.com',
GCP_NA: 'gcp-na-api.contentstack.com',
GCP_EU: 'gcp-eu-api.contentstack.com'
}

/**
* Create client instance
Expand Down Expand Up @@ -170,11 +179,18 @@ import httpClient from './core/contentstackHTTPClient.js'
* @returns {ContentstackClient} Instance of ContentstackClient
*/
export function client (params = {}) {
let defaultHostName = getRegionEndpoint('na')
let defaultHostName

if (params.region) {
params.region = params.region.toLowerCase()
defaultHostName = getRegionEndpoint(params.region)
const region = params.region.toLowerCase()
if (!regionHostMap[region]) {
throw new Error(`Invalid region '${params.region}' provided. Allowed regions are: ${Object.keys(regionHostMap).join(', ')}`)
}
defaultHostName = regionHostMap[region]
} else if (params.host) {
defaultHostName = params.host
} else {
defaultHostName = regionHostMap['NA']
}

const defaultParameter = {
Expand Down
12 changes: 0 additions & 12 deletions lib/core/Util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { platform, release } from 'os'
import regionHostMap from '../assets/regions.json'

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

Expand Down Expand Up @@ -237,14 +236,3 @@ export const validateAndSanitizeConfig = (config) => {
url: config.url.trim() // Sanitize URL by removing whitespace
}
}

export const getRegionEndpoint = (region, service = 'contentManagement') => {
const regionData = regionHostMap.regions.find(r =>
r.id === region ||
r.alias.some(alias => alias === region)
)
if (!regionData) {
throw new Error(`Invalid region '${region}' provided. Allowed regions are: ${regionHostMap.regions.map(r => r.id).join(', ')}`)
}
return regionData.endpoints[service]?.replace(/^https?:\/\//, '')
}
30 changes: 13 additions & 17 deletions lib/core/contentstackHTTPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import clonedeep from 'lodash/cloneDeep'
import Qs from 'qs'
import { ConcurrencyQueue } from './concurrency-queue'
import { getRegionEndpoint, isHost } from './Util'
import { isHost } from './Util'

export default function contentstackHttpClient (options) {
const defaultConfig = {
Expand Down Expand Up @@ -68,28 +68,24 @@ export default function contentstackHttpClient (options) {
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`
}
const baseURL = config.endpoint || `${protocol}://${hostname}:${port}${config.basePath}/{api-version}`
let uiHostName = hostname
let developerHubBaseUrl = hostname

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]
}
if (uiHostName?.endsWith('io')) {
uiHostName = uiHostName.replace('io', 'com')
}

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')
developerHubBaseUrl = `https://${transformedHost.replace('-api.', '-developerhub-api.')}`
} else {
uiHostName = getRegionEndpoint(region, 'application')
developerHubBaseUrl = `https://${getRegionEndpoint(region, 'developerHub')}`
if (uiHostName) {
uiHostName = uiHostName.replace('api', 'app')
}

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

// set ui host name
const axiosOptions = {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
"pre-commit": "npm run lint && husky install && husky && chmod +x .husky/pre-commit && ./.husky/pre-commit",
"prepush": "npm run test:unit",
"generate:docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --readme README.md --verbose",
"husky-check": "npx husky && chmod +x .husky/pre-commit",
"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'",
"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'"
"husky-check": "npx husky && chmod +x .husky/pre-commit"
},
"engines": {
"node": ">=8.0.0"
Expand Down
1 change: 0 additions & 1 deletion test/unit/ContentstackClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ describe('Contentstack Client', () => {
it('should prioritize tfa_token over mfaSecret', done => {
mock.onPost('/user-session').reply(config => {
const data = JSON.parse(config.data)
console.log(data)
expect(data.user).to.deep.equal({
email: 'test@example.com',
password: 'password123',
Expand Down
Loading
Loading