Liking cljdoc? Tell your friends :D

Name-based (reverse) Routing

All routes which have :name route data defined, can also be matched by name.

Given a router:

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

(def router
  (r/router
    ["/api"
     ["/ping" ::ping]
     ["/user/:id" ::user]]))

Listing all route names:

(r/route-names router)
; [:user/ping :user/user]

No match returns nil:

(r/match-by-name router ::kikka)
nil

Matching a route:

(r/match-by-name router ::ping)
; #Match{:template "/api/ping"
;        :data {:name :user/ping}
;        :result nil
;        :path-params {}
;        :path "/api/ping"}

If not all path-parameters are set, a PartialMatch is returned:

(r/match-by-name router ::user)
; #PartialMatch{:template "/api/user/:id",
;               :data {:name :user/user},
;               :result nil,
;               :path-params nil,
;               :required #{:id}}

(r/partial-match? (r/match-by-name router ::user))
; true

With provided path-parameters:

(r/match-by-name router ::user {:id "1"})
; #Match{:template "/api/user/:id"
;        :data {:name :user/user}
;        :path "/api/user/1"
;        :result nil
;        :path-params {:id "1"}}

There is also a exception throwing version:

(r/match-by-name! router ::user)
; ExceptionInfo missing path-params for route /api/user/:id: #{:id}

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close