Skip to content

Normalize still works when server does not send an 'included' or when… #4

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

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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"install": "npm run build",
"test": "mocha -r ts-node/register src/test/*.spec.ts"
},
"repository": {
Expand Down
14 changes: 9 additions & 5 deletions src/functions/find-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export function findItems(response: any, temp: any) {
throw '[Normalize] [JSON:API Syntax Error] A data relationship item does not contain id field!';
}
const result = [];
// Adding all found results
result.push(
response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
(String(item.type) === String(temp.type))),
);

// If response has a 'included' property then look for results
if (response.hasOwnProperty('included')) {
// Adding all found results
result.push(
response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
(String(item.type) === String(temp.type))),
);
}
return result;
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export default function normalize(response: any) {
// Using a for loop to go through all relationships of every item in data
for (const key of Object.keys(item.relationships)) {
const relation = item.relationships[key];

// Skip if relationship is not present
if (!relation.data) {
continue;
}

// Checking if there is only one relationship stored in an object
if (relation.data.constructor === Array) {
// Checking if a data relationship item has type field
Expand Down
79 changes: 79 additions & 0 deletions src/test/normalize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Validation test file
* Description: This file contains tests which check if any modifications
* make the new code incompatible with the old one.
*/

// Importing the normalize function
import normalize from '../index';
// Importing mocha and chai for the tests
import { expect } from 'chai';
import 'mocha';

// Creating the test set
describe('normalize test', () => {
it('should normalize when included relationship is null', () => {
const test = {
"data":[
{
"id":"3",
"type":"conversation",
"attributes":{
"subject":"Test subject"
},
"relationships":{
"last_message":{
"data":{ type: "people", id: "9" }
}
}
},
],
"included": [],
};
expect(() => normalize(test)).to.not.throw();
const normalized = normalize(test);
expect(normalized).to.have.property('conversation');
});
it('should normalize when property "included" is undefined', () => {
const test = {
"data":[
{
"id":"3",
"type":"conversation",
"attributes":{
"subject":"Test subject"
},
"relationships":{
"last_message":{
"data":{ type: "people", id: "9" }
}
}
},
],
};
expect(() => normalize(test)).to.not.throw();
const normalized = normalize(test);
expect(normalized).to.have.property('conversation');
});
it('should normalize when property "included" is undefined and relationship is null', () => {
const test = {
"data":[
{
"id":"3",
"type":"conversation",
"attributes":{
"subject":"Test subject"
},
"relationships":{
"last_message":{
"data": null
}
}
},
],
};
expect(() => normalize(test)).to.not.throw();
const normalized = normalize(test);
expect(normalized).to.have.property('conversation');
});
});
2 changes: 1 addition & 1 deletion src/test/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ describe('Validation test', () => {
validate(test);
}).to.throw('[Normalize] [JSON:API Syntax Error] Not all data items are the same type!');
});
});
});