-
Notifications
You must be signed in to change notification settings - Fork 136
/
transit.cljc
68 lines (61 loc) · 2.68 KB
/
transit.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns ajax.transit
(:require [cognitect.transit :as t]
[ajax.interceptors :as i]
[ajax.protocols :as pr]
[ajax.util :as u]
#? (:clj [ajax.macros :as m])))
; Surprisingly, only the clj version needs m/defn-curried
(defn transit-type [{:keys [type]}]
(or type #? (:cljs :json :clj :msgpack)))
#? (:cljs (defn transit-write-fn
[type opts]
(let [writer (or (:writer opts)
(t/writer type opts))]
(fn transit-write-params [params]
(t/write writer params))))
:clj (m/defn-curried transit-write-fn
[type opts stream params]
(let [writer (t/writer stream type opts)]
(t/write writer params))))
(defn transit-request-format
"Returns a Transit request format.
Options include:
:writer Explicit Transit writer. If not supplied one will be created using the other options.
The default type is :json for ClojureScript and :msgpack for Clojure.
:type Override the default transit type with value :json, :json-verbose or :msgpack
:handlers Custom Transit handlers (refer to Transit documentation)"
([] (transit-request-format {}))
([opts]
(let [type (transit-type opts)
mime-type (if (or (= type :json)
(= type :json-verbose)) "json" "msgpack")]
{:write (transit-write-fn type opts)
:content-type (str "application/transit+" mime-type)})))
#? (:cljs (defn transit-read-fn [opts]
(let [reader (or (:reader opts)
(t/reader :json opts))]
(fn transit-read-response [response]
(t/read reader (pr/-body response)))))
:clj (m/defn-curried transit-read-fn [request response]
(let [content-type (u/get-content-type response)
type (if (.contains content-type "msgpack")
:msgpack :json)
stream (pr/-body response)
reader (t/reader stream type request)]
(t/read reader))))
(defn transit-response-format
"Returns a Transit request format.
Options include:
:reader (CLJS only) Explicit Transit reader. If not supplied one will be created using the other options.
:handlers Custom Transit handlers (refer to Transit documentation)"
([] (transit-response-format {}))
([opts]
(transit-response-format (transit-type opts) opts))
([type opts]
(i/map->ResponseFormat
{:read (transit-read-fn opts)
:description "Transit"
:content-type
#? (:cljs ["application/transit+json"]
:clj ["application/transit+msgpack"
"application/transit+json"])})))