mrichar1 / jsonapi-vuex

Use a JSONAPI api with a Vuex store, with data restructuring/normalization.
GNU Affero General Public License v3.0
156 stars 23 forks source link

Meta object is missing when sending a PATCH request #212

Closed cacholong closed 1 year ago

cacholong commented 1 year ago

Hello @mrichar1,

I'm working on a project using the jsonapi-vuex library which is a great library. The api i'm developing against has some additional option using a meta object. I'm using cleanPatches. When i send the PATCH request the following body is send.

{
    "data": {
        "attributes": { ..... },
        "id": "someId",
        "type": "someType"
    }
}

The request body is missing the added custom object meta which is probably removed by the library. All works fine with a POST request. The object is added as ....._jv.meta = { .... }.

I was wondering if something like that would be possible for the PATCH request also with a resulting request body like blow.

{
    "data": {
        "attributes": { ..... },
        "id": "someId",
        "type": "someType"
        "meta": { ..... },
    }
}

I think this should be possible when looking at the JSON:API specification. https://jsonapi.org/format/#document-resource-objects

I'm looking forward to your reply.

Best regards,

Durk

mrichar1 commented 1 year ago

Hi,

It's not very obviously documented, but there is a cleanPatchProps config setting, which takes an array of the names of the jsonapi 'items' that should be preserved when cleaning the patch. These can be any of 'links', 'meta', and 'relationships'.

In your case I think all you need to do is add the following config:

const conf = {
      cleanPatch: true,
      cleanPatchProps: ['meta']
    }
    jsonapiModule = createJsonapiModule(this.api, conf)

Then just make sure your patch object has a top-level meta section containing the appropriate data.

Let us know if this works for you!

cacholong commented 1 year ago

Hi @mrichar1,

Thank you for the quick reply. That does the trick. Awesome.

Greetings,

Durk

mrichar1 commented 1 year ago

Great - glad that worked as expected!