Fork me on GitHub
#reitit
<
2024-01-27
>
Luciano Laratelli18:01:43

Hello! I’m trying to get some query parameters going with reitit.frontend and reitit.coercion.spec . Here’s my route and app :

(def routes [["/"
              {:name :the/home-page
               :view home-page
               :query (s/keys :opt-un [::code])}]])

(defui app []
  (rfe/start! (rf/router routes {:data {:coercion rss/coercion}})
              (fn [m] (reset! match m))
              {:use-fragment true})
  (if @match
    (let [view (-> @match :data :view)]
      (pp/pprint @match)
      (view @match))
    [:pre (pp/pprint @match)]))
If I visit , this is the value for @match :
{:template "/",
 :data
 {:coercion #object[reitit.coercion.spec.t_reitit$coercion$spec17707],
  :name :the/home-page,
  :view #object[app$core$home_page],
  :query #object[cljs.spec.alpha.t_cljs$spec$alpha19156]},
 :result nil,
 :path-params {},
 :path "/",
 :query-params {},
 :fragment nil, 
 :parameters {:path {}, :query {}, :fragment nil}}
Getting my validation function as the value of :query is unexpected, is that a correct expectation on my part? Would really appreciate some pointers on what I’m doing wrong here.

Janet A. Carr18:01:44

Don't you need to set up a controller for your route and add the query params there? https://github.com/metosin/reitit/blob/master/doc/frontend/controllers.md

Janet A. Carr18:01:01

Specifically, I think you need to call apply-controllers

Janet A. Carr18:01:33

I could be wrong though, it just seems to be what all my frontend reitit code does 😅

Luciano Laratelli18:01:12

thanks for that! I don’t think so, unless I did it wrong. I tried this:

(s/def ::code string?)

(defn home-page [{{{:keys [code]} :query} :parameters :as params}]
  (pp/pprint params)
  (if (str/blank? code)
    ($ :a {:href access-token-uri} "Authenticate with YNAB")
    ($ :h1 (str "You are logged in! Your access code is " code))))

(def routes [["/"
              {:name :the/home-page
               :view home-page
               :parameters {:query (s/keys :opt-un [::code])}
               :controllers [{:parameters {:query [:code]}
                              :start (pp/pprint "starting home page")}]}]])

(defui app []
  (rfe/start! (rf/router routes {:data {:coercion rss/coercion}})
              (fn [new-match]
                (swap! match
                       (fn [old-match]
                         (when new-match
                           (assoc new-match
                                  :controllers (rfc/apply-controllers (:controllers old-match) new-match))))))

              {:use-fragment true})
  (if @match
    (let [view (-> @match :data :view)]
      (view @match))
    [:pre (pp/pprint @match)]))

teodorlu22:01:57

Hi! I don’t understand this paragraph (https://cljdoc.org/d/metosin/reitit/0.7.0-alpha7/doc/advanced/composing-routers). • Nested routers are not trivial and should be avoided. • For dynamic cases, nested routers is the only choice. • For other cases, nested routers are most likely a better option. Am I misunderstanding something here?

teodorlu22:01:00

is “nested routes” different from “nested routers”?

teodorlu22:01:59

I believe that was it ☝️ nested routes is the way it’s normally done. A router is created from plain data. nested routers is when a router object refers to another router object.

teodorlu22:01:09

unrelated, reitit does support interceptors now, right? I found this line in the docs: > • Pedestal supports interceptors (`reitit-http` module will support them too). https://github.com/metosin/reitit/blob/989ab72a58c1b220ffdae4810bb145ab52082d2c/doc/faq.md#L89. Git blame date is from 2018.

teodorlu22:01:44

I really appreciate Reitit’s docs. I come with questions, and I’m consistently able to find good answers. Both about how to do something, and about why a choice has been made.