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

feat: introduce stronger types to fail fast #5

Merged
merged 7 commits into from
May 22, 2023
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const yourJsonData = fetch('https://api.example.com/api/folders');
const rootItems: any[] = deserializer.consume(yourJsonData).getRootItems();
```

Please note that the deserializer only supports successful responses, and you will get an error if the response is not compatible with the JSON:API specification.
I practise the response should be checked for errors before deserializing it and the error handling should be done separately.

### Examples

* [The JSON from the jsonapi.org example](docs/examples/jsonapiorg)
Expand Down
35 changes: 22 additions & 13 deletions src/__tests__/Deserializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const jsonapiOrgExampleData2 = {
self: 'https://example.com/articles/1/relationships/comments',
related: 'https://example.com/articles/1/comments',
},
data: null,
data: [],
},
},
links: {
Expand All @@ -135,7 +135,7 @@ const jsonapiOrgExampleData2 = {
type Article = {
id: number;
title: string;
author?: Person;
author?: Person|null;
comments: Comment[];
};

Expand All @@ -156,13 +156,18 @@ const articleDeserializer: ItemDeserializer<Article> = {
type: 'articles',
deserialize: (item: Item, relationshipDeserializer: RelationshipDeserializer): Article => {
const article: Article = {
author: null,
id: parseInt(item.id),
title: item.attributes.title,
comments: [],
};

article.author = relationshipDeserializer.deserializeRelationship(relationshipDeserializer, item, 'author');
article.comments = relationshipDeserializer.deserializeRelationships(relationshipDeserializer, item, 'comments');
if (relationshipDeserializer.isRelationshipDataPresent(item, 'author')) {
article.author = relationshipDeserializer.deserializeRelationship(item, 'author');
}
if (relationshipDeserializer.isRelationshipDataPresent(item, 'comments')) {
article.comments = relationshipDeserializer.deserializeRelationships(item, 'comments');
}

return article;
},
Expand All @@ -188,8 +193,9 @@ const commentDeserializer: ItemDeserializer<Comment> = {
body: item.attributes.body,
};

comment.author = relationshipDeserializer.deserializeRelationship(relationshipDeserializer, item, 'author');

if (relationshipDeserializer.isRelationshipDataPresent(item, 'author')) {
comment.author = relationshipDeserializer.deserializeRelationship(item, 'author');
}
return comment;
},
};
Expand Down Expand Up @@ -347,7 +353,7 @@ const fileSystemExampleData3 = {
related: 'https://example.com/folders/1/children',
self: 'https://example.com/folders/1/relationships/children',
},
data: null,
data: [],
},
},
},
Expand All @@ -373,8 +379,9 @@ const folderDeserializer: ItemDeserializer<Folder> = {
children: [],
};

folder.children = relationshipDeserializer.deserializeRelationships(relationshipDeserializer, item, 'children');

if (relationshipDeserializer.isRelationshipDataPresent(item, 'children')) {
folder.children = relationshipDeserializer.deserializeRelationships(item, 'children');
}
return folder;
},
};
Expand All @@ -391,17 +398,19 @@ const fileDeserializer: ItemDeserializer<File> = {

describe('Deserializer', () => {
it('deserializes the jsonapi.org example into an object graph', () => {
const deserializer: Deserializer = getDeserializer([articleDeserializer, personDeserializer, commentDeserializer]);
const deserializer: Deserializer = getDeserializer([articleDeserializer, personDeserializer, commentDeserializer])
.consume(jsonapiOrgExampleData);

const rootItems: any[] = deserializer.consume(jsonapiOrgExampleData).getRootItems();
const rootItems: any[] = deserializer.getRootItems();

expect(rootItems).toMatchSnapshot();
});

it('deserializes the second jsonapi.org example (without relationships but with relationships.*.links and data:null) into an object graph', () => {
const deserializer: Deserializer = getDeserializer([articleDeserializer, personDeserializer, commentDeserializer]);
const deserializer: Deserializer = getDeserializer([articleDeserializer, personDeserializer, commentDeserializer])
.consume(jsonapiOrgExampleData2);

const rootItems: any[] = deserializer.consume(jsonapiOrgExampleData2).getRootItems();
const rootItems: any[] = deserializer.getRootItems();

expect(rootItems).toMatchSnapshot();
});
Expand Down
Loading