diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fe51662..891f8e38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ See also: [compojure-api 1.1.x changelog](./CHANGELOG-1.1.x.md) * Add back `defapi` (and deprecate it) * Remove potemkin [#445](https://github.com/metosin/compojure-api/issues/445) * Add back `compojure.api.routes/create` +* Add back `middleware` (and deprecate it) ## 2.0.0-alpha31 (2019-12-20) diff --git a/src/compojure/api/core.clj b/src/compojure/api/core.clj index d6e091a6..be061eb3 100644 --- a/src/compojure/api/core.clj +++ b/src/compojure/api/core.clj @@ -44,9 +44,24 @@ (let [handlers (keep identity handlers)] (routes/map->Route {:handler (meta/routing handlers)}))) +(defmacro middleware + "Wraps routes with given middlewares using thread-first macro. + + Note that middlewares will be executed even if routes in body + do not match the request uri. Be careful with middleware that + has side-effects." + {:style/indent 1 + :deprecated "1.1.14" + :superseded-by "route-middleware"} + [middleware & body] + `(let [body# (routes ~@body) + wrap-mw# (mw/compose-middleware ~middleware)] + (routes/create nil nil {} [body#] (wrap-mw# body#)))) + (defn route-middleware "Wraps routes with given middlewares using thread-first macro." - {:style/indent 1} + {:style/indent 1 + :supercedes "middleware"} [middleware & body] (let [handler (apply routes body) x-handler (compojure/wrap-routes handler (mw/compose-middleware middleware))] diff --git a/src/compojure/api/sweet.clj b/src/compojure/api/sweet.clj index 44f0b188..737a45c3 100644 --- a/src/compojure/api/sweet.clj +++ b/src/compojure/api/sweet.clj @@ -4,6 +4,8 @@ (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)) (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)) (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) +(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)) +(def ^{:arglists (quote ([middleware & body])), :doc "Wraps routes with given middlewares using thread-first macro."} route-middleware compojure.api.core/route-middleware) (defmacro context [& args] (list* (quote compojure.api.core/context) args)) (defmacro GET [& args] (list* (quote compojure.api.core/GET) args)) (defmacro ANY [& args] (list* (quote compojure.api.core/ANY) args)) diff --git a/test/compojure/api/dev/gen.clj b/test/compojure/api/dev/gen.clj index 91e7167c..b3002079 100644 --- a/test/compojure/api/dev/gen.clj +++ b/test/compojure/api/dev/gen.clj @@ -100,9 +100,7 @@ syms))) (def compojure-api-sweet-impl-info - {:vars '([compojure.api.core routes defroutes let-routes undocumented - ;;BREAKING CHANGE FROM 1.1.x - ;middleware + {:vars '([compojure.api.core routes defroutes let-routes undocumented middleware route-middleware context GET ANY HEAD PATCH DELETE OPTIONS POST PUT] [compojure.api.api api defapi] [compojure.api.resource resource]