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
8 changes: 8 additions & 0 deletions lib/contentstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ import httpClient from './core/contentstackHTTPClient.js'
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'value' })
*
* @prop {string=} params.early_access - Optional early_access is a token used for early access of new features in CMA requests.
* @example //Set the `early_access`
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ early_access: ['ea1', 'ea2'] })
*
* @prop {string=} params.authorization - Optional authorization token is a read-write token used to make authorized CMA requests, but it is a user-specific token.
* @example //Set the `authorization`
* import * as contentstack from '@contentstack/management'
Expand Down Expand Up @@ -177,6 +182,9 @@ export function client (params = {}) {
if (params.authorization) {
requiredHeaders.authorization = params.authorization
}
if (params.early_access) {
requiredHeaders.early_access = params.early_access.join(',')
}
params = {
...defaultParameter,
...clonedeep(params)
Expand Down
4 changes: 4 additions & 0 deletions lib/core/contentstackHTTPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export default function contentstackHttpClient (options) {
config.headers['accessToken'] = config.accessToken
}

if (config.early_access) {
config.headers['x-header-ea'] = config.early_access
}

const protocol = config.insecure ? 'http' : 'https'
let hostname = config.defaultHostName
let port = config.port || 443
Expand Down
13 changes: 13 additions & 0 deletions test/unit/ContentstackHTTPClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@ describe('Contentstack HTTP Client', () => {
expect(client.defaults.retryCondition('error')).to.be.equal(true)
done()
})
it('should add x-header-ea in headers when early_access is passed', done => {
var axiosInstance = contentstackHTTPClient(
{
apiKey: 'apiKey',
accessToken: 'accessToken',
early_access: 'ea1,ea2'
})

expect(axiosInstance.defaults.headers.apiKey).to.be.equal('apiKey', 'Api not Equal to \'apiKey\'')
expect(axiosInstance.defaults.headers.accessToken).to.be.equal('accessToken', 'Api not Equal to \'accessToken\'')
expect(axiosInstance.defaults.headers['x-header-ea']).to.be.equal('ea1,ea2')
done()
})
})
11 changes: 11 additions & 0 deletions test/unit/contentstack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ describe('Contentstack HTTP Client', () => {
createClientRewireApi.__ResetDependency__('contentstackClient')
done()
})
it('should have valid format of early_access headers when early_access is passed', done => {
createClientRewireApi.__Rewire__('client', { create: sinon.stub() })
const createHttpClientStub = sinon.stub()
createClientRewireApi.__Rewire__('httpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('contentstackClient', sinon.stub().returns({}))
client({ early_access: ['ea1', 'ea2'] })
expect(createHttpClientStub.args[0][0].headers.early_access).to.be.eql('ea1,ea2', 'Early access does not match')
createClientRewireApi.__ResetDependency__('httpClient')
createClientRewireApi.__ResetDependency__('contentstackClient')
done()
})
})