Skip to content

Commit 224dea2

Browse files
committed
Implement region-based endpoint configuration and add tests for various regions and aliases
1 parent 8802004 commit 224dea2

File tree

7 files changed

+606
-5
lines changed

7 files changed

+606
-5
lines changed

.talismanrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ fileignoreconfig:
3030
checksum: b76ca091caa3a1b2658cd422a2d8ef3ac9996aea0aff3f982d56bb309a3d9fde
3131
- filename: test/unit/ContentstackClient-test.js
3232
checksum: 974a4f335aef025b657d139bb290233a69bed1976b947c3c674e97baffe4ce2f
33+
- filename: test/unit/ContentstackHTTPClient-test.js
34+
checksum: 4043efd843e24da9afd0272c55ef4b0432e3374b2ca12b913f1a6654df3f62be
35+
- filename: test/unit/contentstack-test.js
36+
checksum: 2597efae3c1ab8cc173d5bf205f1c76932211f8e0eb2a16444e055d83481976c
3337
version: "1.0"
38+
39+
40+

lib/contentstack.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import clonedeep from 'lodash/cloneDeep'
77
import getUserAgent, { getRegionEndpoint } from './core/Util.js'
88
import contentstackClient from './contentstackClient.js'
99
import httpClient from './core/contentstackHTTPClient.js'
10-
import regionHostMap from './assets/regions.json'
1110

