Fork me on GitHub
#reitit
<
2023-04-14
>
frozenlock14:04:03

Coming from Yada. When a browser requested some JSON data from an API, it would automatically be converted to HTML with json-html (https://github.com/yogthos/json-html) Is there something similar that can be done with Reitit?

frozenlock15:04:44

It's probably going to be with muuntaja/format-response-middleware :thinking_face:

frozenlock17:04:45

I think this is the proper way:

(defn html-encoder [_]
  (reify
    muuntaja-format/EncodeToBytes
    (encode-to-bytes [_ data charset]
      (.getBytes
          (jh/edn->html data)
        ^String charset))
    muuntaja-format/EncodeToOutputStream
    (encode-to-output-stream [_ data charset]
      (fn [^OutputStream output-stream]
        (.write output-stream (.getBytes
                               (jh/edn->html data)
                               ^String charset))))))

(def muuntaja-instance
  (muuntaja/create
   (-> muuntaja/default-options
       (update-in [:formats "text/html"]
                  merge
                  (muuntaja-format/map->Format {:name    "text/html"
                                                :encoder [html-encoder]})))))
But now it's the default choice in swagger 😕