Skip to content

Commit 32cd8b8

Browse files
committed
backport route-middleware
1 parent c78d377 commit 32cd8b8

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## NEXT
22
* Remove potemkin [#445](https://github.com/metosin/compojure-api/issues/445)
3+
* backport `route-middleware`
34

45
## 1.1.13 (2019-11-02)
56

src/compojure/api/core.clj

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require [compojure.api.meta :as meta]
33
[compojure.api.routes :as routes]
44
[compojure.api.middleware :as mw]
5+
[compojure.core :as compojure]
56
[clojure.tools.macro :as macro]))
67

78
(defn- handle [handlers request]
@@ -37,17 +38,31 @@
3738
(routes/create nil nil {} nil (partial handle handlers))))
3839

3940
(defmacro middleware
40-
"Wraps routes with given middlewares using thread-first macro.
41+
"Wraps routes with given middleware using thread-first macro.
4142
4243
Note that middlewares will be executed even if routes in body
43-
do not match the request uri. Be careful with middlewares that
44-
have side-effects."
45-
{:style/indent 1}
44+
do not match the request uri. Be careful with middleware that
45+
has side-effects."
46+
{:style/indent 1
47+
:deprecated "1.1.14"
48+
:superseded-by "route-middleware"}
4649
[middleware & body]
4750
`(let [body# (routes ~@body)
4851
wrap-mw# (mw/compose-middleware ~middleware)]
4952
(routes/create nil nil {} [body#] (wrap-mw# body#))))
5053

54+
(defn route-middleware
55+
"Wraps routes with given middleware using thread-first macro."
56+
{:style/indent 1
57+
:supercedes "middleware"}
58+
[middleware & body]
59+
(let [handler (apply routes body)
60+
x-handler (compojure/wrap-routes handler (mw/compose-middleware middleware))]
61+
;; use original handler for docs and wrapped handler for implementation
62+
(routes/map->Route
63+
{:childs [handler]
64+
:handler x-handler})))
65+
5166
(defmacro context {:style/indent 2} [& args] (meta/restructure nil args {:context? true}))
5267

5368
(defmacro GET {:style/indent 2} [& args] (meta/restructure :get args nil))

src/compojure/api/sweet.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +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 {: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 middlewares that\n have side-effects."} [middleware & body] (list* (quote compojure.api.core/middleware) middleware body))
7+
(defmacro middleware {:deprecated "1.1.14", :doc "Wraps routes with given middleware 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 middleware using thread-first macro."} route-middleware compojure.api.core/route-middleware)
89
(defmacro context [& args] (list* (quote compojure.api.core/context) args))
910
(defmacro GET [& args] (list* (quote compojure.api.core/GET) args))
1011
(defmacro ANY [& args] (list* (quote compojure.api.core/ANY) args))

test/compojure/api/dev/gen.clj

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

102102
(def compojure-api-sweet-impl-info
103-
{:vars '([compojure.api.core routes defroutes let-routes undocumented middleware
103+
{:vars '([compojure.api.core routes defroutes let-routes undocumented middleware route-middleware
104104
context GET ANY HEAD PATCH DELETE OPTIONS POST PUT]
105105
[compojure.api.api api defapi]
106106
[compojure.api.resource resource]

0 commit comments

Comments
 (0)