Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@ function extractRelationships(relationships, { camelizeKeys, camelizeTypeValues

if (typeof relationship.data !== 'undefined') {
if (isArray(relationship.data)) {
ret[name].data = relationship.data.map(e => ({
id: e.id,
type: camelizeTypeValues ? camelCase(e.type) : e.type,
}));
ret[name].data = relationship.data.map((e) => {
const data = {
id: e.id,
type: camelizeTypeValues ? camelCase(e.type) : e.type,
};
if (typeof e.meta !== 'undefined') {
data.meta = camelizeKeys ? camelizeNestedKeys(e.meta) : e.meta;
}
return data;
});
} else if (!isNull(relationship.data)) {
ret[name].data = {
id: relationship.data.id,
type: camelizeTypeValues ? camelCase(relationship.data.type) : relationship.data.type,
};
if (typeof relationship.data.meta !== 'undefined') {
ret[name].data.meta = camelizeKeys
? camelizeNestedKeys(relationship.data.meta) : relationship.data.meta;
}
} else {
ret[name].data = relationship.data;
}
Expand Down
56 changes: 56 additions & 0 deletions test/normalize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,62 @@ describe('relationships', () => {
expect(result).to.deep.equal(output);
});

it('non-empty to-many with meta', () => {
const json = {
data: [
{
type: 'post',
relationships: {
tags: {
data: [
{
id: 4,
type: 'tag',
meta: {
test: 'meta value'
}
},
],
},
},
id: 2620,
attributes: {
text: 'hello',
},
},
],
};

const output = {
post: {
2620: {
type: 'post',
id: 2620,
attributes: {
text: 'hello',
},
relationships: {
tags: {
data: [
{
id: 4,
type: 'tag',
meta: {
test: 'meta value'
}
},
],
},
},
},
},
};

const result = normalize(json);

expect(result).to.deep.equal(output);
});

it('keys camelized', () => {
const json = {
data: [
Expand Down