Skip to content

Commit bdd0254

Browse files
committed
Add isData alongside isIncluded (servers can return objects in both parts).
1 parent 7a63d49 commit bdd0254

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

src/lib.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,20 @@ const Utils = class {
361361
* Convert JSONAPI record(s) to restructured data
362362
* @memberof module:jsonapi-vuex.utils
363363
* @param {object} data - The `data` object from a JSONAPI record
364-
* @param {boolean} isIncluded - Flag which if true, will mark these records as coming from 'included' not via 'data'
365364
* @return {object} Restructured data
366365
*/
367-
jsonapiToNorm(data, isIncluded) {
366+
jsonapiToNorm(data) {
368367
const norm = {}
369368
if (Array.isArray(data)) {
370369
data.forEach((item) => {
371370
let { id } = item
372371
if (!this.hasProperty(norm, id)) {
373372
norm[id] = {}
374373
}
375-
Object.assign(norm[id], this.jsonapiToNormItem(item, isIncluded))
374+
Object.assign(norm[id], this.jsonapiToNormItem(item))
376375
})
377376
} else {
378-
Object.assign(norm, this.jsonapiToNormItem(data, isIncluded))
377+
Object.assign(norm, this.jsonapiToNormItem(data))
379378
}
380379
return norm
381380
}
@@ -384,10 +383,10 @@ const Utils = class {
384383
* Restructure a single jsonapi item. Used internally by {@link module:jsonapi-vuex.utils.jsonapiToNorm}
385384
* @memberof module:jsonapi-vuex._internal
386385
* @param {object} data - JSONAPI record
387-
* @param {boolean} isIncluded - Flag, which if true will mark this record as coming from 'included', not via 'data'
386+
* @param {boolean} recordType=isData - Set a key in _jv which reflects if this came 'direct' from 'data' or via 'included'
388387
* @return {object} Restructured data
389388
*/
390-
jsonapiToNormItem(data, isIncluded = false) {
389+
jsonapiToNormItem(data, recordType = 'isData') {
391390
if (!data) {
392391
return {}
393392
}
@@ -396,9 +395,8 @@ const Utils = class {
396395
// Create a new object omitting attributes
397396
const { attributes, ...normNoAttrs } = norm[this.jvtag] // eslint-disable-line no-unused-vars
398397
norm[this.jvtag] = normNoAttrs
399-
if (isIncluded) {
400-
norm[this.jvtag].isIncluded = isIncluded
401-
}
398+
// Set recordType (either isData or isIncluded) to true
399+
norm[this.jvtag][recordType] = true
402400
return norm
403401
}
404402

@@ -504,9 +502,8 @@ const Utils = class {
504502
*/
505503
processIncludedRecords(context, results) {
506504
for (let item of get(results, ['data', 'included'], [])) {
507-
//Mark record as coming from included
508-
let isIncluded = true
509-
const includedItem = this.jsonapiToNormItem(item, isIncluded)
505+
// Mark record as coming from included
506+
const includedItem = this.jsonapiToNormItem(item, 'isIncluded')
510507
context.commit('mergeRecords', includedItem)
511508
}
512509
}

tests/unit/actions/get.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ describe('get', function () {
114114
// for a real API call, would need axios include params here
115115
await jsonapiModule.actions.get(stubContext, normWidget1)
116116

117+
// Add isIncluded, remove isData (As would be found in 'real' response)
117118
normWidget2._jv.isIncluded = true
118119
normMachine1._jv.isIncluded = true
120+
delete normWidget2._jv.isData
121+
delete normMachine1._jv.isData
119122
expect(stubContext.commit).to.have.been.calledWith('mergeRecords', normWidget2)
120123
expect(stubContext.commit).to.have.been.calledWith('mergeRecords', normMachine1)
121124
})

tests/unit/fixtures/widget1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function normFormat() {
5555
foo: 1,
5656
bar: 'baz',
5757
_jv: {
58+
isData: true,
5859
type: 'widget',
5960
id: '1',
6061
links: {
@@ -85,6 +86,7 @@ export function normFormatPatch() {
8586
return {
8687
foo: 'update',
8788
_jv: {
89+
isData: true,
8890
type: 'widget',
8991
id: '1',
9092
},
@@ -96,6 +98,7 @@ export function normFormatUpdate() {
9698
foo: 'update',
9799
bar: 'baz',
98100
_jv: {
101+
isData: true,
99102
type: 'widget',
100103
id: '1',
101104
links: {

tests/unit/fixtures/widget2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function normFormat() {
2929
return {
3030
foo: 2,
3131
_jv: {
32+
isData: true,
3233
type: 'widget',
3334
id: '2',
3435
relationships: {

tests/unit/fixtures/widget3.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function normFormat() {
2222
return {
2323
foo: 3,
2424
_jv: {
25+
isData: true,
2526
type: 'widget',
2627
id: '3',
2728
relationships: {

tests/unit/jsonapi-vuex.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ describe('jsonapi-vuex tests', function () {
256256
})
257257
describe('utils.cleanPatch', function () {
258258
it('should return patch unmodified if record not in state', function () {
259+
// Remove isData as clean Patch will have removed it
260+
delete normWidget1Patch._jv.isData
259261
const res = utils.cleanPatch(normWidget1Patch, {})
260262
expect(res).to.deep.equal(normWidget1Patch)
261263
})
@@ -357,9 +359,10 @@ describe('jsonapi-vuex tests', function () {
357359
expect(utils.jsonapiToNormItem(jsonWidget1)).to.deep.equal(normWidget1)
358360
})
359361
it("should set the 'isIncluded' property if isIncluded param is true", function () {
360-
// Set inData param to true
362+
// Set isIncluded param to true, drop isData
361363
normWidget1._jv.isIncluded = true
362-
expect(utils.jsonapiToNormItem(jsonWidget1, true)).to.deep.equal(normWidget1)
364+
delete normWidget1._jv.isData
365+
expect(utils.jsonapiToNormItem(jsonWidget1, 'isIncluded')).to.deep.equal(normWidget1)
363366
})
364367
})
365368

0 commit comments

Comments
 (0)