reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Ovidiu Stoica 2021-05-25T04:54:57.029700Z

Hello guys 👋! First of all thank you for reitit! It helped me a lot. How would you go about having all your server responses turned to camelCase (FE is in React). I want this to be automatic (something like a middleware) but is it possible to have middleware trigger after the handler function triggered?

dharrigan 2021-05-25T09:55:50.030300Z

Hey @ovidiu.stoica1094 I have something that may interest you. A moment and I'll push it up to github

🙌 1
dharrigan 2021-05-25T10:02:45.030600Z

<https://github.com/dharrigan/camel-case/blob/master/src/camel_case/main.clj>

dharrigan 2021-05-25T10:02:49.030900Z

hope it helps!

admarrs 2021-05-25T10:58:39.034600Z

Hello! I'm trying (and failing) to use reitit frontend to force a re-direct to the "/" route if an unauthenticated user tries to directly navigate to a non-public route. For example "/#/entities/1" redirects to "/". Whats the best pattern for achieving this?

dharrigan 2021-05-25T12:05:29.034700Z

You do also have another option:

dharrigan 2021-05-25T12:05:32.034900Z

(require '[camel-snake-kebab.core :as csk])

(def m
  (m/create
    (assoc-in
      m/default-options
      [:formats "application/json" :encoder-opts]
      {:encode-key-fn csk/-&gt;camelCase})))

(-&gt;&gt; {:some-property "some-value"}
     (m/encode m "application/json")
     slurp)
; =&gt; "{\":someProperty\":\"some-value\"}"

dharrigan 2021-05-25T12:05:46.035100Z

where m == muuntaja

Ovidiu Stoica 2021-05-25T12:27:03.035300Z

Thank you very much @dharrigan!

dharrigan 2021-05-25T12:30:14.035500Z

You're most welcome 🙂

dharrigan 2021-05-25T12:37:54.035700Z

I have another example here, where you might like to strip away nil/empty values...

dharrigan 2021-05-25T12:38:30.035900Z

<https://github.com/dharrigan/strip-nils/blob/master/src/strip_nils/main.clj>

dharrigan 2021-05-25T14:39:14.036500Z

Hello @admarrs Hi, I have a very very very simple example, here...`https://github.com/dharrigan/startrek-ui/blob/master/src/startrek/main.cljs`

dharrigan 2021-05-25T14:39:37.037Z

It doesn't do any backend authentication, simply relies upon a button to be pressed (that sets an atom) beforehand

dharrigan 2021-05-25T14:40:01.037600Z

if you try to navigate to "/#/the-queen" directly, without pressing the button, you'll get a "go-away" page :)

dharrigan 2021-05-25T14:40:36.038Z

You can see it running here: <https://startrek.harrigan.online/#/>

dharrigan 2021-05-25T14:41:39.038700Z

If you were to replace, line 34 in 'startrek.main', with [views/render], it would take the user back to the initial landing page

dharrigan 2021-05-25T14:41:50.039Z

(thus, negating the need for the go-away page)

dharrigan 2021-05-25T14:42:01.039300Z

anyhoooo, it's just an example :-)

admarrs 2021-05-25T16:16:36.042200Z

Hi @dharrigan, thanks for the example. Though it's more of a re-render than a re-direct since the go-away page is rendered on the /#/the-queen route (i.e the url isn't re-directed). If that makes sense..

admarrs 2021-05-25T16:32:20.042300Z

Sorted with a hat tip to @dharrigan by adding a push-state if user isn't defined:

admarrs 2021-05-25T16:33:34.042500Z

(defn app
  []
  (let [user @(subscribe [:user])
        current-route @(subscribe [:current-route])]
    (if-not user (rfe/push-state :home))
    [:div
     [nav {:current-route current-route}]
     (when current-route
       [(-&gt; current-route :data :view)])]))

dharrigan 2021-05-25T17:16:38.042800Z

:thumbsup: