Fork me on GitHub
#re-frame
<
2022-02-21
>
mikecarter16:02:30

We have a re-frame app that talks to a Rails based JSON API. We'd like to convert the JSON keys from snake_case to kebab-case on the re-frame end for API requests and responses. Can anyone advise on how best to tackle this? I'm wondered whether a re-frame interceptor would be best for this or perhaps a cljs-ajax interceptor?

p-himik16:02:03

Not an interceptor but a custom effect that does this. If you're using the http-xhrio effect, you can create custom body readers and writers that would do the conversion, and then in your custom effect you can just wrap the http-xhrio one while overriding the default body readers/writers with yours when needed.

lispers-anonymous16:02:21

I've dealt with a similar problem. I essentially did what p-himik describes. For the conversion, I used this camel-snake-kebab library: https://github.com/clj-commons/camel-snake-kebab It's an extra dependency, so a simple mechanical transformation might be preferred if the data you are working with is pretty simple.

mikecarter16:02:01

Thanks @U2FRKM4TW, @UDVJE9RE3! Is there an example of a custom body reader/writer anywhere?

p-himik16:02:45

Oh, actually another answer is interceptors, just not re-frame's ones but cljs-ajax's ones: https://github.com/JulianBirch/cljs-ajax/blob/master/docs/interceptors.md Regarding readers/writers - cljs-ajax calls them response/request formats: https://github.com/JulianBirch/cljs-ajax/blob/master/docs/formats.md The best examples are probably in the cljs-ajax source code itself.

mikecarter16:02:10

Wonderful, thanks! I'll dive in

👍 1
mikecarter11:02:50

Thanks, both, for your help. I ended up writing custom response/request formats for cljs-ajax as it appears it doesn't allow you to alter the content of a response body using interceptors 👍

👍 1