Update docs

This commit is contained in:
Tommi Reiman 2019-06-09 20:46:20 +03:00
parent c9281f0e89
commit 46897f3927
6 changed files with 56 additions and 16 deletions

View file

@ -21,6 +21,24 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino
[metosin/jsonista "0.2.3"] is available but we use "0.2.2"
```
### `reitit-core`
* Add support for explixit selection of router path-parameter `:syntax`, fixes [#276](https://github.com/metosin/reitit/issues/276)
```clj
(require '[reitit.core :as r])
(-> (r/router
["http://localhost:8080/api/user/{id}" ::user-by-id]
{:syntax :bracket})
(r/match-by-path "http://localhost:8080/api/user/123"))
;#Match{:template "http://localhost:8080/api/user/{id}",
; :data {:name :user/user-by-id},
; :result nil,
; :path-params {:id "123"},
; :path "http://localhost:8080/api/user/123"}
```
## 0.3.7 (2019-05-25)
### `reitit-pedestal`

View file

@ -2,12 +2,13 @@
Routers can be configured via options. The following options are available for the `reitit.core/router`:
| key | description |
|--------------|-------------|
| key | description
|--------------|-------------
| `:path` | Base-path for routes
| `:routes` | Initial resolved routes (default `[]`)
| `:data` | Initial route data (default `{}`)
| `:spec` | clojure.spec definition for a route data, see `reitit.spec` on how to use this
| `:syntax` | Path-parameter syntax as keyword or set of keywords (default #{:bracket :colon})
| `:expand` | Function of `arg opts => data` to expand route arg to route data (default `reitit.core/expand`)
| `:coerce` | Function of `route opts => route` to coerce resolved route, can throw or return `nil`
| `:compile` | Function of `route opts => result` to compile a route handler

View file

@ -4,7 +4,7 @@ Routes are defined as vectors of String path and optional (non-sequential) route
Routes can be wrapped in vectors and lists and `nil` routes are ignored.
Paths can have path-parameters (`:id`) or catch-all-parameters (`*path`). Since version `0.3.0`, parameters can also be wrapped in brackets, enabling use of qualified keywords `{user/id}`, `{*user/path}`. The non-bracket syntax might be deprecated later.
Paths can have path-parameters (`:id`) or catch-all-parameters (`*path`). Parameters can also be wrapped in brackets, enabling use of qualified keywords `{user/id}`, `{*user/path}`. By default, both syntaxes are supported, see [configuring routers](../advanced/configuring_routers.md) on how to change this.
### Examples
@ -129,3 +129,23 @@ Routes are just data, so it's easy to create them programmatically:
; ["/add-user" {:post {:interceptors [add-user]}}]
; ["/add-order" {:post {:interceptors [add-order]}}])]
```
### Explicit path-parameter syntax
Router options `:syntax` allows the path-parameter syntax to be explicitely defined. It takes a keyword or set of keywords as a value. Valid values are `:colon` and `:bracket`. Default value is `#{:colon :bracket}`.
Supporting only `:bracket` syntax:
```clj
(require '[reitit.core :as r])
(-> (r/router
["http://localhost:8080/api/user/{id}" ::user-by-id]
{:syntax :bracket})
(r/match-by-path "http://localhost:8080/api/user/123"))
;#Match{:template "http://localhost:8080/api/user/{id}",
; :data {:name :user/user-by-id},
; :result nil,
; :path-params {:id "123"},
; :path "http://localhost:8080/api/user/123"}
```

View file

