reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Kasey Speakman 2020-08-20T00:54:32.125Z

Is it possible to get path-params when listing routes on the router?

Kasey Speakman 2020-08-20T00:56:24.125200Z

compiled-routes does not have them

Kasey Speakman 2020-08-20T00:59:01.125900Z

Nvm, I think it will be easier to take a different approach

Jonas Svalin 2020-08-20T09:37:05.001700Z

I recently joined a new project where we use Reitit (first time for me). I’m trying to wrap my head around what the best way to have my JSON API convert my kebab-case clojure maps to camelCase as part of the JSON conversion

Jonas Svalin 2020-08-20T09:38:22.003Z

As part of our middleware chain we have response coercion and muuntaja/format-response-middleware. It seems to me like I should somehow tell muuntaja what encoded-key-fn to use, but I’m at a loss of where to add this property/how to pass it to muuntaja

ikitommi 2020-08-20T09:49:07.011600Z

managing the configs is too hard as there are so many ways and no schemas/specs to validate the library configs. Here, the steps: 1) play with jsonista configs in the repl, e.g. (jsonista.core/read-value .. (j/object-mapper configs-here)) The source code should show how to do that transformation 2) play with Muuntaja configs in the repl to inject the object-mapoer in, e.g. (muuntaja.core/decode ... (m/create config-here)) 3) inject the custom Muuntaja instance into the route data to make it effective

ikitommi 2020-08-20T09:52:02.015400Z

there is an Grand Idea of common configuration tool, based on Malli, which would have good defaults and single config that can be used for all.

Jonas Svalin 2020-08-20T09:53:11.015900Z

OK, I will try to see if I can use that, thanks @ikitommi

Jonas Svalin 2020-08-20T11:25:17.017Z

In case anyone else is curious, I managed to solve this by instantiating the muuntaja instance with slightly updated options:

(m/create
 (assoc-in m/default-options
  [:formats "application/json" :encoder-opts]
  {:encode-key-fn csk/->camelCaseString}))

👍 1
nando 2020-08-20T12:52:55.020600Z

I'm getting the following error after adding metosin/ring-http-response {:mvn/version "0.9.1"} to my deps.edn. "Error building classpath. Failed to read artifact descriptor for metosin:ring-http-response:jar:0.9.1" I've confirmed that the library is on Clojars here, https://clojars.org/metosin/ring-http-response , that the dependency designation is correct, and that commenting it out makes the error go away. Same happens for 0.9.0

nando 2020-08-20T12:54:09.021600Z

I've also tried pulling it in via github, using the following coordinates

metosin/ring-http-response {:git/url "<https://github.com/metosin/ring-http-response>"
                                     :sha "e6d7e24e41548e68d07bd7056a1aaf0b7f6a68c6"}

ikitommi 2020-08-20T12:54:27.021800Z

I’ll push deps.edn there

ikitommi 2020-08-20T12:54:42.022300Z

but, should work via maven, haven’t seen that :thinking_face:

nando 2020-08-20T12:54:46.022400Z

Error building classpath. Manifest type not detected when finding deps for metosin/ring-http-response in coordinate {:git/url "https://github.com/metosin/ring-http-response", :sha "e6d7e24e41548e68d07bd7056a1aaf0b7f6a68c6"}

nando 2020-08-20T12:55:18.022900Z

I may be making a more fundamental mistake ...

nando 2020-08-20T12:56:57.023700Z

Here's what I have in my deps.edn for completeness

:deps {org.clojure/clojure {:mvn/version "1.10.1"}
        ;; Web Application
        http-kit        {:mvn/version "2.4.0"}
        ring/ring-core  {:mvn/version "1.8.1"}
        ring/ring-devel {:mvn/version "1.8.1"}
        ring/ring-defaults  {:mvn/version "0.3.2"}
        compojure       {:mvn/version "1.6.1"}
        hiccup          {:mvn/version "2.0.0-alpha2"}
        metosin/reitit  {:mvn/version "0.5.5"}
        metosin/reitit-middleware  {:mvn/version "0.5.5"}
       ;; metosin/ring-http-response {:mvn/version "0.9.1"}
       metosin/ring-http-response {:git/url "<https://github.com/metosin/ring-http-response>"
                                     :sha "e6d7e24e41548e68d07bd7056a1aaf0b7f6a68c6"}

nando 2020-08-20T13:00:00.024700Z

I'm not 100% sure that the sha is correct. I used the following command to get it:

git ls-remote <https://github.com/metosin/ring-http-response> master

ikitommi 2020-08-20T13:00:50.025300Z

➜  ~ clojure -Sdeps '{:deps {metosin/ring-http-response {:mvn/version "0.9.1"}}}}}'
Clojure 1.10.1
user=&gt; (require '[ring.util.http-response :refer :all])
user=&gt; (continue)
; {:status 100, :headers {}, :body ""}

nando 2020-08-20T13:11:23.026500Z

@ikitommi Thank you very much. It's working for me now.

martinklepsch 2020-08-20T15:43:26.028100Z

With reitit.frontend is there a way to prevent the route from being updated in certain cases? E.g. a user clicks a link that requires auth but isn’t authenticated. In this case I’d like to show a modal and prevent the URL change from happening.

martinklepsch 2020-08-20T17:52:02.028400Z

Figured out a way to get this to work using ignore-anchor-click? I’m doing a bit more than just returning a boolean though which feels like I’m abusing this slightly

(defn ignore-anchor-click?
  "Intercept navigation events to show an authentication modal if the target
  handler requires `:auth?`. This wraps `reitit.frontend.history/ignore-anchor-click?`"
  [r router e el uri]
  (let [route-match (rfh/ignore-anchor-click? router e el uri)
        handler-name (-&gt; route-match :data :name)
        handler-map (get handlers/handlers handler-name)
        authenticated? (= :authenticated (-&gt; @r :user :auth-state))
        initial-pageload (not (-&gt; @r :router :handler)) ]
    (if (and route-match
             (or (:auth? handler-map)
                 (= handler-name :sign-in))
             (not authenticated?)
             ;; Use sign-in page instead of modal if this is the first page visited by the user.
             (not initial-pageload))
      ;; returning `false` means reitit will let the browser decide what to do
      ;; in order to not have page reload we also need to `preventDefault`
      (do (.preventDefault e)
          (ice/dispatch! :ui/open-auth-modal uri (:sign-in-header handler-map))
          false)
      route-match)))