Skip to content

would not be nice to have a generic valueForRelationship? #256

@ftaiolivista

Description

@ftaiolivista

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch jsonapi-serializer@3.6.7 for the project I'm working on.

Currently I have to declare valueForRelationship for every relationship but many times the function must be applied to all entities so could be nice to have the possibility to declare a general valueForRelationship tha can be replaced per specific relationship.

Here is the diff that solved my problem:

diff --git a/node_modules/jsonapi-serializer/lib/deserializer-utils.js b/node_modules/jsonapi-serializer/lib/deserializer-utils.js
index 1265fd4..b63e3e6 100644
--- a/node_modules/jsonapi-serializer/lib/deserializer-utils.js
+++ b/node_modules/jsonapi-serializer/lib/deserializer-utils.js
@@ -14,9 +14,9 @@ module.exports = function (jsonapi, data, opts) {
   }
 
   function getValueForRelationship(relationshipData, included) {
-    if (opts && relationshipData && opts[relationshipData.type]) {
-      var valueForRelationshipFct = opts[relationshipData.type]
-        .valueForRelationship;
+    if (opts && relationshipData && (opts[relationshipData.type] || opts.valueForRelationship)) {
+      var valueForRelationshipFct =
+        opts[relationshipData.type]?.valueForRelationship || opts.valueForRelationship;
 
       return valueForRelationshipFct(relationshipData, included);
     } else {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions