SeyZ / jsonapi-serializer

A Node.js framework agnostic library for (de)serializing your data to JSON API
MIT License
735 stars 196 forks source link

Is it possible to customize the value of the type key? #261

Closed Gonzalo2683 closed 2 years ago

Gonzalo2683 commented 2 years ago

Hello, The api that I am consuming is from the JSON API of drupal, it has different values for the type field in the relationship, for example:

This is the format in which I need to serialize, If we look at the value for the type field for the relationship we see type: "user--user",

const payload = {
  data: {
    type: "node--services",
    attributes: {
      status: true,
      title: "Test Service 5",
      body: {
        value: "<p>Test 5</p>\r\n",
        format: "basic_html",
      },
      field_date: "2022-01-08T14:40:00-03:00",
      field_type_service: ["maniana"],
    },
    relationships: {
      uid: {
        data: {
          type: "user--user",
          id: "562c5c13-dde1-421c-92f4-b7996f99d693",
        },
      },
      field_director: {
        data: {
          type: "node--directors",``
          id: "b87a9939-dcc2-4332-9f5b-b3c81cb63cc0",
        },
      },
    },
  },
};

Serialization example

const dataSet = {
        status: true,
        title: "Test Service 5",
        body: {
          value: "<p>Test 5</p>\r\n",
          format: "basic_html",
        },
        field_date: "2022-01-08T14:40:00-03:00",
        field_type_service: ["maniana"],
        uid: {
          id: "562c5c13-dde1-421c-92f4-b7996f99d693",
          type: "user--user",
        },
        field_director: {
          type: "node--directors",
          id: "b87a9939-dcc2-4332-9f5b-b3c81cb63cc0",
        },
      };

      const serialized = serializer("node--services", {
        keyForAttribute: "snake_case",
        attributes: [
          "status",
          "title",
          "body",
          "field_date",
          "field_type_service",
          "uid",
          "field_director",
        ],
        uid: {
          ref: "id",
          included: false,
          attributes: ["type"],
        },
        field_director: {
          ref: "id",
          included: false,
          attributes: ["type"],
        },
      }).serialize(dataSet);

Results: As you can see, the required value is node--directors and not field_directors, the same happens with user--user and uids.

},
      "relationships": Object {
        "field_director": Object {
          "data": Object {
            "id": "b87a9939-dcc2-4332-9f5b-b3c81cb63cc0",
-           "type": "node--directors",
+           "type": "field_directors",
          },
        },
        "uid": Object {
          "data": Object {
            "id": "562c5c13-dde1-421c-92f4-b7996f99d693",
-           "type": "user--user",
+           "type": "uids",
          },
        },
      },
      "type": "node--services",
    },

Is there a way to change the value for the type key? I have read the documentation and did not find any reference on this change.

Gonzalo2683 commented 2 years ago

I found the solution in the tests, looking more deeply I could see that there is a property called typeForAttribute that receives a function that allows changing the name of the type key.

....
typeForAttribute: function (attribute, data) {
  // sometimes this returns undefined
  return data.customKey;
},