Skip to content

Commit

Permalink
Merge pull request #94 from contentstack/feat/early-access-headers
Browse files Browse the repository at this point in the history
feat: early access headers implementation
  • Loading branch information
harshithad0703 authored Nov 16, 2023
2 parents 9344c09 + 9ecb16f commit 3d0c78a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
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()
})
})

0 comments on commit 3d0c78a

Please sign in to comment.