mrichar1 / jsonapi-vuex

Use a JSONAPI api with a Vuex store, with data restructuring/normalization.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option preserveJson ignored

jewe opened this issue · comments

Thank you for this wonderful library!

I integrate jsonapi-vuex into my nuxt project like this:

import { jsonapiModule } from 'jsonapi-vuex'

export default function ({ $axios, store }) {
    store.registerModule('jv', jsonapiModule($axios, { preserveJson: true }))
}

My JSON contains a global meta field:

{
    "data": [
        {
            "id": "1",
            ...
        }
    ],
    "meta": {
        "headers": [...]
    },
    "jsonapi": {
        "version": "1.0"
    }
}

But in the store the path "jv/_jv/" contains only an empty object.
How can i access the global meta data?

preserveJson is request-specific - the JSON isn't added to the store, since any asynchronous action could update it. This would cause problems if the global meta contained request-specific things like session keys, dates etc.

The JSON data is instead found in the response from the action call, e.g.:

this.$store
  .dispatch('jv/get', '/widget/1')
  .then((res) => {
    console.log(res._jv.json)
  })

If there is a need to store the values, then you can opt to copy it into the store 'root' _jv or into the _jv for specific object(s) in the store. For example:

store.commit(
  'jv/mergeRecords',
  {
    1: {
      _jv: {
        meta: { ... },
      }
    },
)

Thank you very much! This works!