Fork me on GitHub
#reitit
<
2021-09-24
>
Ian McQuoid05:09:54

Hello! I'm having an issue with adding coercion middleware (but since I'm very new to all of this, I'm having trouble explaining my problem correctly 🥲) I'm currently building a webapp on top of the luminus template for lein and when I add my own middleware to a route, it seems to overwrite(?) or generally stop the template's error reporting middleware. It's very likely that with how the template handles middleware that I can't just add

:middleware [foo]
as I'd like to, but I'm hopeful that there's an easy fix 🙂

Ian McQuoid05:09:13

Oh, after fiddling with it, I think that it's because I'm registering two exception handling middleware at the same time (the coercion one is a merge of the default handlers and one looking for request-coercion errors).

Ian McQuoid05:09:45

Does anyone know of a way to make these two play nicely?

drewverlee15:09:52

Could someone help me understand what it means here https://cljdoc.org/d/metosin/reitit/0.5.15/doc/coercion/coercion-explained#compiling-coercers > Before the actual coercion, we should need to compile the coercers against the route data. Compiled coercers yield much better performance and the manual step of adding a coercion compiler makes things explicit and non-magical. I see you need to provide a corecion/compile-rquest-coercers value

(require '[reitit.coercion :as coercion])
(require '[reitit.coercion.schema])
(require '[schema.core :as s])

(def router
  (r/router
    ["/:company/users/:user-id" {:name ::user-view
                                 :coercion reitit.coercion.schema/coercion
                                 :parameters {:path {:company s/Str
                                                     :user-id s/Int}}}]
    {:compile coercion/compile-request-coercers}))
So compiling happens before run time, what can be done here that isn't at run time as the search param arguments need to be changed at run time?

drewverlee15:09:17

> The compiler added a :result key into the match (done just once, at router creation time), which holds the compiled coercers. We are almost done.

drewverlee15:09:57

i'm confused, wouldn't that cljs coercion functions get compiled by the compiler normally to a js object?

ikitommi15:09:44

@drewverlee router compilation happens at language runtime: route tree, models and middleware/interceptor/controller chains are read from the (data-)definitions and optimized pure functions are returned. If you need to change those definitions, you need to re-create the router - or use dynamic routing, which is described in the docs.

drewverlee16:09:46

as an example are we saying that when the routes are compiled

{:path {:company s/Str :user-id s/Int}}
is turned into (fn [company user-id] ...)