Fork me on GitHub
#reitit
<
2018-08-03
>
valerauko05:08:57

i'm still confused a little about the role of muuntaja. it only does content type negotiation, right? so if for example an endpoint can serve both html and json (which are obviously very different) i still have to handle that in my handler right?

ikitommi10:08:27

@vale Muuntaja is about body parameter encoding and decoding - and negotiating about formats it supports. By default it supports json, edn and transit format. So, if you are sending & expecting those, it works behind the scenes without much notice - just read :body params as clojure data and return response :body as clojure data as.

đź‘Ť 4
ikitommi10:08:12

For some reason, it doesn’t publish the negotiataion results for formats it doesn’t cover. e.g. if the client expects a text/html response, the handler doesn’t get a hint of it - need to parse the headers itself.

ikitommi10:08:15

that could be changed.

ikitommi10:08:21

((muuntaja.middleware/wrap-format-negotiate identity)
  {:headers {"content-type" "application/transit+json"
             "accept" "application/edn"}})
;{:headers {"content-type" "application/transit+json", "accept" "application/edn"},
; :muuntaja/request #FormatAndCharset{:format "application/transit+json", :charset "utf-8"},
; :muuntaja/response #FormatAndCharset{:format "application/edn", :charset "utf-8"}}

ikitommi10:08:59

that’s the basic usage. wrap-format contains all of: wrap-format-negotiate, wrap-request-format and wrap-response-format.

ikitommi10:08:26

this is kinda wrong:

ikitommi10:08:28

((muuntaja.middleware/wrap-format-negotiate identity)
  {:headers {"accept" "text/html"}})
;{:headers {"accept" "text/html"},
; :muuntaja/request nil,
; :muuntaja/response #FormatAndCharset{:format "application/json", :charset "utf-8"}}

ikitommi10:08:49

but… if you want to return alway text/html from an endpoint, just set the [:headers "Content-Type"] yourself, format the body accordingly and muuntaja doesn’t touch it.

ikitommi10:08:51

e.g.

{:status 200
 :headers {"Content-Type" "text/html"}
 :body "<h1>hello</h1>"}

ikitommi10:08:30

the reitit-middleware is merged in master & in clojars. The new guide: https://metosin.github.io/reitit/ring/default_middleware.html

valerauko10:08:27

what i was having a bit of trouble with

valerauko10:08:58

:get {:produces #{"text/html"}
      :handler html-producing-handler}

valerauko10:08:30

and this dies with a Malformed application/json in :muuntaja/encode if it's wrapped with wrap-format

ikitommi10:08:58

which version are you using?

valerauko10:08:28

[metosin/reitit "0.1.3"]
[metosin/muuntaja "0.5.0"]

ikitommi10:08:10

few things: • the :produces is not handled by anything • if you are returning text/html, you need to set the header yourself

ikitommi10:08:29

the latter will be resolved when the openapi3-syntax comes along.

valerauko10:08:42

ok then i'll do that

ikitommi10:08:03

{:get {:swagger {:produces #{"text/html"}} ;; not mandatory, just for docs
       :handler (constantly
                  {:status 200
                   :headers {"Content-Type" "text/html"}
                   :body "<h1>hello</h1>"})}}

ikitommi10:08:34

should work.

ikitommi10:08:03

without the swagger-hint:

{:get (constantly
        {:status 200
         :headers {"Content-Type" "text/html"}
         :body "<h1>hello</h1>"})}

valerauko10:08:05

just to confirm: does :produces have to be under :swagger to be recognized?

ikitommi10:08:38

yes, it's just a a documentation hint now. There is a good sample app here: https://github.com/metosin/reitit/blob/master/examples/ring-swagger/src/example/server.clj

đź’Ż 4
ikitommi10:08:35

(example requires 0.2.0-SNAPSHOT, will release on next few days)

pauld20:08:30

Is there documentation on how reitit.ring/routes works?

pauld20:08:03

how does it combine handlers?

pauld20:08:24

For instance, I want to have a default resource handler, but I also want to have a default handler that returns the index.html file of my SPA.

pauld20:08:22

That default handler should trigger instead of 404 for any uri that is not a legitimate resource.

pauld20:08:45

similar to what is achieved by compojure: