Skip to content

Commit

Permalink
add back middleware and deprecate it
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Apr 22, 2024
1 parent fa5fb01 commit 2529df3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 16 additions & 1 deletion src/compojure/api/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
2 changes: 2 additions & 0 deletions src/compojure/api/sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 1 addition & 3 deletions test/compojure/api/dev/gen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2529df3

Please sign in to comment.