Skip to content

Region support added #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Region support added
  • Loading branch information
sunil-lakshman committed Apr 29, 2025
commit e6aea8bc228d03c405341b4523eb8d1325d62cbd
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v1.21.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.21.0) (2025-05-05)
- Enhancement
- Region support added

## [v1.20.3](https://github.com/contentstack/contentstack-management-javascript/tree/v1.20.3) (2025-04-21)
- Fix
- Handle the sanity tests when ENVs are not provided
Expand Down
27 changes: 25 additions & 2 deletions lib/contentstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
import getUserAgent from './core/Util.js'
import contentstackClient from './contentstackClient.js'
import httpClient from './core/contentstackHTTPClient.js'
const regionHostMap = {
'NA': 'api.contentstack.io',

Check failure on line 11 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'NA' found
'EU': 'eu-api.contentstack.com',

Check failure on line 12 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'EU' found
'AZURE_NA': 'azure-na-api.contentstack.com',

Check failure on line 13 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'AZURE_NA' found
'AZURE_EU': 'azure-eu-api.contentstack.com',

Check failure on line 14 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'AZURE_EU' found
'GCP_NA': 'gcp-na-api.contentstack.com',

Check failure on line 15 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'GCP_NA' found
'GCP_EU': 'gcp-eu-api.contentstack.com'

Check failure on line 16 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'GCP_EU' found
}

/**
* Create client instance
Expand Down Expand Up @@ -160,10 +168,25 @@
* @prop {string=} params.integration - Integration name and version e.g react/version
* @returns Contentstack.Client
*/
export function client (params = {}) {

Check failure on line 171 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Block must not be padded by blank lines
const defaultParameter = {
defaultHostName: 'api.contentstack.io'

let defaultHostName

if (params.region) {

Check failure on line 175 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 2 spaces but found 0
const region = params.region.toUpperCase()

Check failure on line 176 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2
if (!regionHostMap[region]) {

Check failure on line 177 in lib/contentstack.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 2
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 = {
defaultHostName: defaultHostName
}

const sdkAgent = `contentstack-management-javascript/${packages.version}`
const userAgentHeader = getUserAgent(sdkAgent,
Expand Down
3 changes: 3 additions & 0 deletions lib/core/contentstackHTTPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export default function contentstackHttpClient (options) {
let port = config.port || 443
const version = config.version || 'v3'

if (config.region) {
config.host = config.defaultHostName //set region on priority
}
if (isHost(config.host)) {
const parsed = config.host.split(':')
if (parsed.length === 2) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/management",
"version": "1.20.3",
"version": "1.21.0",
"description": "The Content Management API is used to manage the content of your Contentstack account",
"main": "./dist/node/contentstack-management.js",
"browser": "./dist/web/contentstack-management.js",
Expand Down
63 changes: 63 additions & 0 deletions test/sanity-check/api/user-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { contentstackClient } from '../../sanity-check/utility/ContentstackClien
import { jsonWrite } from '../../sanity-check/utility/fileOperations/readwrite'
import axios from 'axios'
import dotenv from 'dotenv'
import * as contentstack from '../../../lib/contentstack.js'

dotenv.config()
var authtoken = ''
Expand Down Expand Up @@ -74,4 +75,66 @@ describe('Contentstack User Session api Test', () => {
})
.catch(done)
})

it('should get host for NA region by default', done => {
const client = contentstack.client()
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly by default')
done()
})

it('should get host for NA region', done => {
const client = contentstack.client({ region: 'NA' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly')
done()
})

it('should get host for NA region on priority', done => {
const client = contentstack.client({ region: 'NA', host: 'dev11-api.csnonprod.com' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly with priority')
done()
})

it('should get custom host', done => {
const client = contentstack.client({ host: 'dev11-api.csnonprod.com' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('dev11-api.csnonprod.com', 'custom host set correctly')
done()
})

it('should get host for EU region', done => {
const client = contentstack.client({ region: 'EU' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('eu-api.contentstack.com', 'region EU set correctly')
done()
})

it('should get host for AZURE_NA region', done => {
const client = contentstack.client({ region: 'AZURE_NA' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('azure-na-api.contentstack.com', 'region AZURE_NA set correctly')
done()
})

it('should get host for GCP_NA region', done => {
const client = contentstack.client({ region: 'GCP_NA' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('gcp-na-api.contentstack.com', 'region GCP_NA set correctly')
done()
})


it('should throw error for invalid region', done => {
try {
contentstack.client({ region: 'DUMMYREGION' })
done(new Error('Expected error was not thrown for invalid region'))
} catch (error) {
expect(error.message).to.include('Invalid region', 'Error message should indicate invalid region')
done()
}
})


})
Loading