From 61e11b651f7c6bfbe16ce213a842dc82a1c1294c Mon Sep 17 00:00:00 2001 From: Ron Brunninkhuis Date: Thu, 28 Jun 2018 11:35:34 +0200 Subject: [PATCH] Add environment option Contentful has made it possible to add environments to your space. With this addition, contentful-metalsmith can make use of this. --- lib/processor.js | 20 ++++++++++---------- package.json | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/processor.js b/lib/processor.js index c077a6c..6d0c47c 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -12,15 +12,17 @@ const clients = {} * @param {String} accessToken access token * @param {String} spaceId space id * @param {String} host host + * @param {String} environment environment * * @return {Object} contentful client */ -function getContentfulClient (accessToken, spaceId, host) { +function getContentfulClient (accessToken, spaceId, host, environment = 'master') { if (!clients[spaceId]) { clients[spaceId] = contentful.createClient({ space: spaceId, accessToken, - host + host, + environment, }) } @@ -40,7 +42,7 @@ function getCommonContentForSpace (entries, options) { return entries } - const client = getContentfulClient(options.access_token, options.space_id, options.host) + const client = getContentfulClient(options.access_token, options.space_id, options.host, options.environment) const commonQueries = [] const commonIds = [] @@ -127,7 +129,8 @@ function processEntriesForFile (file, entries, options) { data: entry, id: entry.sys.id, contentType: contentfulOptions.content_type, - layout: contentfulOptions.entry_template, + view: contentfulOptions.entry_template, + locale: contentfulOptions.locale, _fileName: entry._fileName, _parentFileName: file._fileName @@ -158,17 +161,14 @@ function processFile (file, options) { const spaceId = file.contentful.space_id || options.space_id const accessToken = file.contentful.access_token || options.access_token + const environment = file.contentful.environment || options.environment const host = file.contentful.host || options.host const query = util.getEntriesQuery(file.contentful, options.filterTransforms) - const client = getContentfulClient(accessToken, spaceId, host) - - if (file._fileName) { - debug('Query for', file._fileName, '->', query) - } + const client = getContentfulClient(accessToken, spaceId, host, environment) return client.getEntries(query) - .catch(error => decorateAPIError(error, file)) + .catch(error => (decorateAPIError(error, file))) .then(entries => mapEntriesForFile(entries.items, file, options)) .then(entries => processEntriesForFile(file, entries, options)) .then(entries => getCommonContentForSpace(entries, options)) diff --git a/package.json b/package.json index 2a96d53..b953349 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "node": ">=4" }, "dependencies": { - "contentful": "^3.5.0", + "contentful": "^6.1.1", "debug": "^2.6.3", "lodash.pick": "^4.4.0", "lodash.template": "^4.4.0",