Is it possible to get path-params when listing routes on the router?
compiled-routes
does not have them
Nvm, I think it will be easier to take a different approach
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
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
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
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.
OK, I will try to see if I can use that, thanks @ikitommi
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}))
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
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"}
I’ll push deps.edn there
but, should work via maven, haven’t seen that :thinking_face:
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"}
I may be making a more fundamental mistake ...
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"}
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
➜ ~ clojure -Sdeps '{:deps {metosin/ring-http-response {:mvn/version "0.9.1"}}}}}'
Clojure 1.10.1
user=> (require '[ring.util.http-response :refer :all])
user=> (continue)
; {:status 100, :headers {}, :body ""}
@ikitommi Thank you very much. It's working for me now.
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.
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 (-> route-match :data :name)
handler-map (get handlers/handlers handler-name)
authenticated? (= :authenticated (-> @r :user :auth-state))
initial-pageload (not (-> @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)))