Skip to content

Commit

Permalink
feat(environments): add environments option
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Rötsch authored and axe312ger committed Feb 27, 2018
1 parent 2cc40fe commit 7733912
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
40 changes: 28 additions & 12 deletions lib/contentful.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import createGlobalOptions from './create-global-options'
* @param {Object} params - Client initialization parameters
* @prop {string} params.space - Space ID
* @prop {string} params.accessToken - Contentful CDA Access Token
* @prop {string} [params.environment="master"] - Contentful Environment ID
* @prop {boolean=} params.insecure - Requests will be made over http instead of the default https (default: true)
* @prop {string=} params.host - API host (default: cdn.contentful.com). Also usable with preview.contentful.com.
* @prop {string=} params.basePath - Path appended to the host to support gateways/proxies with custom urls.
Expand Down Expand Up @@ -50,26 +51,41 @@ export function createClient (params) {
throw new TypeError('Expected parameter space')
}

// Use resolveLinks param if specified, otherwise default to true
const resolveLinks = !!('resolveLinks' in params ? params.resolveLinks : true)
const removeUnresolved = !!params.removeUnresolved
const defaultConfig = {
resolveLinks: true,
removeUnresolved: false,
environment: 'master'
}

const config = {
...defaultConfig,
...params
}

config.defaultHostname = 'cdn.contentful.com'

const getGlobalOptions = createGlobalOptions({
resolveLinks: config.resolveLinks,
removeUnresolved: config.removeUnresolved
})

const getGlobalOptions = createGlobalOptions({resolveLinks, removeUnresolved})
const userAgentHeader = getUserAgentHeader(`contentful.js/${__VERSION__}`,
params.application,
params.integration
config.application,
config.integration
)
params.defaultHostname = 'cdn.contentful.com'
params.headers = {
...params.headers,
config.headers = {
...config.headers,
'Content-Type': 'application/vnd.contentful.delivery.v1+json',
'X-Contentful-User-Agent': userAgentHeader
}

const http = createHttpClient(axios, params)
const http = createHttpClient(axios, config)

// Append environment to baseURL
http.defaults.baseURL = `${http.defaults.baseURL}/environments/${config.environment}`

return createContentfulApi({
http: http,
getGlobalOptions: getGlobalOptions
http,
getGlobalOptions
})
}
72 changes: 66 additions & 6 deletions test/unit/contentful-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ test('Throws if no space is defined', (t) => {
})
test('Generate the correct User Agent Header', (t) => {
createClientRewireApi.__Rewire__('@contentful/axios', sinon.stub)
const createHttpClientStub = sinon.stub()
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
Expand All @@ -40,7 +44,11 @@ test('Generate the correct User Agent Header', (t) => {
test('Passes along HTTP client parameters', (t) => {
createClientRewireApi.__Rewire__('@contentful/axios', sinon.stub)
createClientRewireApi.__Rewire__('version', 'version')
const createHttpClientStub = sinon.stub()
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
Expand All @@ -54,7 +62,11 @@ test('Passes along HTTP client parameters', (t) => {
})

test('Returns a client instance', (t) => {
const createHttpClientStub = sinon.stub()
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
Expand All @@ -72,7 +84,11 @@ test('Returns a client instance', (t) => {
})

test('Initializes API with link resolution turned on by default', (t) => {
const createHttpClientStub = sinon.stub()
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
Expand All @@ -87,7 +103,11 @@ test('Initializes API with link resolution turned on by default', (t) => {
})

test('Initializes API with link resolution turned on explicitly', (t) => {
const createHttpClientStub = sinon.stub()
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
Expand All @@ -102,7 +122,11 @@ test('Initializes API with link resolution turned on explicitly', (t) => {
})

test('Initializes API with link resolution turned off explicitly', (t) => {
const createHttpClientStub = sinon.stub()
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
Expand All @@ -114,3 +138,39 @@ test('Initializes API with link resolution turned off explicitly', (t) => {
createClientRewireApi.__ResetDependency__('rateLimit')
t.end()
})

test('Initializes API and attaches default environment', (t) => {
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
const apiStub = sinon.stub().returns({})
createClientRewireApi.__Rewire__('createContentfulApi', apiStub)
createClient({accessToken: 'accesstoken', space: 'spaceid'})
t.is(apiStub.args[0][0].http.defaults.baseURL, 'http://some-base-url.com/environments/master')
createClientRewireApi.__ResetDependency__('createHttpClient')
createClientRewireApi.__ResetDependency__('rateLimit')
t.end()
})

test('Initializes API and attaches custom environment', (t) => {
const createHttpClientStub = sinon.stub().returns({
defaults: {
baseURL: 'http://some-base-url.com'
}
})
const rateLimitStub = sinon.stub()
createClientRewireApi.__Rewire__('createHttpClient', createHttpClientStub)
createClientRewireApi.__Rewire__('rateLimit', rateLimitStub)
const apiStub = sinon.stub().returns({})
createClientRewireApi.__Rewire__('createContentfulApi', apiStub)
createClient({accessToken: 'accesstoken', space: 'spaceid', environment: 'stage'})
t.is(apiStub.args[0][0].http.defaults.baseURL, 'http://some-base-url.com/environments/stage')
createClientRewireApi.__ResetDependency__('createHttpClient')
createClientRewireApi.__ResetDependency__('rateLimit')
t.end()
})

0 comments on commit 7733912

Please sign in to comment.