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

How to normalize keys from server #179

Closed cloke closed 3 years ago

cloke commented 3 years ago

First I want to say I super appreciate this library.

Is there a proper approach to converting keys from the server? The json api response uses kebab case, but we'd like the Vuex store to keep them as camelCase. Then when we save a record they'd be transformed back.

I thought utils. jsonapiToNorm might do the trick, but it is unclear how to modify the data before jsonapi-vuex puts it into the store. I have the code to actually recursively change the keys, just need to know where to put it.

mrichar1 commented 3 years ago

This isn't something the code currently has support for - I'm not 100% sure there is a 'single point' in the code where you could put in translation and guarantee that it would apply everywhere. At the very least it would need to be in jsonapiToNorm/normToJsonapi but possibly also in getURL and some of the relationship handling code.

Perhaps the easiest option is to do this with the request/response using axios interceptors: https://github.com/axios/axios#interceptors

There's a middleware module that does this between cases that might work 'as-is' (or serve as inspiration for your own translation middleware): https://www.npmjs.com/package/axios-case-converter

Let me know if that would be enough to do what you need - I'd be happy to look at adding hooks into the utils functions, but it might not be necessary if the above works!

cloke commented 3 years ago

That works. Thank you.