Skip to content

Commit

Permalink
Merge pull request #66 from contentful/stringify-sync
Browse files Browse the repository at this point in the history
feat: Add stringifySafe and toPlainObject to sync responses
  • Loading branch information
trodrigues committed Mar 18, 2016
2 parents 9563a59 + 32a7a56 commit 2b84d59
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {filter} from 'lodash/collection'
import {cloneDeep} from 'lodash/lang'
import createRequestConfig from './create-request-config'
import mixinLinkGetters from './mixins/link-getters'
import mixinStringifySafe from './mixins/stringify-safe'
import mixinToPlainObject from './mixins/to-plain-object'

/**
* @memberof Sync
Expand All @@ -15,6 +17,8 @@ import mixinLinkGetters from './mixins/link-getters'
* @prop {Array<Sync.DeletedEntry>} deletedEntries - List of deleted Entries since last sync
* @prop {Array<Sync.DeletedAsset>} deletedAssets - List of deleted Assets since last sync
* @prop {string} nextSyncToken - Token to be sent to the next sync call
* @prop {function(): Object} toPlainObject() - Returns this Sync collection as a plain JS object
* @prop {function(?function=, space=): Object} stringifySafe(replacer,space) - Stringifies the Sync 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.
*/

/**
Expand Down Expand Up @@ -65,7 +69,7 @@ export default function pagedSync (http, query, resolveLinks) {
// maps response items again after getters are attached
const mappedResponseItems = mapResponseItems(response.items)
mappedResponseItems.nextSyncToken = response.nextSyncToken
return Object.freeze(mappedResponseItems)
return Object.freeze(mixinStringifySafe(mixinToPlainObject(mappedResponseItems)))
}, error => {
throw error.data
})
Expand Down
14 changes: 8 additions & 6 deletions test/unit/sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test('Initial sync with one page and filter', t => {
})

test('Initial sync with multiple pages', t => {
t.plan(8)
t.plan(9)
const http = {get: sinon.stub()}
http.get.withArgs('sync', {params: {initial: true}}).returns(Promise.resolve({
data: {
Expand Down Expand Up @@ -148,14 +148,16 @@ test('Initial sync with multiple pages', t => {

return pagedSync(http, {initial: true}, true)
.then(response => {
const objResponse = response.toPlainObject()
t.ok(http.get.args[0][1].params.initial, 'http request has initial param')
t.equal(http.get.args[1][1].params.sync_token, 'nextpage1', 'http request param for first page')
t.equal(http.get.args[2][1].params.sync_token, 'nextpage2', 'http request param for second page')
t.equal(response.entries.length, 3, 'entries length')
t.equal(response.deletedEntries.length, 2, 'deleted entries length')
t.equal(response.assets.length, 3, 'entries length')
t.equal(response.deletedAssets.length, 1, 'deleted assets length')
t.equal(response.nextSyncToken, 'nextsynctoken', 'next sync token')
t.equal(objResponse.entries.length, 3, 'entries length')
t.equal(objResponse.deletedEntries.length, 2, 'deleted entries length')
t.equal(objResponse.assets.length, 3, 'entries length')
t.equal(objResponse.deletedAssets.length, 1, 'deleted assets length')
t.equal(objResponse.nextSyncToken, 'nextsynctoken', 'next sync token')
t.ok(response.stringifySafe(), 'stringifies response')
})
})

Expand Down

0 comments on commit 2b84d59

Please sign in to comment.