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

Encoded type and id might cause to return undefined instead of stored object

szymonlankauf opened this issue · comments

Function utils.getTypeId(data) returns array of encoded type, id and rel. In example id = abc|123 becomes id = abc%7C123.
In some places the function is used like this:

const [type, id] = utils.getTypeId(data)

result = state[type][id]

This is a mistake, because objects are stored under decoded id and type:
result = state[type][id] expects decoded type and id, but it receives encoded ones, therefore it will return undefined if either type or id has a special sign. According to JSON:API specification using special signs is not forbidden

I believe this is an issue only in getters. A quick and simple fix would be to map encoded type and id to decoded ones:
const [type, id] = utils.getTypeId(data).map(el => decodeURIComponent(el))

Hi - thanks for spotting this! Yes, the encoding is over-zealous, and doesn't need to happen for getters since they're never used in URLs.

Rather than 'undo' the encoding, I've taken the approach of making it optional in utils.getTypeId instead. Will submit a PR to cover this shortly.