1211
/**
1312
* Create client instance

lib/core/Util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,4 @@ export const getRegionEndpoint = (region, service = 'contentManagement') => {
247247
throw new Error(`Invalid region '${region}' provided. Allowed regions are: ${regionHostMap.regions.map(r => r.id).join(', ')}`)
248248
}
249249
return regionData.endpoints[service]?.replace(/^https?:\/\//, '')
250-
}
250+
}

lib/core/contentstackHTTPClient.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ 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-
const uiHostName = getRegionEndpoint(config.region, 'application')
72-
const developerHubBaseUrl = getRegionEndpoint(config.region, 'developerHub').replace(/^/, 'https://')
71+
const region = config.region || 'na'
72+
const uiHostName = getRegionEndpoint(region, 'application')
73+
const developerHubBaseUrl = getRegionEndpoint(region, 'developerHub').replace(/^/, 'https://')
7374
const uiBaseUrl = config.endpoint || `${protocol}://${uiHostName}`
7475

7576
// set ui host name

test/unit/ContentstackHTTPClient-test.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,169 @@ describe('Contentstack HTTP Client', () => {
167167
expect(axiosInstance.defaults.headers['x-header-ea']).to.be.equal('ea1,ea2')
168168
done()
169169
})
170+
171+
describe('Region-based endpoint configuration', () => {
172+
it('should configure endpoints for NA region', done => {
173+
var axiosInstance = contentstackHTTPClient({
174+
apiKey: 'apiKey',
175+
accessToken: 'accessToken',
176+
defaultHostName: 'api.contentstack.io',
177+
region: 'na'
178+
})
179+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://app.contentstack.com', 'NA UI base URL should match')
180+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('developerhub-api.contentstack.com', 'NA developer hub should match')
181+
done()
182+
})
183+
184+
it('should configure endpoints for EU region', done => {
185+
var axiosInstance = contentstackHTTPClient({
186+
apiKey: 'apiKey',
187+
accessToken: 'accessToken',
188+
defaultHostName: 'eu-api.contentstack.com',
189+
region: 'eu'
190+
})
191+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://eu-app.contentstack.com', 'EU UI base URL should match')
192+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('eu-developerhub-api.contentstack.com', 'EU developer hub should match')
193+
done()
194+
})
195+
196+
it('should configure endpoints for AU region', done => {
197+
var axiosInstance = contentstackHTTPClient({
198+
apiKey: 'apiKey',
199+
accessToken: 'accessToken',
200+
defaultHostName: 'au-api.contentstack.com',
201+
region: 'au'
202+
})
203+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://au-app.contentstack.com', 'AU UI base URL should match')
204+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('au-developerhub-api.contentstack.com', 'AU developer hub should match')
205+
done()
206+
})
207+
208+
it('should configure endpoints for Azure NA region', done => {
209+
var axiosInstance = contentstackHTTPClient({
210+
apiKey: 'apiKey',
211+
accessToken: 'accessToken',
212+
defaultHostName: 'azure-na-api.contentstack.com',
213+
region: 'azure-na'
214+
})
215+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://azure-na-app.contentstack.com', 'Azure NA UI base URL should match')
216+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('azure-na-developerhub-api.contentstack.com', 'Azure NA developer hub should match')
217+
done()
218+
})
219+
220+
it('should configure endpoints for Azure EU region', done => {
221+
var axiosInstance = contentstackHTTPClient({
222+
apiKey: 'apiKey',
223+
accessToken: 'accessToken',
224+
defaultHostName: 'azure-eu-api.contentstack.com',
225+
region: 'azure-eu'
226+
})
227+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://azure-eu-app.contentstack.com', 'Azure EU UI base URL should match')
228+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('azure-eu-developerhub-api.contentstack.com', 'Azure EU developer hub should match')
229+
done()
230+
})
231+
232+
it('should configure endpoints for GCP NA region', done => {
233+
var axiosInstance = contentstackHTTPClient({
234+
apiKey: 'apiKey',
235+
accessToken: 'accessToken',
236+
defaultHostName: 'gcp-na-api.contentstack.com',
237+
region: 'gcp-na'
238+
})
239+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://gcp-na-app.contentstack.com', 'GCP NA UI base URL should match')
240+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('gcp-na-developerhub-api.contentstack.com', 'GCP NA developer hub should match')
241+
done()
242+
})
243+
244+
it('should configure endpoints for GCP EU region', done => {
245+
var axiosInstance = contentstackHTTPClient({
246+
apiKey: 'apiKey',
247+
accessToken: 'accessToken',
248+
defaultHostName: 'gcp-eu-api.contentstack.com',
249+
region: 'gcp-eu'
250+
})
251+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://gcp-eu-app.contentstack.com', 'GCP EU UI base URL should match')
252+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('gcp-eu-developerhub-api.contentstack.com', 'GCP EU developer hub should match')
253+
done()
254+
})
255+
256+
it('should include https protocol in developer hub base URL', done => {
257+
var axiosInstance = contentstackHTTPClient({
258+
apiKey: 'apiKey',
259+
accessToken: 'accessToken',
260+
defaultHostName: 'api.contentstack.io',
261+
region: 'na'
262+
})
263+
expect(axiosInstance.defaults.developerHubBaseUrl).to.match(/^https:\/\//, 'Developer hub URL should start with https://')
264+
done()
265+
})
266+
267+
it('should include https protocol in UI base URL', done => {
268+
var axiosInstance = contentstackHTTPClient({
269+
apiKey: 'apiKey',
270+
accessToken: 'accessToken',
271+
defaultHostName: 'api.contentstack.io',
272+
region: 'na'
273+
})
274+
expect(axiosInstance.defaults.uiBaseUrl).to.match(/^https:\/\//, 'UI base URL should start with https://')
275+
done()
276+
})
277+
278+
it('should configure UI base URL with protocol for NA region', done => {
279+
var axiosInstance = contentstackHTTPClient({
280+
apiKey: 'apiKey',
281+
accessToken: 'accessToken',
282+
defaultHostName: 'api.contentstack.io',
283+
region: 'na'
284+
})
285+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://app.contentstack.com', 'NA UI base URL should include protocol')
286+
done()
287+
})
288+
289+
it('should configure UI base URL with protocol for EU region', done => {
290+
var axiosInstance = contentstackHTTPClient({
291+
apiKey: 'apiKey',
292+
accessToken: 'accessToken',
293+
defaultHostName: 'eu-api.contentstack.com',
294+
region: 'eu'
295+
})
296+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://eu-app.contentstack.com', 'EU UI base URL should include protocol')
297+
done()
298+
})
299+
300+
it('should handle region aliases when configuring endpoints', done => {
301+
var axiosInstance = contentstackHTTPClient({
302+
apiKey: 'apiKey',
303+
accessToken: 'accessToken',
304+
defaultHostName: 'api.contentstack.io',
305+
region: 'us'
306+
})
307+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://app.contentstack.com', 'US alias should map to NA endpoints')
308+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('developerhub-api.contentstack.com', 'US alias should map to NA developer hub')
309+
done()
310+
})
311+
312+
it('should handle azure_na alias when configuring endpoints', done => {
313+
var axiosInstance = contentstackHTTPClient({
314+
apiKey: 'apiKey',
315+
accessToken: 'accessToken',
316+
defaultHostName: 'azure-na-api.contentstack.com',
317+
region: 'azure_na'
318+
})
319+
expect(axiosInstance.defaults.uiBaseUrl).to.be.equal('https://azure-na-app.contentstack.com', 'azure_na alias should work')
320+
expect(axiosInstance.defaults.developerHubBaseUrl).to.contain('azure-na-developerhub-api.contentstack.com', 'azure_na alias should work for developer hub')
321+
done()
322+
})
323+
324+
it('should configure region property in config', done => {
325+
var axiosInstance = contentstackHTTPClient({
326+
apiKey: 'apiKey',
327+
accessToken: 'accessToken',
328+
defaultHostName: 'api.contentstack.io',
329+
region: 'eu'
330+
})
331+
expect(axiosInstance.defaults.region).to.be.equal('eu', 'Region should be stored in defaults')
332+
done()
333+
})
334+
})
170335
})

0 commit comments

Comments
 (0)