Enable transit encoding parametrization.

This commit is contained in:
Andrey Antukh 2020-02-03 22:34:36 +01:00
parent 2a5fa4df0e
commit 49f27066b8
2 changed files with 11 additions and 3 deletions

View file

@ -31,6 +31,7 @@
:smtp-enabled false :smtp-enabled false
:allow-demo-users true :allow-demo-users true
:registration-enabled true :registration-enabled true
:debug-humanize-transit true
}) })
(s/def ::http-server-port ::us/integer) (s/def ::http-server-port ::us/integer)

View file

@ -2,11 +2,15 @@
;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; ;;
;; Copyright (c) 2019 Andrey Antukh <niwi@niwi.nz> ;; This Source Code Form is "Incompatible With Secondary Licenses", as
;; defined by the Mozilla Public License, v. 2.0.
;;
;; Copyright (c) 2019-2020 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.http.interceptors (ns uxbox.http.interceptors
(:require (:require
[vertx.web :as vw] [vertx.web :as vw]
[uxbox.config :as cfg]
[uxbox.common.exceptions :as ex] [uxbox.common.exceptions :as ex]
[uxbox.util.transit :as t]) [uxbox.util.transit :as t])
(:import (:import
@ -30,12 +34,15 @@
(def format-response-body (def format-response-body
{:leave (fn [{:keys [response] :as data}] {:leave (fn [{:keys [response] :as data}]
(let [body (:body response)] (let [body (:body response)
type (if (:debug-humanize-transit cfg/config)
:json-verbose
:json)]
(cond (cond
(coll? body) (coll? body)
(-> data (-> data
(assoc-in [:response :body] (assoc-in [:response :body]
(t/bytes->buffer (t/encode body))) (t/bytes->buffer (t/encode body {:type type})))
(update-in [:response :headers] (update-in [:response :headers]
assoc "content-type" "application/transit+json")) assoc "content-type" "application/transit+json"))