diff --git a/lib/contentful.js b/lib/contentful.js index 270429fd9..9d8caf3b4 100644 --- a/lib/contentful.js +++ b/lib/contentful.js @@ -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. @@ -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 }) } diff --git a/test/unit/contentful-test.js b/test/unit/contentful-test.js index b7591fabb..e25e2e1cc 100644 --- a/test/unit/contentful-test.js +++ b/test/unit/contentful-test.js @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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() +})