diff --git a/package.json b/package.json index 1d66644..4d18635 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/functions/find-items.ts b/src/functions/find-items.ts index 1217df7..1bf628a 100644 --- a/src/functions/find-items.ts +++ b/src/functions/find-items.ts @@ -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; } diff --git a/src/index.ts b/src/index.ts index 0e64323..922460d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/test/normalize.spec.ts b/src/test/normalize.spec.ts new file mode 100644 index 0000000..c004c8c --- /dev/null +++ b/src/test/normalize.spec.ts @@ -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'); + }); +}); diff --git a/src/test/validate.spec.ts b/src/test/validate.spec.ts index 384674a..71bec56 100644 --- a/src/test/validate.spec.ts +++ b/src/test/validate.spec.ts @@ -51,4 +51,4 @@ describe('Validation test', () => { validate(test); }).to.throw('[Normalize] [JSON:API Syntax Error] Not all data items are the same type!'); }); -}); \ No newline at end of file +});