Skip to content

Commit 12afb26

Browse files
Merge pull request #335 from contentstack/Enh/dx-2764
Region support added
2 parents 214ffc1 + c2ec78d commit 12afb26

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v1.21.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.21.0) (2025-05-05)
4+
- Enhancement
5+
- Region support added
6+
37
## [v1.20.3](https://github.com/contentstack/contentstack-management-javascript/tree/v1.20.3) (2025-04-21)
48
- Fix
59
- Handle the sanity tests when ENVs are not provided

lib/contentstack.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import clonedeep from 'lodash/cloneDeep'
77
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+
AZURE_NA: 'azure-na-api.contentstack.com',
14+
AZURE_EU: 'azure-eu-api.contentstack.com',
15+
GCP_NA: 'gcp-na-api.contentstack.com',
16+
GCP_EU: 'gcp-eu-api.contentstack.com'
17+
}
1018

1119
/**
1220
* Create client instance
@@ -161,8 +169,22 @@ import httpClient from './core/contentstackHTTPClient.js'
161169
* @returns Contentstack.Client
162170
*/
163171
export function client (params = {}) {
172+
let defaultHostName
173+
174+
if (params.region) {
175+
const region = params.region.toUpperCase()
176+
if (!regionHostMap[region]) {
177+
throw new Error(`Invalid region '${params.region}' provided. Allowed regions are: ${Object.keys(regionHostMap).join(', ')}`)
178+
}
179+
defaultHostName = regionHostMap[region]
180+
} else if (params.host) {
181+
defaultHostName = params.host
182+
} else {
183+
defaultHostName = regionHostMap['NA']
184+
}
185+
164186
const defaultParameter = {
165-
defaultHostName: 'api.contentstack.io'
187+
defaultHostName: defaultHostName
166188
}
167189

168190
const sdkAgent = `contentstack-management-javascript/${packages.version}`

lib/core/contentstackHTTPClient.js

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export default function contentstackHttpClient (options) {
5353
let port = config.port || 443
5454
const version = config.version || 'v3'
5555

56+
if (config.region) {
57+
config.host = config.defaultHostName // set region on priority
58+
}
5659
if (isHost(config.host)) {
5760
const parsed = config.host.split(':')
5861
if (parsed.length === 2) {

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.20.3",
3+
"version": "1.21.0",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

test/sanity-check/api/user-test.js

+60
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { contentstackClient } from '../../sanity-check/utility/ContentstackClien
44
import { jsonWrite } from '../../sanity-check/utility/fileOperations/readwrite'
55
import axios from 'axios'
66
import dotenv from 'dotenv'
7+
import * as contentstack from '../../../lib/contentstack.js'
78

89
dotenv.config()
910
var authtoken = ''
@@ -74,4 +75,63 @@ describe('Contentstack User Session api Test', () => {
7475
})
7576
.catch(done)
7677
})
78+
79+
it('should get host for NA region by default', done => {
80+
const client = contentstack.client()
81+
const baseUrl = client.axiosInstance.defaults.baseURL
82+
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly by default')
83+
done()
84+
})
85+
86+
it('should get host for NA region', done => {
87+
const client = contentstack.client({ region: 'NA' })
88+
const baseUrl = client.axiosInstance.defaults.baseURL
89+
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly')
90+
done()
91+
})
92+
93+
it('should get host for NA region on priority', done => {
94+
const client = contentstack.client({ region: 'NA', host: 'dev11-api.csnonprod.com' })
95+
const baseUrl = client.axiosInstance.defaults.baseURL
96+
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly with priority')
97+
done()
98+
})
99+
100+
it('should get custom host', done => {
101+
const client = contentstack.client({ host: 'dev11-api.csnonprod.com' })
102+
const baseUrl = client.axiosInstance.defaults.baseURL
103+
expect(baseUrl).to.include('dev11-api.csnonprod.com', 'custom host set correctly')
104+
done()
105+
})
106+
107+
it('should get host for EU region', done => {
108+
const client = contentstack.client({ region: 'EU' })
109+
const baseUrl = client.axiosInstance.defaults.baseURL
110+
expect(baseUrl).to.include('eu-api.contentstack.com', 'region EU set correctly')
111+
done()
112+
})
113+
114+
it('should get host for AZURE_NA region', done => {
115+
const client = contentstack.client({ region: 'AZURE_NA' })
116+
const baseUrl = client.axiosInstance.defaults.baseURL
117+
expect(baseUrl).to.include('azure-na-api.contentstack.com', 'region AZURE_NA set correctly')
118+
done()
119+
})
120+
121+
it('should get host for GCP_NA region', done => {
122+
const client = contentstack.client({ region: 'GCP_NA' })
123+
const baseUrl = client.axiosInstance.defaults.baseURL
124+
expect(baseUrl).to.include('gcp-na-api.contentstack.com', 'region GCP_NA set correctly')
125+
done()
126+
})
127+
128+
it('should throw error for invalid region', done => {
129+
try {
130+
contentstack.client({ region: 'DUMMYREGION' })
131+
done(new Error('Expected error was not thrown for invalid region'))
132+
} catch (error) {
133+
expect(error.message).to.include('Invalid region', 'Error message should indicate invalid region')
134+
done()
135+
}
136+
})
77137
})

0 commit comments

Comments
 (0)