Merge pull request #446 from zelark/update-handler

Update create-swagger-ui-handler
This commit is contained in:
Tommi Reiman 2020-10-22 17:23:07 +03:00 committed by GitHub
commit fb771a33bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,32 +14,26 @@
| :path | optional path to mount the handler to. Works only if mounted outside of a router. | :path | optional path to mount the handler to. Works only if mounted outside of a router.
| :config | parameters passed to swaggger-ui as-is. | :config | parameters passed to swaggger-ui as-is.
See https://github.com/swagger-api/swagger-ui/tree/2.x#parameters See https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
for all available :config options for all available :config options.
Examples: Examples:
```
(swagger-ui/create-swagger-ui-handler)
;; with defaults (swagger-ui/create-swagger-ui-handler
(create-swagger-ui-handler) {:path \"/swagger-ui\"
:url \"/api/swagger.json\"
;; with path and url set, swagger validator disabled, jsonEditor enabled :config {:validatorUrl nil}})
(swagger-ui/create-swagger-ui-handler ```"
{:path \"/\"
:url \"/api/swagger.json\"
:config {:validatorUrl nil
:jsonEditor true})"
([] ([]
(create-swagger-ui-handler nil)) (create-swagger-ui-handler nil))
([options] ([options]
(let [config-json (fn [{:keys [url config]}] (j/write-value-as-string (merge config {:url url}))) (let [config-json (fn [{:keys [url config]}] (j/write-value-as-string (merge config {:url url})))
conf-js (fn [opts] (str "window.API_CONF = " (config-json opts) ";"))
options (as-> options $ options (as-> options $
(update $ :root (fnil identity "swagger-ui")) (update $ :root (fnil identity "swagger-ui"))
(update $ :url (fnil identity "/swagger.json")) (update $ :url (fnil identity "/swagger.json"))
(assoc $ :paths {"/conf.js" {:headers {"Content-Type" "application/javascript"} (assoc $ :paths {"/config.json" {:headers {"Content-Type" "application/json"}
:status 200
:body (conf-js $)}
"/config.json" {:headers {"Content-Type" "application/json"}
:status 200 :status 200
:body (config-json $)}}))] :body (config-json $)}}))]
(ring/routes (ring/routes