How to tweak my own transit settings on the front-end?

With the help of Muuntaja, I’ve written my own Transit encoders for my back-end that can handle Instants and soon UUIDs. What I’m not sure about is how to make the parallel decoders on the front-end, where I’m using re-frame-http-fx for my ajax requests. I lifted the following from @yogthos Luminus, but in that code-base as-transit isn’t actually called anywhere, and I’m not sure where I would use it. How should I setup my transit reading on the front-end so that data I receive over the wire correctly interprets my custom special types (Instant, UUID) into the matching Clojurescript things?

(ns myproject.ajax
  (:require [ajax.core :as ajax]
	    [luminus-transit.time :as time]
	    [cognitect.transit :as transit]))

(defn as-transit [opts]
  (merge {:raw             false
	  :format          :transit
	  :response-format :transit
	  :reader          (transit/reader :json time/time-deserialization-handlers)
	  :writer          (transit/writer :json time/time-serialization-handlers)}
	 opts))

The as-transit function is meant to be used with cljs-ajax to set the opts that will hint to use transit for serialization/deserialization., e.g: (ajax/POST "/foo" (as-transit {:params {:foo :bar}})).

I see. I’m looking for how to plug it in to the standard re-frame http call, which doesn’t quite work this way:

(rfc/reg-event-fx
 :handler-with-http
 (let [transit-response-format (my-ajax/as-transit (ajax/transit-response-format))]
     (fn handler-with-http [db [_ {:keys [uri method params on-success-kvec]
                                   :or {method :get
                                        on-success-kvec [:on-success]}}]]
       {:http-xhrio {:method method
                     :uri             uri
                     :params          params
                     :timeout         8000
                     :format (ajax/transit-request-format)
                     :response-format transit-response-format
                     :on-success      on-success-kvec
                     :on-failure      [:on-failure]}})))

In theory that looks like the way to do it. Might be worth trying making your own reg-fx to see if that works first. The implementation in re-frame is pretty small https://github.com/day8/re-frame-http-fx/blob/master/src/day8/re_frame/http_fx.cljs

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.