mirror of
https://github.com/penpot/penpot.git
synced 2025-07-22 01:07:18 +02:00
🎉 Allow application/json on Accept header
This commit is contained in:
parent
f2b60261f8
commit
240e480b2e
2 changed files with 50 additions and 2 deletions
|
@ -32,6 +32,12 @@
|
||||||
{:name ::params
|
{:name ::params
|
||||||
:compile (constantly ymw/wrap-params)})
|
:compile (constantly ymw/wrap-params)})
|
||||||
|
|
||||||
|
(def ^:private json-mapper
|
||||||
|
(json/mapper
|
||||||
|
{:encode-key-fn str/camel
|
||||||
|
:decode-key-fn (comp keyword str/kebab)
|
||||||
|
:pretty true}))
|
||||||
|
|
||||||
(defn wrap-parse-request
|
(defn wrap-parse-request
|
||||||
[handler]
|
[handler]
|
||||||
(letfn [(process-request [request]
|
(letfn [(process-request [request]
|
||||||
|
@ -46,7 +52,7 @@
|
||||||
|
|
||||||
(str/starts-with? header "application/json")
|
(str/starts-with? header "application/json")
|
||||||
(with-open [is (yrq/body request)]
|
(with-open [is (yrq/body request)]
|
||||||
(let [params (json/decode is)]
|
(let [params (json/decode is json-mapper)]
|
||||||
(-> request
|
(-> request
|
||||||
(assoc :body-params params)
|
(assoc :body-params params)
|
||||||
(update :params merge params))))
|
(update :params merge params))))
|
||||||
|
@ -117,7 +123,32 @@
|
||||||
(finally
|
(finally
|
||||||
(.close ^OutputStream output-stream))))))
|
(.close ^OutputStream output-stream))))))
|
||||||
|
|
||||||
(format-response [response request]
|
(json-streamable-body [data]
|
||||||
|
(reify yrs/StreamableResponseBody
|
||||||
|
(-write-body-to-stream [_ _ output-stream]
|
||||||
|
(try
|
||||||
|
|
||||||
|
(with-open [bos (buffered-output-stream output-stream buffer-size)]
|
||||||
|
(json/write! bos data json-mapper))
|
||||||
|
|
||||||
|
(catch java.io.IOException _cause
|
||||||
|
;; Do nothing, EOF means client closes connection abruptly
|
||||||
|
nil)
|
||||||
|
(catch Throwable cause
|
||||||
|
(l/warn :hint "unexpected error on encoding response"
|
||||||
|
:cause cause))
|
||||||
|
(finally
|
||||||
|
(.close ^OutputStream output-stream))))))
|
||||||
|
|
||||||
|
(format-response-with-json [response _]
|
||||||
|
(let [body (yrs/body response)]
|
||||||
|
(if (or (boolean? body) (coll? body))
|
||||||
|
(-> response
|
||||||
|
(update :headers assoc "content-type" "application/json")
|
||||||
|
(assoc :body (json-streamable-body body)))
|
||||||
|
response)))
|
||||||
|
|
||||||
|
(format-response-with-transit [response request]
|
||||||
(let [body (yrs/body response)]
|
(let [body (yrs/body response)]
|
||||||
(if (or (boolean? body) (coll? body))
|
(if (or (boolean? body) (coll? body))
|
||||||
(let [qs (yrq/query request)
|
(let [qs (yrq/query request)
|
||||||
|
@ -130,6 +161,20 @@
|
||||||
(assoc :body (transit-streamable-body body opts))))
|
(assoc :body (transit-streamable-body body opts))))
|
||||||
response)))
|
response)))
|
||||||
|
|
||||||
|
(format-response [response request]
|
||||||
|
(let [accept (yrq/get-header request "accept")]
|
||||||
|
(cond
|
||||||
|
(or (= accept "application/transit+json")
|
||||||
|
(str/includes? accept "application/transit+json"))
|
||||||
|
(format-response-with-transit response request)
|
||||||
|
|
||||||
|
(or (= accept "application/json")
|
||||||
|
(str/includes? accept "application/json"))
|
||||||
|
(format-response-with-json response request)
|
||||||
|
|
||||||
|
:else
|
||||||
|
(format-response-with-transit response request))))
|
||||||
|
|
||||||
(process-response [response request]
|
(process-response [response request]
|
||||||
(cond-> response
|
(cond-> response
|
||||||
(map? response) (format-response request)))]
|
(map? response) (format-response request)))]
|
||||||
|
|
|
@ -60,6 +60,7 @@
|
||||||
http/conditional-decode-transit)]
|
http/conditional-decode-transit)]
|
||||||
(->> (http/send! {:method :get
|
(->> (http/send! {:method :get
|
||||||
:uri (u/join @cf/public-uri "api/rpc/query/" (name id))
|
:uri (u/join @cf/public-uri "api/rpc/query/" (name id))
|
||||||
|
:headers {"accept" "application/transit+json"}
|
||||||
:credentials "include"
|
:credentials "include"
|
||||||
:query params})
|
:query params})
|
||||||
(rx/map decode-transit)
|
(rx/map decode-transit)
|
||||||
|
@ -71,6 +72,7 @@
|
||||||
[id params]
|
[id params]
|
||||||
(->> (http/send! {:method :post
|
(->> (http/send! {:method :post
|
||||||
:uri (u/join @cf/public-uri "api/rpc/mutation/" (name id))
|
:uri (u/join @cf/public-uri "api/rpc/mutation/" (name id))
|
||||||
|
:headers {"accept" "application/transit+json"}
|
||||||
:credentials "include"
|
:credentials "include"
|
||||||
:body (http/transit-data params)})
|
:body (http/transit-data params)})
|
||||||
(rx/map http/conditional-decode-transit)
|
(rx/map http/conditional-decode-transit)
|
||||||
|
@ -88,6 +90,7 @@
|
||||||
(->> (http/send! {:method method
|
(->> (http/send! {:method method
|
||||||
:uri (u/join @cf/public-uri "api/rpc/command/" (name id))
|
:uri (u/join @cf/public-uri "api/rpc/command/" (name id))
|
||||||
:credentials "include"
|
:credentials "include"
|
||||||
|
:headers {"accept" "application/transit+json"}
|
||||||
:body (when (= method :post)
|
:body (when (= method :post)
|
||||||
(if form-data?
|
(if form-data?
|
||||||
(http/form-data params)
|
(http/form-data params)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue