Fork me on GitHub
#reitit
<
2018-11-22
>
danielgrosse12:11:08

Hej, just discovered reitit and play a little around with it. How can I define optional parameters, like for path users/:id

valerauko12:11:43

so you want it to have both a users/ route and users/:id routes?

danielgrosse13:11:10

yes I want to get alle users or a specific user

valerauko13:11:09

["/users"
 ["" ::users/index]
 ["/:id" ::users/show]]
like this?

👍 8
ikitommi13:11:13

another option to use flattened routes:

[["/users" ::users/index]
 ["/users/:id" ::users/show]]
… both compile to identical internal routing tree, so identical perf etc.

danielgrosse13:11:26

And when I use

["/users"
       {:get {:summary "get users"
              :parameters {:id}}}]

valerauko13:11:49

that'd be

["/users"
 ["/:id" {:get {:parameters {:path {:id int?}}
                :handler users-show}}]]
like this

👍 4
danielgrosse13:11:12

::users/index is the function index in ns users?

ikitommi13:11:16

["/users" ::users/index] expands to ["/users" {:name ::users/index}], which is usually good enough in the frontend.

ikitommi13:11:31

for ring/http, you need the :handler to serve the requests

ikitommi13:11:56

adding to @vale latest example:

["/users"
 ["" {:get get-users}]
 ["/:id" {:get {:parameters {:path {:id int?}}
                :handler users-show}}]]

ikitommi13:11:12

here the get-users and show-users are the ring-handlers

ikitommi13:11:11

there are few places where the router expands the route syntax, in the the {:get get-users} expands to {:get {:handler get-users}}.