Skip to content

Commit e6aea8b

Browse files
Region support added
1 parent 214ffc1 commit e6aea8b

File tree

6 files changed

+98
-5
lines changed

6 files changed

+98
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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

Lines changed: 25 additions & 2 deletions
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,9 +169,24 @@ import httpClient from './core/contentstackHTTPClient.js'
161169
* @returns Contentstack.Client
162170
*/
163171
export function client (params = {}) {
164-
const defaultParameter = {
165-
defaultHostName: 'api.contentstack.io'
172+
173+
let defaultHostName
174+
175+
if (params.region) {
176+
const region = params.region.toUpperCase()
177+
if (!regionHostMap[region]) {
178+
throw new Error(`Invalid region '${params.region}' provided. Allowed regions are: ${Object.keys(regionHostMap).join(', ')}`)
166179
}
180+
defaultHostName = regionHostMap[region]
181+
} else if (params.host) {
182+
defaultHostName = params.host
183+
} else {
184+
defaultHostName = regionHostMap['NA']
185+
}
186+
187+
const defaultParameter = {
188+
defaultHostName: defaultHostName
189+
}
167190

168191
const sdkAgent = `contentstack-management-javascript/${packages.version}`
169192
const userAgentHeader = getUserAgent(sdkAgent,

lib/core/contentstackHTTPClient.js

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 63 additions & 0 deletions
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,66 @@ 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+
129+
it('should throw error for invalid region', done => {
130+
try {
131+
contentstack.client({ region: 'DUMMYREGION' })
132+
done(new Error('Expected error was not thrown for invalid region'))
133+
} catch (error) {
134+
expect(error.message).to.include('Invalid region', 'Error message should indicate invalid region')
135+
done()
136+
}
137+
})
138+
139+
77140
})

0 commit comments

Comments
 (0)