Skip to content

Commit

Permalink
Fix swagger endpoints and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
gmsvalente committed Nov 10, 2023
1 parent 776663a commit 8f4e4a9
Showing 1 changed file with 59 additions and 53 deletions.
112 changes: 59 additions & 53 deletions src/xiana/swagger.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,70 @@
[reitit.trie :as trie]
[ring.util.response]))


;; "xiana-route->reitit-route is taking route entry of our custom shape of routes
;; and transforms it into proper reitit route entry that is valid on the Swagger
;; implemention of reitit.

;; (xiana-route->reitit-route [\"/swagger-ui\" {:action :swagger-ui
;; :some-values true}])
;; ;; => [\"/swagger-ui\"
;; {:get
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :patch
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :trace
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :connect
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :delete
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :head
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :post
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :action :swagger-ui,
;; :options
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :put
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :some-values true}]
;; "

(def all-methods
[:get :patch :trace :connect :delete :head :post :options :put])

(defn- no-method?
[opt-map]
(:action opt-map))

(defn- reduce-opt-map
[opt-map all-methods]
(reduce (fn [acc method]
(if (get acc method)
(if (get-in acc [method :handler])
acc
(-> acc
(assoc-in [method :handler] identity)))
acc))
opt-map
all-methods))

(defn- process-opt-map
[opt-map all-methods]
(if (no-method? opt-map)
(-> opt-map
(assoc-in [:get :handler] identity)
(assoc-in [:get :action] (:action opt-map)))
(reduce-opt-map opt-map all-methods)))


(defn xiana-route->reitit-route
"xiana-route->reitit-route is taking route entry of our custom shape of routes
and transforms it into proper reitit route entry that is valid on the Swagger
implemention of reitit.
(xiana-route->reitit-route [\"/swagger-ui\" {:action :swagger-ui
:some-values true}])
;; => [\"/swagger-ui\"
{:get
{:handler #function[clojure.core/identity], :action :swagger-ui},
:patch
{:handler #function[clojure.core/identity], :action :swagger-ui},
:trace
{:handler #function[clojure.core/identity], :action :swagger-ui},
:connect
{:handler #function[clojure.core/identity], :action :swagger-ui},
:delete
{:handler #function[clojure.core/identity], :action :swagger-ui},
:head
{:handler #function[clojure.core/identity], :action :swagger-ui},
:post
{:handler #function[clojure.core/identity], :action :swagger-ui},
:action :swagger-ui,
:options
{:handler #function[clojure.core/identity], :action :swagger-ui},
:put
{:handler #function[clojure.core/identity], :action :swagger-ui},
:some-values true}]
"
[[url opt-map & nested-routes :as route] all-methods]
(let [new-opt-map (if (:action opt-map)
(let [action' (:action opt-map)
swagger-base-of-endpoint (:swagger-* opt-map)]
(reduce (fn [acc method]
(-> acc
(assoc-in [method :handler] identity)
(assoc-in [method :action] action')
(merge swagger-base-of-endpoint)))
opt-map
all-methods))
(let [swagger-base-of-endpoint (get opt-map :swagger-* {})]
(reduce (fn [acc method]
(if (get acc method)
(if (get-in acc [method :handler])
acc
(-> acc
(assoc-in [method :handler] identity)
(merge swagger-base-of-endpoint)))
acc))
opt-map
all-methods)))]
(if (-> route meta :no-doc)
nil
(apply conj [url new-opt-map]
(map #(xiana-route->reitit-route % all-methods) nested-routes)))))
(when-not (-> route meta :no-doc)
(apply conj
[url (process-opt-map opt-map all-methods)]
(map #(xiana-route->reitit-route % all-methods) nested-routes))))

(defn xiana-routes->reitit-routes
"Transforms routes to the proper reitit form."
Expand Down

0 comments on commit 8f4e4a9

Please sign in to comment.