@ -347,12 +347,13 @@
Selects implementation based on route details. The following options
are available:
| key | description |
| -------------|-------------|
| key | description
| -------------|-------------
| `:path` | Base-path for routes
| `:routes` | Initial resolved routes (default `[]`)
| `:data` | Initial route data (default `{}`)
| `:spec` | clojure.spec definition for a route data, see `reitit.spec` on how to use this
| `:syntax` | Path-parameter syntax as keyword or set of keywords (default #{:bracket :colon})
| `:expand` | Function of `arg opts => data` to expand route arg to route data (default `reitit.core/expand`)
| `:coerce` | Function of `route opts => route` to coerce resolved route, can throw or return `nil`
| `:compile` | Function of `route opts => result` to compile a route handler

View file

@ -57,9 +57,9 @@
(keyword (subs s 0 i) (subs s (inc i)))
(keyword s)))
(defn split-path [s {:keys [parameter-syntax] :or {parameter-syntax #{:bracket :colon}}}]
(let [bracket? (-> parameter-syntax (into-set) :bracket)
colon? (-> parameter-syntax (into-set) :colon)
(defn split-path [s {:keys [syntax] :or {syntax #{:bracket :colon}}}]
(let [bracket? (-> syntax (into-set) :bracket)
colon? (-> syntax (into-set) :colon)
-static (fn [from to] (if-not (= from to) [(subs s from to)]))
-wild (fn [from to] [(->Wild (-keyword (subs s (inc from) to)))])
-catch-all (fn [from to] [(->CatchAll (keyword (subs s (inc from) to)))])]

View file

@ -31,9 +31,9 @@
(deftest split-path-test
(testing "colon"
(doseq [parameter-syntax [:colon #{:colon}]]
(doseq [syntax [:colon #{:colon}]]
(are [path expected]
(is (= expected (trie/split-path path {:parameter-syntax parameter-syntax})))
(is (= expected (trie/split-path path {:syntax syntax})))
"/olipa/:kerran/avaruus", ["/olipa/" (trie/->Wild :kerran) "/avaruus"]
"/olipa/{kerran}/avaruus", ["/olipa/{kerran}/avaruus"]
@ -43,9 +43,9 @@
"/olipa/kerran/{*valtavan.suuri/avaruus}", ["/olipa/kerran/{" (trie/->CatchAll (keyword "valtavan.suuri/avaruus}"))])))
(testing "bracket"
(doseq [parameter-syntax [:bracket #{:bracket}]]
(doseq [syntax [:bracket #{:bracket}]]
(are [path expected]
(is (= expected (trie/split-path path {:parameter-syntax parameter-syntax})))
(is (= expected (trie/split-path path {:syntax syntax})))
"/olipa/:kerran/avaruus", ["/olipa/:kerran/avaruus"]
"/olipa/{kerran}/avaruus", ["/olipa/" (trie/->Wild :kerran) "/avaruus"]
@ -55,9 +55,9 @@
"/olipa/kerran/{*valtavan.suuri/avaruus}", ["/olipa/kerran/" (trie/->CatchAll :valtavan.suuri/avaruus)])))
(testing "both"
(doseq [parameter-syntax [#{:bracket :colon}]]
(doseq [syntax [#{:bracket :colon}]]
(are [path expected]
(is (= expected (trie/split-path path {:parameter-syntax parameter-syntax})))
(is (= expected (trie/split-path path {:syntax syntax})))
"/olipa/:kerran/avaruus", ["/olipa/" (trie/->Wild :kerran) "/avaruus"]
"/olipa/{kerran}/avaruus", ["/olipa/" (trie/->Wild :kerran) "/avaruus"]
@ -67,9 +67,9 @@
"/olipa/kerran/{*valtavan.suuri/avaruus}", ["/olipa/kerran/" (trie/->CatchAll :valtavan.suuri/avaruus)])))
(testing "nil"
(doseq [parameter-syntax [nil]]
(doseq [syntax [nil]]
(are [path expected]
(is (= expected (trie/split-path path {:parameter-syntax parameter-syntax})))
(is (= expected (trie/split-path path {:syntax syntax})))
"/olipa/:kerran/avaruus", ["/olipa/:kerran/avaruus"]
"/olipa/{kerran}/avaruus", ["/olipa/{kerran}/avaruus"]