Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Om.next tempid handling for pedestal. Interceptors.
(ns srs-s.routes.core
(:require [io.pedestal.http :as pedestal]
[io.pedestal.http.route.definition :refer [defroutes]]
[io.pedestal.interceptor.helpers :as interceptor]
[io.pedestal.http.body-params :as body-params]
[ring.util.response :as ring-response]
[cognitect.transit :as transit]
[om.next.server :as om]
[om.tempid :as tempid])
(:import [java.io OutputStream]
[com.cognitect.transit ReadHandler]
[om.tempid TempId]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SERVER
(def transit+om-json-body
"Copied from io.pedestal.http/transit-json-body
Tiny edit is commented inline."
(interceptor/on-response
::transit-om-json-body
(fn [response]
(let [body (:body response)
content-type (get-in response [:headers "Content-Type"])]
(if (and (coll? body) (not content-type))
(-> response
(ring-response/content-type "application/transit+json;charset=UTF-8")
(assoc :body (fn [^OutputStream output-stream]
(transit/write (om/writer ;; changed this.
output-stream)
body)
(.flush output-stream))))
response)))))
(defn om-next-query
[req]
{:status 200
:body {:foo (tempid/tempid "my-id")}})
(def om-body-params
(body-params/body-params
(body-params/default-parser-map :transit-options
[{:handlers {"om/id" (reify
ReadHandler
(fromRep [_ id]
(TempId. id)))}}])))
(defroutes routes
[[["/api/v1"
["/public"
^:interceptors [om-body-params transit+om-json-body]
["/om-next" {:post om-next-query}]]
]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment