Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
feat: add option to fetch content available in all files (#27, fix #22)
Browse files Browse the repository at this point in the history
* add missing doc

* feat: add common content to all entries

no tests yet, just proof of concept

refs #22

* test for previous commit's work on #22

* add docs for common queries

refs #22

also fix up older doc a lil bit

* improve comments, refs #22

* provide a template usage example and fix syntax in other example

* use options directly instead of useless extra var definitions

* use similar arg ordering to other functions

* note about hbs syntax

* transform the query just for convenience
  • Loading branch information
bnchdrff authored and stefanjudis committed Sep 22, 2016
1 parent 50d316c commit 668ea79
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
28 changes: 27 additions & 1 deletion docs/global-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ You can then use the keyword `__NOW__` in your source file's `filter` values, li

```markdown
---
title: Post overview of entries including "rabbit"
title: Articles with certain start and end dates
contentful:
content_type: post
filter:
Expand All @@ -81,3 +81,29 @@ contentful:
layout: posts.html
---
```

### `common` *(optional)*

The results of queries placed in this property will be made available in all templates

For example, find your five latest entries so you can include them in various templates:

```javascript
{
// ...
"common": {
"latest": {
"content_type": "post",
"limit": 5,
"order": "sys.createdAt"
}
},
// ...
}
```

In templates you can then access `common.latest` to get the raw results of the above query:

```
The first latest post's title, in handlebars syntax: {{ common.latest.items.0.fields.title }}
```
44 changes: 44 additions & 0 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const clients = {}
*
* @param {String} accessToken access token
* @param {String} spaceId space id
* @param {String} host host
*
* @return {Object} contentful client
*/
Expand All @@ -25,6 +26,48 @@ function getContentfulClient (accessToken, spaceId, host) {
return clients[spaceId]
}

/**
* Fetch common content for a certain space.
*
* @param {Object} entries entries fetched from contentful
* @param {Object} options plugin config
*
* @return {Object} file mapping object with common content added
*/
function getCommonContentForSpace (entries, options) {
if (!options.common) {
return entries
}

const client = getContentfulClient(options.access_token, options.space_id, options.host)

const commonQueries = []
const commonIds = []

for (let id in options.common) {
commonQueries.push(client.getEntries(util.getEntriesQuery(options.common[id], options.filterTransforms)))
commonIds.push(id)
}

// First, execute all common queries.
return Promise.all(commonQueries).then(commonContent => {
return new Promise((resolve, reject) => {
// Store the results in an object using the configured IDs.
const commonsObj = commonIds.reduce((prev, curr, index) => {
prev[curr] = commonContent[index]
return prev
}, {})

// Assign common query results to each entry.
for (let entry in entries) {
entries[entry].common = commonsObj
}

resolve(entries)
})
})
}

/**
* Enrich all fetched entries with additional properties
*
Expand Down Expand Up @@ -121,6 +164,7 @@ function processFile (file, options) {
return client.getEntries(query)
.then(entries => mapEntriesForFile(entries.items, file, options))
.then(entries => processEntriesForFile(file, entries))
.then(entries => getCommonContentForSpace(entries, options))
}

module.exports = {
Expand Down
62 changes: 62 additions & 0 deletions lib/processor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,65 @@ test('processor.processFile - resolves correctly for multiple entries', t => {
)
})
})

test('processor.getCommonContentForSpace - load a set of common content', t => {
const options = {
access_token: 'global-bar',
host: 'global-baz',
space_id: 'global-foo',
common: {
doublets: {
filter: 'mulledwine'
}
}
}

const entriesToBeReturned = [
{
sys: {
id: 'bazinga',
contentType: {
sys: {
id: 'boing'
}
}
},
fields: {
name: 'John Doe'
}
},
{
sys: {
id: 'badabum',
contentType: {
sys: {
id: 'kazum'
}
}
},
fields: {
name: 'Jane Doe'
}
}
]

const processor = proxyquire(
'./processor',
{
contentful: {
createClient: function () {
return {
getEntries: function () {
return new Promise(resolve => resolve({items: entriesToBeReturned}))
}
}
}
}
}
)

processor.processFile({ contentful: {}, _fileName: 'turnip.html' }, options)
.then(fileMap => {
t.is(fileMap['turnip.html'].common.doublets, entriesToBeReturned)
})
})

0 comments on commit 668ea79

Please sign in to comment.