Skip to content

Commit

Permalink
backport route-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Apr 22, 2024
1 parent c78d377 commit 32cd8b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
* Remove potemkin [#445](https://github.com/metosin/compojure-api/issues/445)
* backport `route-middleware`

## 1.1.13 (2019-11-02)

Expand Down
23 changes: 19 additions & 4 deletions src/compojure/api/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [compojure.api.meta :as meta]
[compojure.api.routes :as routes]
[compojure.api.middleware :as mw]
[compojure.core :as compojure]
[clojure.tools.macro :as macro]))

(defn- handle [handlers request]
Expand Down Expand Up @@ -37,17 +38,31 @@
(routes/create nil nil {} nil (partial handle handlers))))

(defmacro middleware
"Wraps routes with given middlewares using thread-first macro.
"Wraps routes with given middleware using thread-first macro.
Note that middlewares will be executed even if routes in body
do not match the request uri. Be careful with middlewares that
have side-effects."
{:style/indent 1}
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 middleware using thread-first macro."
{:style/indent 1
:supercedes "middleware"}
[middleware & body]
(let [handler (apply routes body)
x-handler (compojure/wrap-routes handler (mw/compose-middleware middleware))]
;; use original handler for docs and wrapped handler for implementation
(routes/map->Route
{:childs [handler]
:handler x-handler})))

(defmacro context {:style/indent 2} [& args] (meta/restructure nil args {:context? true}))

(defmacro GET {:style/indent 2} [& args] (meta/restructure :get args nil))
Expand Down
3 changes: 2 additions & 1 deletion src/compojure/api/sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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 {: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))
(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))
(def ^{:arglists (quote ([middleware & body])), :doc "Wraps routes with given middleware 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
2 changes: 1 addition & 1 deletion test/compojure/api/dev/gen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
syms)))

(def compojure-api-sweet-impl-info
{:vars '([compojure.api.core routes defroutes let-routes undocumented 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 32cd8b8

Please sign in to comment.