-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresponse.clj
46 lines (38 loc) · 1.37 KB
/
response.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
(ns triangulum.response
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[triangulum.config :refer [get-config]]
[triangulum.utils :as utils]))
;; spec
(s/def ::response-type #{:json :edn :transit})
#_{:clj-kondo/ignore [:deprecated-var]}
(defn data-response
"Creates a response object.
Body is required. Status, type, and session are optional.
When a type keyword is passed, the body is converted to that type,
otherwise the body is converted to your config.edn's :server :response-type."
[body & [params]]
(utils/data-response body
(if (:type params)
params
(assoc params :type (get-config :server :response-type)))))
(defn json-response
"Creates a json response type."
[body & [params]]
(data-response body (assoc params :type :json)))
(defn edn-response
"Creates an edn response type."
[body & [params]]
(data-response body (assoc params :type :edn)))
(defn transit-response
"Creates a transit response type."
[body & [params]]
(data-response body (assoc params :type :transit)))
(defn no-cross-traffic?
"Checks for cross traffic."
[{:strs [referer host]}]
(and referer host (str/includes? referer host)))
(defn forbidden-response
"Returns a forbidden response."
[_]
(data-response "Forbidden" {:status 403}))