olosegres/jsona

Get meta from the API

robbporto opened this issue · 6 comments

Hi, everyone! I have the following JSON:

{
  "data": [
    {
      "id": "1",
      "type": "formations",
      "links": {
        "self": "https://localhost.com/api/v1/formations/1"
      },
      "attributes": {
        "title": "Voluptatibus magni maiores et qui aut commodi tempore.",
        "text_content": "Debitis ipsa odio iusto ut ea consectetur perspiciatis. Quae ea voluptatem ut atque velit. Nobis quia et quod corrupti.",
        "audio_url": null,
        "favorited": false,
        "cover_url": null,
        "cover_attachment_id": null,
        "audio_attachment_id": null
      }
    },
    {
      "id": "2",
      "type": "formations",
      "links": {
        "self": "https://localhost.com/api/v1/formations/2"
      },
      "attributes": {
        "title": "Nihil pariatur deserunt non optio temporibus.",
        "text_content": "Qui architecto enim. Beatae sed molestias facilis voluptates dolorem. Repellat dolor voluptatum. Commodi dolorem enim.",
        "audio_url": null,
        "favorited": false,
        "cover_url": null,
        "cover_attachment_id": null,
        "audio_attachment_id": null
      }
    }
  ],
  "meta": {
    "total_count": 2
  },
  "links": {
    "first": "https://localhost.com/api/v1/formations?page%5Bnumber%5D=1&page%5Bsize%5D=10",
    "last": "https://localhost.com/api/v1/formations?page%5Bnumber%5D=1&page%5Bsize%5D=10"
  }
}

I'm using the deserialize function to normalize the attributes but I need to have this meta: {total_count: 2} normalized as well.

Is this possible? I searched in the "Customize" section and tried to implement this for an hour but I didn't succeed.

Thanks for your help!

@robbporto
Hi, you should handle this apart, just get it from jsonBody that you pass to jsona.deserialize method jsona.deserialize(jsonBody).

Jsona's deserialize method converts json body to collection or one model, omitting top level meta.

@robbporto or you may create PR, where deserialize will return object like this:

const deserialized = {
  meta: { yourMeta: true },
  stuff: [{ type: 'foo', id: 'bar' }] /* or stuff: { type: 'foo', id: 'bar' } */
};

Thank you for your answer, @olosegres.

I read that there's a function called setMeta. Do you think that this function would be useful here?

No, it's for handling Resource object's meta
http://jsonapi.org/format/#document-resource-objects

Oh, you're right, @olosegres. I think that I'll make a PR for that.

Thanks for your help!