Last active
December 10, 2017 17:08
-
-
Save rauhs/c137c0518cb7067f58ee to your computer and use it in GitHub Desktop.
Om.next tempid handling for pedestal. Interceptors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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