Skip to content

Commit cc8a223

Browse files
committed
Include self links in normalization
1 parent 81d7172 commit cc8a223

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/normalize.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ function extractEntities(json, { camelizeKeys }) {
6363
ret[type][elem.id].attributes = elem.attributes;
6464
}
6565

66+
if (elem.links) {
67+
ret[type][elem.id].links = {};
68+
69+
keys(elem.links).forEach((key) => {
70+
ret[type][elem.id].links[key] = elem.links[key];
71+
})
72+
}
73+
6674
if (elem.relationships) {
6775
ret[type][elem.id].relationships =
6876
extractRelationships(elem.relationships, { camelizeKeys });

test/normalize.spec.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('data is normalized', () => {
1111
attributes: {
1212
text: 'hello',
1313
number: 3,
14+
},
15+
links: {
16+
self: 'http://www.example.com/post/3'
1417
}
1518
},
1619
{
@@ -19,6 +22,9 @@ describe('data is normalized', () => {
1922
attributes: {
2023
text: 'hello world',
2124
number: 4,
25+
},
26+
links: {
27+
self: 'http://www.example.com/post/4'
2228
}
2329
}
2430
]
@@ -31,13 +37,19 @@ describe('data is normalized', () => {
3137
attributes: {
3238
text: 'hello',
3339
number: 3
40+
},
41+
links: {
42+
self: 'http://www.example.com/post/3'
3443
}
3544
},
3645
"4": {
3746
id: 4,
3847
attributes: {
3948
text: 'hello world',
4049
number: 4
50+
},
51+
links: {
52+
self: 'http://www.example.com/post/4'
4153
}
4254
}
4355
}
@@ -174,7 +186,7 @@ describe('relationships', () => {
174186
}
175187
}]
176188
};
177-
189+
178190
const output = {
179191
post: {
180192
"2620": {
@@ -211,7 +223,7 @@ describe('relationships', () => {
211223
}
212224
}]
213225
};
214-
226+
215227
const output = {
216228
post: {
217229
"2620": {
@@ -251,7 +263,7 @@ describe('relationships', () => {
251263
}
252264
}]
253265
};
254-
266+
255267
const output = {
256268
post: {
257269
"2620": {
@@ -294,7 +306,7 @@ describe('relationships', () => {
294306
}
295307
}]
296308
};
297-
309+
298310
const output = {
299311
post: {
300312
"2620": {
@@ -349,7 +361,7 @@ describe('relationships', () => {
349361
}
350362
}]
351363
};
352-
364+
353365
const output = {
354366
post: {
355367
"2620": {
@@ -407,7 +419,7 @@ describe('relationships', () => {
407419
}
408420
}]
409421
};
410-
422+
411423
const output = {
412424
post: {
413425
"2620": {
@@ -682,7 +694,7 @@ describe('meta', () => {
682694
}
683695
};
684696

685-
expect(isEqual(normalize(json3, { endpoint: 'posts/me' }), output3)).to.be.true;
697+
expect(isEqual(normalize(json3, { endpoint: 'posts/me' }), output3)).to.be.true;
686698
});
687699

688700
it('empty collection', () => {
@@ -1068,7 +1080,7 @@ describe('lazy loading', () => {
10681080
"self": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/relationships/movie",
10691081
"related": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/movie"
10701082
}
1071-
},
1083+
},
10721084
},
10731085
type: "question"
10741086
}]
@@ -1087,7 +1099,7 @@ describe('lazy loading', () => {
10871099
movie: {
10881100
links: {
10891101
"self": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/relationships/movie",
1090-
"related": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/movie"
1102+
"related": "http://localhost:3000/api/v1/actor/1c9d234b-66c4-411e-b785-955d57db5536/movie"
10911103
}
10921104
}
10931105
}
@@ -1097,6 +1109,6 @@ describe('lazy loading', () => {
10971109

10981110
it('basic test', () => {
10991111
const result = normalize(json);
1100-
expect(isEqual(result, output)).to.be.true;
1112+
expect(isEqual(result, output)).to.be.true;
11011113
});
11021114
});

0 commit comments

Comments
 (0)