Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource type attribute to deserialized resource objects #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
"@types/type-check": "^0.3.27",
"jasmine": "^2.5.3",
"tslint": "^4.3.1",
"typescript": "^2.1.5"
"typescript": "^4.0.0"
}
}
10 changes: 6 additions & 4 deletions spec/deserializer-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,19 @@ describe('deserialize', () => {

let expected = [
{
'type': 'articles',
'id': '1',
'title': 'JSON API paints my bikeshed!',
'author': {
'type': 'people',
'id': '9',
'first-name': 'Dan',
'last-name': 'Gebhardt',
'twitter': 'dgeb'
},
'comments': [
{ 'id': '5', 'body': 'First!' },
{ 'id': '12', 'body': 'I like XML better' }
{ 'type': 'comments', 'id': '5', 'body': 'First!' },
{ 'type': 'comments', 'id': '12', 'body': 'I like XML better' }
]
}
];
Expand Down Expand Up @@ -124,8 +126,8 @@ describe('deserialize', () => {
let result: any = deserialize(document);

let expected = [
{ id: '1', name: 'John', friend: { id: '2', name: 'Carl', dog: { id: '3', name: 'Bobby' } } },
{ id: '2', name: 'Carl', dog: { id: '3', name: 'Bobby' } }
{ type: 'people', id: '1', name: 'John', friend: { type: 'people', id: '2', name: 'Carl', dog: { type: 'dogs', id: '3', name: 'Bobby' } } },
{ type: 'people', id: '2', name: 'Carl', dog: { type: 'dogs', id: '3', name: 'Bobby' } }
];

expect(matches(expected)(result)).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function denormalize(resources: Multiple<Resource>, relations: RelationsHash): a
}

function processResource(res: Resource, relHash: RelationsHash): Serialization {
let { id, attributes, relationships } = res;
let { id, type, attributes, relationships } = res;

let rels: Multiple<any> = mapValues(relationships, (rel: RelationshipObj): any =>
fmapMultiple(rel.data, (relId: ResourceId): any => {
Expand All @@ -38,7 +38,7 @@ function processResource(res: Resource, relHash: RelationsHash): Serialization {
})
);

return assign({}, attributes, { id }, rels);
return assign({}, attributes, { type, id }, rels);
}

function findRelation(rel: ResourceId, relations: RelationsHash): Resource | undefined {
Expand Down