reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Zaymon 2021-05-08T05:36:38.236700Z

Currently using ring + reitit and malli coercion. I want to convert all incoming requests keys to snake-case (requests incoming from a JS front-end). I’d like to keep naming idiomatic in both places. I’ve tried defining an instance of Muuntaja to do the encoding / decoding:

;; Create an explicit instance of Muuntaja with case rewriting
(def m
  (muun/create
   (-> muun/default-options
       (assoc-in
        [:formats "application/json" :decoder-opts]
        {:decode-key-fn csk/->kebab-case-keyword})
       (assoc-in
        [:formats "application/json" :encoder-opts]
        {:encode-key-fn csk/->camelCase}))))
And my middleware now looks like this:
defn service-routes [db]
  ["/api"
   {:coercion malli-coercer
    :muuntaja formats/instance
    :swagger {:id ::api}
    :middleware [;; Cors
                 #(wrap-cors %
                             :access-control-allow-origin [#".*"]
                             :access-control-allow-methods [:get :post]) ;; TODO: This is BAD! :(
                 ;; query-params & form-params
                 parameters/parameters-middleware
                 ;; content-negotiation
                 muuntaja/format-negotiate-middleware {:muuntaja m} ;; Note the use of the explicit instance of Muuntaja here
                 ;; encoding response body
                 muuntaja/format-response-middleware {:muuntaja m} ;; Note the use of the explicit instance of Muuntaja here
                 ;; exception handling
                 ring-coercion/coerce-exceptions-middleware
                 ;; decoding request body
                 muuntaja/format-request-middleware {:muuntaja m} ;; Note the use of the explicit instance of Muuntaja here
                 ;; coercing response bodys
                 ring-coercion/coerce-response-middleware
                 ;; coercing request parameters
                 ring-coercion/coerce-request-middleware
                 ;; multipart
                 multipart/multipart-middleware]

    :compile coercion/compile-request-coercers
    :exception pretty/exception}
The m instance works on it’s own, but all requests still fail validation / coercion because I think that might be done earlier in the chain? (See :coercion malli-coercer) Does anyone know how I can get muuntaja to do it’s transformations before the coercion takes place? Also if there’s a simpler way to achieve the case rewriting I am interested in hearing that too.

matheusashton 2021-05-08T15:39:12.239600Z

I passed the muuntaja instance to :muuntaja root option, like this:

1✅
Zaymon 2021-05-09T23:19:46.248600Z

Thanks! I didn’t know you could do that. Now it’s breaking my swagger haha. Ah well

Margo 2021-05-08T14:46:48.239200Z

Hello, guys! Some help needed with reitit.frontend.easy I am trying to do a simple redirect on button click using (*rfe/push-state* ::item {:id 210}) but it seems like unless I have my routes are defines in the same namespace, it doesn't work. May somebody suggest whether that is expected behaviour?

lassemaatta 2021-05-08T14:53:01.239300Z

When you move the routes to another namespace, I assume you also change the route name (`::item`) to match the route namespace? For example if your routes are in my.app.routes namespace, you can require them as [my.app.routes :as routes] and then use that alias when referring to individual routes (`::routes/item`)

2👍
matheusashton 2021-05-08T15:39:12.239600Z

I passed the muuntaja instance to :muuntaja root option, like this:

1✅
matheusashton 2021-05-08T15:42:34.242100Z

I saw some useful converters in schema-tools.coerce like string->date but I don't know how to use it, with reitit-ring coercion middlewares, do I have to declare my schema as java.util.Date ? Does it require any wiring?