reitit/doc/basics/route_syntax.md

92 lines
1.7 KiB
Markdown
Raw Normal View History

2017-10-29 07:29:06 +00:00
# Route Syntax
Routes are defined as vectors of String path and optional (non-sequential) route argument child routes.
Routes can be wrapped in vectors and lists and `nil` routes are ignored.
Paths can have path-parameters (`:id`) or catch-all-parameters (`*path`).
### Examples
2017-10-29 07:29:06 +00:00
Simple route:
```clj
["/ping"]
```
Two routes:
```clj
[["/ping"]
["/pong"]]
```
Routes with route arguments:
```clj
[["/ping" ::ping]
["/pong" {:name ::pong}]]
```
Routes with path parameters:
```clj
[["/users/:user-id"]
["/api/:version/ping"]]
```
Route with catch-all parameter:
```clj
["/public/*path"]
```
Nested routes:
```clj
["/api"
["/admin" {:middleware [::admin]}
["" ::admin]
["/db" ::db]]
["/ping" ::ping]]
```
Same routes flattened:
```clj
[["/api/admin" {:middleware [::admin], :name ::admin}]
["/api/admin/db" {:middleware [::admin], :name ::db}]
["/api/ping" {:name ::ping}]]
```
### Generating routes
2018-02-11 17:15:25 +00:00
Routes are just data, so it's easy to create them programmatically:
2017-10-29 07:29:06 +00:00
```clj
2018-02-11 17:15:25 +00:00
(defn cqrs-routes [actions]
2017-10-29 07:29:06 +00:00
["/api" {:interceptors [::api ::db]}
(for [[type interceptor] actions
:let [path (str "/" (name interceptor))
method (condp = type
:query :get
:command :post)]]
2018-02-11 17:15:25 +00:00
[path {method {:interceptors [interceptor]}}])])
2017-10-29 07:29:06 +00:00
```
```clj
(cqrs-routes
[[:query 'get-user]
[:command 'add-user]
2018-02-11 17:15:25 +00:00
[:command 'add-order]])
2017-10-29 07:29:06 +00:00
; ["/api" {:interceptors [::api ::db]}
; (["/get-user" {:get {:interceptors [get-user]}}]
; ["/add-user" {:post {:interceptors [add-user]}}]
2018-02-11 17:15:25 +00:00
; ["/add-order" {:post {:interceptors [add-order]}}])]
2017-10-29 07:29:06 +00:00
```
### Encoding
Reitit does not apply any encoding to your paths. If you need that, you must encode them yourself.
E.g., `/foo bar` should be `/foo%20bar`.