diff --git a/lib/contentstack.js b/lib/contentstack.js index 5ecb13fa..cf716495 100644 --- a/lib/contentstack.js +++ b/lib/contentstack.js @@ -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' @@ -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) diff --git a/lib/core/contentstackHTTPClient.js b/lib/core/contentstackHTTPClient.js index 1ad77e1a..30f8dfad 100644 --- a/lib/core/contentstackHTTPClient.js +++ b/lib/core/contentstackHTTPClient.js @@ -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 diff --git a/test/unit/ContentstackHTTPClient-test.js b/test/unit/ContentstackHTTPClient-test.js index 12e3a28d..1b78a290 100644 --- a/test/unit/ContentstackHTTPClient-test.js +++ b/test/unit/ContentstackHTTPClient-test.js @@ -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() + }) }) diff --git a/test/unit/contentstack-test.js b/test/unit/contentstack-test.js index 18e0bd2c..067d6460 100644 --- a/test/unit/contentstack-test.js +++ b/test/unit/contentstack-test.js @@ -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() + }) })