Skip to content

Commit

Permalink
fix(link-getter): Add a workaround for resolving links from preview e…
Browse files Browse the repository at this point in the history
…ndpoint

the extra locale property inside the sys of an item will cause the link resolution chain to be
broken and not resolve deeper entities

closes #80
  • Loading branch information
Khaledgarbaya committed Jun 28, 2016
1 parent f2cbb44 commit 467df4b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/create-contentful-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ export default function createContentfulApi ({
*/
function getEntries (query = {}) {
const resolveLinks = shouldLinksResolve(query)
const resolveForAllLocales = (query.locale && query.locale === '*')
return http.get('entries', createRequestConfig({query: query}))
.then((response) => wrapEntryCollection(response.data, resolveLinks), errorHandler)
.then((response) => wrapEntryCollection(response.data, resolveLinks, resolveForAllLocales), errorHandler)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/entities/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export function wrapEntry (data) {
* @param {Object} data - Raw entry collection data
* @return {EntryCollection} Wrapped entry collection data
*/
export function wrapEntryCollection (data, resolveLinks) {
export function wrapEntryCollection (data, resolveLinks, resolveForAllLocales) {
const wrappedData = mixinStringifySafe(mixinToPlainObject(cloneDeep(data)))
if (resolveLinks) {
const includes = prepareIncludes(wrappedData.includes, wrappedData.items)
mixinLinkGetters(wrappedData.items, includes)
mixinLinkGetters(wrappedData.items, includes, resolveForAllLocales)
}
return freezeSys(wrappedData)
}
Expand Down
11 changes: 9 additions & 2 deletions lib/mixins/link-getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import memoize from 'lodash/memoize'
* @param {Array<Entry|Asset|DeletedEntry|DeletedAsset>} items
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset
*/
export default function mixinLinkGetters (items, includes) {
export default function mixinLinkGetters (items, includes, resolveForAllLocales) {
const linkGetter = memoize(getLinksFromIncludes, memoizationResolver)
each(items, (item) => setLocalizedFieldGetters(item.fields, !!item.sys.locale))
each(items, (item) => {
// TODO: workaround the preview endpoint extra locale this should be removed when
// it is fixed on the backend
if (resolveForAllLocales && item.sys.locale) {
delete item.sys.locale
}
setLocalizedFieldGetters(item.fields, !!item.sys.locale)
})

/**
* If a field does not have a locale defined in sys, the content of that field
Expand Down
20 changes: 20 additions & 0 deletions test/integration/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const params = {
accessToken: 'b4c0n73n7fu1',
space: 'cfexampleapi'
}
const previewParams = {
host: 'preview.contentful.com',
accessToken: 'e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50',
space: 'cfexampleapi'
}

if (process.env.API_INTEGRATION_TESTS) {
params.host = '127.0.0.1:5000'
Expand All @@ -15,6 +20,8 @@ if (process.env.API_INTEGRATION_TESTS) {

const client = contentful.createClient(params)

const previewClient = contentful.createClient(previewParams)

test('Gets space', (t) => {
t.plan(3)
return client.getSpace()
Expand Down Expand Up @@ -115,6 +122,19 @@ test('Gets entries with linked includes', (t) => {
})
})

test('Gets entries with linked includes with preview', (t) => {
t.plan(5)
return previewClient.getEntries({locale: '*', include: 5, 'sys.id': 'nyancat'})
.then((response) => {
t.ok(response.includes, 'includes')
t.ok(response.includes.Asset, 'includes for Assets from preview endpoint')
t.ok(Object.keys(response.includes.Asset).length > 0, 'list of includes has asset items from preview endpoint')
// testing the fiels needs to be done first because it will call the getters and fill in bestFriend['en-US'].sys.type
t.ok(response.items[0].fields.bestFriend['en-US'].fields, 'resolved entry has fields from preview endpoint')
t.equal(response.items[0].fields.bestFriend['en-US'].sys.type, 'Entry', 'entry gets resolved from other entries in collection from preview endpoint')
})
})

test('Gets entries with content type query param', (t) => {
t.plan(2)
return client.getEntries({content_type: 'cat'})
Expand Down

0 comments on commit 467df4b

Please sign in to comment.