Skip to content

Commit 00fc709

Browse files
committed
feat: Add stringifySafe method to entry collections
Entry collections can potentially have circular references when using Links. This method allows a collection to be stringified safely.
1 parent c3a8da1 commit 00fc709

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

lib/entities/entry.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {cloneDeep} from 'lodash/lang'
22
import {uniq} from 'lodash/array'
33
import mixinToPlainObject from '../mixins/to-plain-object'
44
import mixinLinkGetters from '../mixins/link-getters'
5+
import mixinStringifySafe from '../mixins/stringify-safe'
56

67
/**
78
* @memberof Entities
@@ -30,6 +31,7 @@ export function wrapEntry (data) {
3031
* @prop {number} limit
3132
* @prop {Array<Entities.Entry>} items
3233
* @prop {function(): Object} toPlainObject() - Returns this Entry collection as a plain JS object
34+
* @prop {function(?function=, space=): Object} stringifySafe(replacer,space) - Stringifies the entry collection, accounting for circular references. Circular references will be replaced with just a Link object, with a <code>circular</code> property set to <code>true</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify">MDN</a> and <a href="https://www.npmjs.com/package/json-stringify-safe">json-stringify-safe</a> for more details on the arguments this method can take.
3335
*/
3436

3537
/**
@@ -39,7 +41,7 @@ export function wrapEntry (data) {
3941
* @return {EntryCollection} Wrapped entry collection data
4042
*/
4143
export function wrapEntryCollection (data, resolveLinks) {
42-
const wrappedData = mixinToPlainObject(cloneDeep(data))
44+
const wrappedData = mixinStringifySafe(mixinToPlainObject(cloneDeep(data)))
4345
if (resolveLinks) {
4446
const includes = prepareIncludes(wrappedData.includes, wrappedData.items)
4547
mixinLinkGetters(wrappedData.items, includes)

lib/mixins/stringify-safe.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import jsonStringifySafe from 'json-stringify-safe'
2+
3+
export default function mixinStringifySafe (data) {
4+
return Object.defineProperty(data, 'stringifySafe', {
5+
enumerable: false,
6+
configurable: false,
7+
writable: false,
8+
value: function (serializer = null, indent = '') {
9+
return jsonStringifySafe(this, serializer, indent, (key, value) => {
10+
return {
11+
sys: {
12+
type: 'Link',
13+
linkType: 'Entry',
14+
id: value.sys.id,
15+
circular: true
16+
}
17+
}
18+
})
19+
}
20+
})
21+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"dependencies": {
5050
"babel-runtime": "^6.3.19",
5151
"follow-redirects": "0.0.7",
52+
"json-stringify-safe": "^5.0.1",
5253
"lodash": "^4.2.0"
5354
},
5455
"devDependencies": {

test/unit/entities/entry-test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test('Entry collection is wrapped', t => {
3737
t.end()
3838
})
3939

40-
test.only('Entry collection links are resolved', t => {
40+
test('Entry collection links are resolved', t => {
4141
const entryCollection = {
4242
total: 1,
4343
skip: 0,
@@ -87,7 +87,9 @@ test.only('Entry collection links are resolved', t => {
8787
}
8888
}
8989

90-
const wrappedEntry = wrapEntryCollection(entryCollection, true).toPlainObject()
90+
const wrappedCollection = wrapEntryCollection(entryCollection, true)
91+
const wrappedEntry = wrappedCollection.toPlainObject()
92+
9193
// first linked entry resolved from includes
9294
t.equals(wrappedEntry.items[0].fields.linked1.sys.type, 'Asset', 'first linked entity is resolved')
9395
t.ok(wrappedEntry.items[0].fields.linked1.fields, 'first linked entity has fields')
@@ -96,5 +98,6 @@ test.only('Entry collection links are resolved', t => {
9698
t.ok(wrappedEntry.items[0].fields.linked2.fields, 'second linked entity has fields')
9799
t.equals(wrappedEntry.items[1].fields.linked1.sys.type, 'Entry', 'third linked entity is resolved')
98100
t.ok(wrappedEntry.items[1].fields.linked1.fields, 'third linked entity has fields')
101+
t.ok(wrappedCollection.stringifySafe(), 'stringifies safely')
99102
t.end()
100103
})

0 commit comments

Comments
 (0)