Skip to content

Commit 2529df3

Browse files
committed
add back middleware and deprecate it
1 parent fa5fb01 commit 2529df3

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ See also: [compojure-api 1.1.x changelog](./CHANGELOG-1.1.x.md)
66
* Add back `defapi` (and deprecate it)
77
* Remove potemkin [#445](https://github.com/metosin/compojure-api/issues/445)
88
* Add back `compojure.api.routes/create`
9+
* Add back `middleware` (and deprecate it)
910

1011
## 2.0.0-alpha31 (2019-12-20)
1112

src/compojure/api/core.clj

+16-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,24 @@
4444
(let [handlers (keep identity handlers)]
4545
(routes/map->Route {:handler (meta/routing handlers)})))
4646

47+
(defmacro middleware
48+
"Wraps routes with given middlewares using thread-first macro.
49+
50+
Note that middlewares will be executed even if routes in body
51+
do not match the request uri. Be careful with middleware that
52+
has side-effects."
53+
{:style/indent 1
54+
:deprecated "1.1.14"
55+
:superseded-by "route-middleware"}
56+
[middleware & body]
57+
`(let [body# (routes ~@body)
58+
wrap-mw# (mw/compose-middleware ~middleware)]
59+
(routes/create nil nil {} [body#] (wrap-mw# body#))))
60+
4761
(defn route-middleware
4862
"Wraps routes with given middlewares using thread-first macro."
49-
{:style/indent 1}
63+
{:style/indent 1
64+
:supercedes "middleware"}
5065
[middleware & body]
5166
(let [handler (apply routes body)
5267
x-handler (compojure/wrap-routes handler (mw/compose-middleware middleware))]

src/compojure/api/sweet.clj

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
(defmacro defroutes {:doc "Define a Ring handler function from a sequence of routes.\n The name may optionally be followed by a doc-string and metadata map."} [name & routes] (list* (quote compojure.api.core/defroutes) name routes))
55
(defmacro let-routes {:doc "Takes a vector of bindings and a body of routes.\n\n Equivalent to: `(let [...] (routes ...))`"} [bindings & body] (list* (quote compojure.api.core/let-routes) bindings body))
66
(def ^{:arglists (quote ([& handlers])), :doc "Routes without route-documentation. Can be used to wrap routes,\n not satisfying compojure.api.routes/Routing -protocol."} undocumented compojure.api.core/undocumented)
7+
(defmacro middleware {:deprecated "1.1.14", :doc "Wraps routes with given middlewares using thread-first macro.\n\n Note that middlewares will be executed even if routes in body\n do not match the request uri. Be careful with middleware that\n has side-effects."} [middleware & body] (list* (quote compojure.api.core/middleware) middleware body))
8+
(def ^{:arglists (quote ([middleware & body])), :doc "Wraps routes with given middlewares using thread-first macro."} route-middleware compojure.api.core/route-middleware)
79
(defmacro context [& args] (list* (quote compojure.api.core/context) args))
810
(defmacro GET [& args] (list* (quote compojure.api.core/GET) args))
911
(defmacro ANY [& args] (list* (quote compojure.api.core/ANY) args))

test/compojure/api/dev/gen.clj

+1-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@
100100
syms)))
101101

102102
(def compojure-api-sweet-impl-info
103-
{:vars '([compojure.api.core routes defroutes let-routes undocumented
104-
;;BREAKING CHANGE FROM 1.1.x
105-
;middleware
103+
{:vars '([compojure.api.core routes defroutes let-routes undocumented middleware route-middleware
106104
context GET ANY HEAD PATCH DELETE OPTIONS POST PUT]
107105
[compojure.api.api api defapi]
108106
[compojure.api.resource resource]

0 commit comments

Comments
 (0)