Fork me on GitHub
#reitit
<
2018-06-21
>
arlicle09:06:32

how can I add the :options to every :get or :port uri in one place?

arlicle09:06:30

I add :option to every uri my hand

ikitommi11:06:53

@arlicle hi. it’s all data, so you have at least two good options: 1) create the endpoints with a wrapper function, that adds the :option, something like:

(defn endpoint [data]
  (assoc data :options {:handler ...}))

["/api"
  ["/something" (endpoint {:get ...})]]
2) transform the routes and add the endpoints later:
(def router
  (ring/router
    ["/api"
     ["/ping" {:get identity}]
     ["/pong" {:get identity}]]))


(defn router-with-options [router]
  (ring/router 
    (map (fn [[path data]]
           [path (assoc data :options identity)])
         (r/routes router))
    (r/options router)))

(router-with-options router)

ikitommi11:06:51

e.g. the Router can be reconstructed from another router, via r/routes and r/options. the route tree is flattened, so one doesn’t have to walk the routes to find the endpoint data. it’s in the second position in the vector. There are Specs for these too.

ikitommi11:06:28

one can also merge routers, but like all merge, the last one wins - if you merge a forced :linear-router and a forced :segment-tree-router it will become segment-tree-router (and might fail at creation time if there are router conflicts, not allowed in forced :segment-tree-router).

ikitommi11:06:31

hope this helps.

arlicle12:06:36

thank you very much 🙂