Fork me on GitHub
#reitit
<
2018-08-23
>
levitanong06:08:20

Hi all, is there a story for using routes that don’t have associated handler functions

["/ ::index
  ["settings" ::settings]
with ring/router?

levitanong06:08:40

I want to use the same routing tree for both frontend and backend to aid in SSR.

levitanong07:08:59

My current strategy is to flatten the routes, then map over each route, associating the appropriate handler for the key. Not sure if there is a more idiomatic way of accomplishing the same task

Hannu Hartikainen07:08:03

I’ve been thinking about sharing the routes between clj and cljs too. So you just have a map of {::settings settings-handler} etc and merge those handlers to the route list? Any issues so far?

ikitommi08:08:36

@levitanong @hannu.hartikainen great question! the 0.2.0 already supports middleware & interceptor registries, but bot handler registries (https://metosin.github.io/reitit/ring/middleware_registry.html)

ikitommi08:08:20

I think the :name might not be best for this, as the it’s a property of an route, not an endpoint.

ikitommi08:08:52

e.g. ["/kikka" {:name ::kikka, {:get get-kikka, :post :post-kikka}}]

ikitommi08:08:04

should the endpoints have a unique identifier like :id that could be used?

ikitommi08:08:01

["/kikka" 
 {:name ::kikka
  :get {:id ::get-kikka}
  :post {:id ::post-kikka}}]

levitanong09:08:19

@ikitommi would the client then use ::kikka, while the server uses ::get-kikka and ::post-kikka?

ikitommi09:08:52

if the client would like to use reverse-routing, currently it would need to do match-by-name and then know which method to invoke I guess.

Hannu Hartikainen09:08:21

my intuition is that it would be better to just have the handlers per verb in the handler map (`{::kikka {:get kikka-get-handler, :post kikka-post-handler}}`) as usually you have a mental model of each url

ikitommi09:08:01

sounds legit. I think you can use reader conditionals too?

Hannu Hartikainen09:08:36

i have no idea what those are 😄 (clojure noob here)

levitanong09:08:36

Or you could go a level above handlers, and make use of the names themselves.

;; foo is some hypothetical function that would resolve this stuff
(foo
  ["/kikka" ::kikka
    ["/bar" ::bar]
  {::kikka {:get get-handler :post post-handler}
   ::bbar bar-handler)

ikitommi09:08:41

with reader conditionals:

["/kikka"
 {:name ::kikka
  :get #?(:clj {:handler get-kikka})
  :post #?(:clj {:handler get-kikka})}]

👍 4
levitanong09:08:44

oh that might be equivalent to @hannu.hartikainen’s idea

ikitommi09:08:03

@levitanong there is a way to do that, will post soon

ikitommi09:08:46

(require '[reitit.core :as r])

(defn my-expand [registry]
  (fn [data opts]
    (or (if (keyword? data)
          (some-> data
                  registry
                  (r/expand opts)
                  (assoc :name data)))
        (r/expand data opts))))

(-> (r/router
      [["/kikka" ::kikka]
       ["/bar" ::bar]]
      {:expand (my-expand
                 {::kikka {:get 'get-kikka
                           :post 'post-kikka}})})
    (r/routes))
;[["/kikka" {:get get-kikka, :post post-kikka, :name :user/kikka}]
; ["/bar" {:name :user/bar}]]

levitanong09:08:59

oooh cool thanks @ikitommi!

mkvlr09:08:12

I see the PR has been merged and released, great, thank you 🙏

mkvlr09:08:49

actually, not released yet (had a stale dep) but master works