-
Notifications
You must be signed in to change notification settings - Fork 250
/
muuntaja.clj
117 lines (93 loc) · 4.59 KB
/
muuntaja.clj
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(ns reitit.http.interceptors.muuntaja
(:require [clojure.spec.alpha :as s]
[muuntaja.core :as m]
[muuntaja.interceptor]))
(s/def ::muuntaja m/muuntaja?)
(s/def ::spec (s/keys :opt-un [::muuntaja]))
(defn- displace [x] (with-meta x {:displace true}))
(defn- stripped [x] (select-keys x [:enter :leave :error]))
(defn- publish-swagger-data? [{:keys [form body]}]
(not (and (some? form) (nil? body))))
(defn format-interceptor
"Interceptor for content-negotiation, request and response formatting.
Negotiates a request body based on `Content-Type` header and response body based on
`Accept`, `Accept-Charset` headers. Publishes the negotiation results as `:muuntaja/request`
and `:muuntaja/response` keys into the request.
Decodes the request body into `:body-params` using the `:muuntaja/request` key in request
if the `:body-params` doesn't already exist.
Encodes the response body using the `:muuntaja/response` key in request if the response
doesn't have `Content-Type` header already set.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
Optionally takes a default muuntaja instance as argument.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
([]
(format-interceptor nil))
([default-muuntaja]
{:name ::format
:spec ::spec
:compile (fn [{:keys [muuntaja parameters]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(merge
(stripped (muuntaja.interceptor/format-interceptor muuntaja))
(if (publish-swagger-data? parameters)
{:data {:swagger {:produces (displace (m/encodes muuntaja))
:consumes (displace (m/decodes muuntaja))}}}))))}))
(defn format-negotiate-interceptor
"Interceptor for content-negotiation.
Negotiates a request body based on `Content-Type` header and response body based on
`Accept`, `Accept-Charset` headers. Publishes the negotiation results as `:muuntaja/request`
and `:muuntaja/response` keys into the request.
Optionally takes a default muuntaja instance as argument.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
([]
(format-negotiate-interceptor nil))
([default-muuntaja]
{:name ::format-negotiate
:spec ::spec
:compile (fn [{:keys [muuntaja]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(stripped (muuntaja.interceptor/format-negotiate-interceptor muuntaja))))}))
(defn format-request-interceptor
"Interceptor for request formatting.
Decodes the request body into `:body-params` using the `:muuntaja/request` key in request
if the `:body-params` doesn't already exist.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
Optionally takes a default muuntaja instance as argument.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
([]
(format-request-interceptor nil))
([default-muuntaja]
{:name ::format-request
:spec ::spec
:compile (fn [{:keys [muuntaja parameters]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(merge
(stripped (muuntaja.interceptor/format-request-interceptor muuntaja))
(when (publish-swagger-data? parameters)
{:data {:swagger {:consumes (displace (m/decodes muuntaja))}}}))))}))
(defn format-response-interceptor
"Interceptor for response formatting.
Encodes the response body using the `:muuntaja/response` key in request if the response
doesn't have `Content-Type` header already set.
Swagger-data will be omitted when `:form`, but no `:body`, parameters are defined.
Optionally takes a default muuntaja instance as argument.
| key | description |
| -------------|-------------|
| `:muuntaja` | `muuntaja.core/Muuntaja` instance, does not mount if not set."
([]
(format-response-interceptor nil))
([default-muuntaja]
{:name ::format-response
:spec ::spec
:compile (fn [{:keys [muuntaja parameters]} _]
(if-let [muuntaja (or muuntaja default-muuntaja)]
(merge
(stripped (muuntaja.interceptor/format-response-interceptor muuntaja))
(when (publish-swagger-data? parameters)
{:data {:swagger {:produces (displace (m/encodes muuntaja))}}}))))}))