Skip to content

Commit a8fc7ac

Browse files
Merge pull request #94 from contentstack/feat/early-access-headers
feat: early access headers implementation
2 parents cae9bdc + 40dcffd commit a8fc7ac

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

lib/contentstack.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ import httpClient from './core/contentstackHTTPClient.js'
3939
* import * as contentstack from '@contentstack/management'
4040
* const client = contentstack.client({ authtoken: 'value' })
4141
*
42+
* @prop {string=} params.early_access - Optional early_access is a token used for early access of new features in CMA requests.
43+
* @example //Set the `early_access`
44+
* import * as contentstack from '@contentstack/management'
45+
* const client = contentstack.client({ early_access: ['ea1', 'ea2'] })
46+
*
4247
* @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.
4348
* @example //Set the `authorization`
4449
* import * as contentstack from '@contentstack/management'
@@ -177,6 +182,9 @@ export function client (params = {}) {
177182
if (params.authorization) {
178183
requiredHeaders.authorization = params.authorization
179184
}
185+
if (params.early_access) {
186+
requiredHeaders.early_access = params.early_access.join(',')
187+
}
180188
params = {
181189
...defaultParameter,
182190
...clonedeep(params)

lib/core/contentstackHTTPClient.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export default function contentstackHttpClient (options) {
4444
config.headers['accessToken'] = config.accessToken
4545
}
4646

47+
if (config.early_access) {
48+
config.headers['x-header-ea'] = config.early_access
49+
}
50+
4751
const protocol = config.insecure ? 'http' : 'https'
4852
let hostname = config.defaultHostName
4953
let port = config.port || 443

test/unit/ContentstackHTTPClient-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,17 @@ describe('Contentstack HTTP Client', () => {
154154
expect(client.defaults.retryCondition('error')).to.be.equal(true)
155155
done()
156156
})
157+
it('should add x-header-ea in headers when early_access is passed', done => {
158+
var axiosInstance = contentstackHTTPClient(
159+
{
160+
apiKey: 'apiKey',
161+
accessToken: 'accessToken',
162+
early_access: 'ea1,ea2'
163+
})
164+
165+
expect(axiosInstance.defaults.headers.apiKey).to.be.equal('apiKey', 'Api not Equal to \'apiKey\'')
166+
expect(axiosInstance.defaults.headers.accessToken).to.be.equal('accessToken', 'Api not Equal to \'accessToken\'')
167+
expect(axiosInstance.defaults.headers['x-header-ea']).to.be.equal('ea1,ea2')
168+
done()
169+
})
157170
})

test/unit/contentstack-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,15 @@ describe('Contentstack HTTP Client', () => {
9797
createClientRewireApi.__ResetDependency__('contentstackClient')
9898
done()
9999
})
100+
it('should have valid format of early_access headers when early_access is passed', done => {
101+
createClientRewireApi.__Rewire__('client', { create: sinon.stub() })
102+
const createHttpClientStub = sinon.stub()
103+
createClientRewireApi.__Rewire__('httpClient', createHttpClientStub)
104+
createClientRewireApi.__Rewire__('contentstackClient', sinon.stub().returns({}))
105+
client({ early_access: ['ea1', 'ea2'] })
106+
expect(createHttpClientStub.args[0][0].headers.early_access).to.be.eql('ea1,ea2', 'Early access does not match')
107+
createClientRewireApi.__ResetDependency__('httpClient')
108+
createClientRewireApi.__ResetDependency__('contentstackClient')
109+
done()
110+
})
100111
})

0 commit comments

Comments
 (0)