Skip to content

Commit

Permalink
fix: ignore relationships that don't have data property (#778)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Warren <pdw@ex-parrot.com>
Co-authored-by: Vladimír Gorej <vladimir.gorej@gmail.com>
  • Loading branch information
3 people authored Feb 22, 2023
1 parent 1dcda5c commit 65f0daa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
concat,
insert,
assocPath,
pickBy,
} from 'ramda';
import { mapIndexed, reduceIndexed, ensureArray, isArray } from 'ramda-adjunct';

Expand All @@ -24,7 +25,11 @@ import { mapIndexed, reduceIndexed, ensureArray, isArray } from 'ramda-adjunct';
// RelPath = {path: String, key: ResourceKey}

// getRelationships :: Resource -> Relationships
const getRelationships = pipe(propOr({}, ['relationships']), keys);
const getRelationships = pipe(
propOr({}, ['relationships']),
pickBy(has('data')),
keys
);

// resourceKey :: ResourceRel -> ResourceKey
const resourceKey = ({ type, id }) => `${type}-${id}`;
Expand Down
23 changes: 20 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,27 @@ describe('jsonApiMerge', function () {
};

specify('should throw error', function () {
const included = jsonApiMerge(jsonApiData.included, jsonApiData.included);
const thunk = () => jsonApiMerge(included, jsonApiData.data);
const expected = {
id: 1,
type: 'resource',
attributes: {
name: 'Resource name',
},
relationships: {
related: {
links: {
related: {
href: 'http://example.com/related-resource/',
title: 'Related',
},
},
},
},
};

assert.throws(thunk, TypeError);
const actual = jsonApiMerge(jsonApiData.included, jsonApiData.data);

assert.deepEqual(actual, expected);
});
});

Expand Down

0 comments on commit 65f0daa

Please sign in to comment.