Skip to content

Commit 072e222

Browse files
committed
wip
1 parent 9f4db3b commit 072e222

2 files changed

Lines changed: 55 additions & 67 deletions

File tree

src/compojure/api/sweet.clj

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,22 @@
1-
(ns compojure.api.sweet
2-
(:require compojure.api.core
3-
compojure.api.api
4-
compojure.api.routes
5-
compojure.api.resource
6-
compojure.api.swagger
7-
ring.swagger.json-schema
8-
[potemkin :refer [import-vars]]))
9-
10-
(import-vars
11-
12-
[compojure.api.core
13-
14-
routes
15-
defroutes
16-
let-routes
17-
undocumented
18-
middleware
19-
20-
context
21-
22-
GET
23-
ANY
24-
HEAD
25-
PATCH
26-
DELETE
27-
OPTIONS
28-
POST
29-
PUT]
30-
31-
[compojure.api.api
32-
33-
api
34-
defapi]
35-
36-
[compojure.api.resource
37-
38-
resource]
39-
40-
[compojure.api.routes
41-
42-
path-for]
43-
44-
[compojure.api.swagger
45-
46-
swagger-routes]
47-
48-
[ring.swagger.json-schema
49-
50-
describe])
1+
;; NOTE: This namespace is generated by compojure.api.dev.gen
2+
(ns compojure.api.sweet (:require compojure.api.core compojure.api.api compojure.api.routes compojure.api.resource compojure.api.swagger ring.swagger.json-schema))
3+
(def ^{:arglists (quote ([& handlers])), :doc "Create a Ring handler by combining several handlers into one."} routes compojure.api.core/routes)
4+
(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))
5+
(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))
6+
(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)
7+
(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))
8+
(defmacro context [& args] (list* (quote compojure.api.core/context) args))
9+
(defmacro GET [& args] (list* (quote compojure.api.core/GET) args))
10+
(defmacro ANY [& args] (list* (quote compojure.api.core/ANY) args))
11+
(defmacro HEAD [& args] (list* (quote compojure.api.core/HEAD) args))
12+
(defmacro PATCH [& args] (list* (quote compojure.api.core/PATCH) args))
13+
(defmacro DELETE [& args] (list* (quote compojure.api.core/DELETE) args))
14+
(defmacro OPTIONS [& args] (list* (quote compojure.api.core/OPTIONS) args))
15+
(defmacro POST [& args] (list* (quote compojure.api.core/POST) args))
16+
(defmacro PUT [& args] (list* (quote compojure.api.core/PUT) args))
17+
(def ^{:arglists (quote ([& body])), :doc "Returns a ring handler wrapped in compojure.api.middleware/api-middlware.\n Creates the route-table at api creation time and injects that into the request via\n middlewares. Api and the mounted api-middleware can be configured by optional\n options map as the first parameter:\n\n (api\n {:formats [:json-kw :edn :transit-msgpack :transit-json]\n :exceptions {:handlers {:compojure.api.exception/default my-logging-handler}}\n :api {:invalid-routes-fn (constantly nil)}\n :swagger {:spec \"/swagger.json\"\n :ui \"/api-docs\"\n :data {:info {:version \"1.0.0\"\n :title \"My API\"\n :description \"the description\"}}}}\n (context \"/api\" []\n ...))\n\n ### direct api options:\n\n - **:api** All api options are under `:api`.\n - **:invalid-routes-fn** A 2-arity function taking handler and a sequence of\n invalid routes (not satisfying compojure.api.route.Routing)\n setting value to nil ignores invalid routes completely.\n defaults to `compojure.api.routes/log-invalid-child-routes`\n - **:disable-api-middleware?** boolean to disable the `api-middleware` from api.\n - **:swagger** Options to configure the Swagger-routes. Defaults to nil.\n See `compojure.api.swagger/swagger-routes` for details.\n\n ### api-middleware options\n\n Opinionated chain of middlewares for web apis. Takes optional options-map.\n\n ### Exception handlers\n\n An error handler is a function of exception, ex-data and request to response.\n\n When defining these options, it is suggested to use alias for the exceptions namespace,\n e.g. `[compojure.api.exception :as ex]`.\n\n Default:\n\n {::ex/request-validation ex/request-validation-handler\n ::ex/request-parsing ex/request-parsing-handler\n ::ex/response-validation ex/response-validation-handler\n ::ex/default ex/safe-handler}\n\n Note: Because the handlers are merged into default handlers map, to disable default handler you\n need to provide `nil` value as handler.\n\n Note: To catch Schema errors use `{:schema.core/error ex/schema-error-handler}`.\n\n ### Options\n\n - **:exceptions** for *compojure.api.middleware/wrap-exceptions* (nil to unmount it)\n - **:handlers** Map of error handlers for different exception types, type refers to `:type` key in ExceptionInfo data.\n\n - **:format** for ring-middleware-format middlewares (nil to unmount it)\n - **:formats** sequence of supported formats, e.g. `[:json-kw :edn]`\n - **:params-opts** for *ring.middleware.format-params/wrap-restful-params*,\n e.g. `{:transit-json {:handlers readers}}`\n - **:response-opts** for *ring.middleware.format-params/wrap-restful-response*,\n e.g. `{:transit-json {:handlers writers}}`\n\n - **:ring-swagger** options for ring-swagger's swagger-json method.\n e.g. `{:ignore-missing-mappings? true}`\n\n - **:coercion** A function from request->type->coercion-matcher, used\n in endpoint coercion for :body, :string and :response.\n Defaults to `(constantly compojure.api.middleware/default-coercion-matchers)`\n Setting value to nil disables all coercion\n\n - **:components** Components which should be accessible to handlers using\n :components restructuring. (If you are using api,\n you might want to take look at using wrap-components\n middleware manually.). Defaults to nil (middleware not mounted)."} api compojure.api.api/api)
18+
(defmacro defapi {:doc "Defines an api.\n\n API middleware options:\n\n Opinionated chain of middlewares for web apis. Takes optional options-map.\n\n ### Exception handlers\n\n An error handler is a function of exception, ex-data and request to response.\n\n When defining these options, it is suggested to use alias for the exceptions namespace,\n e.g. `[compojure.api.exception :as ex]`.\n\n Default:\n\n {::ex/request-validation ex/request-validation-handler\n ::ex/request-parsing ex/request-parsing-handler\n ::ex/response-validation ex/response-validation-handler\n ::ex/default ex/safe-handler}\n\n Note: Because the handlers are merged into default handlers map, to disable default handler you\n need to provide `nil` value as handler.\n\n Note: To catch Schema errors use `{:schema.core/error ex/schema-error-handler}`.\n\n ### Options\n\n - **:exceptions** for *compojure.api.middleware/wrap-exceptions* (nil to unmount it)\n - **:handlers** Map of error handlers for different exception types, type refers to `:type` key in ExceptionInfo data.\n\n - **:format** for ring-middleware-format middlewares (nil to unmount it)\n - **:formats** sequence of supported formats, e.g. `[:json-kw :edn]`\n - **:params-opts** for *ring.middleware.format-params/wrap-restful-params*,\n e.g. `{:transit-json {:handlers readers}}`\n - **:response-opts** for *ring.middleware.format-params/wrap-restful-response*,\n e.g. `{:transit-json {:handlers writers}}`\n\n - **:ring-swagger** options for ring-swagger's swagger-json method.\n e.g. `{:ignore-missing-mappings? true}`\n\n - **:coercion** A function from request->type->coercion-matcher, used\n in endpoint coercion for :body, :string and :response.\n Defaults to `(constantly compojure.api.middleware/default-coercion-matchers)`\n Setting value to nil disables all coercion\n\n - **:components** Components which should be accessible to handlers using\n :components restructuring. (If you are using api,\n you might want to take look at using wrap-components\n middleware manually.). Defaults to nil (middleware not mounted)."} [name & body] (list* (quote compojure.api.api/defapi) name body))
19+
(def ^{:arglists (quote ([info] [info options])), :doc "Creates a nested compojure-api Route from enchanced ring-swagger operations map and options.\n By default, applies both request- and response-coercion based on those definitions.\n\n Options:\n\n - **:coercion** A function from request->type->coercion-matcher, used\n in resource coercion for :body, :string and :response.\n Setting value to `(constantly nil)` disables both request- &\n response coercion. See tests and wiki for details.\n\n Enchancements to ring-swagger operations map:\n\n 1) :parameters use ring request keys (query-params, path-params, ...) instead of\n swagger-params (query, path, ...). This keeps things simple as ring keys are used in\n the handler when destructuring the request.\n\n 2) at resource root, one can add any ring-swagger operation definitions, which will be\n available for all operations, using the following rules:\n\n 2.1) :parameters are deep-merged into operation :parameters\n 2.2) :responses are merged into operation :responses (operation can fully override them)\n 2.3) all others (:produces, :consumes, :summary,...) are deep-merged by compojure-api\n\n 3) special key `:handler` either under operations or at top-level. Value should be a\n ring-handler function, responsible for the actual request processing. Handler lookup\n order is the following: operations-level, top-level.\n\n 4) request-coercion is applied once, using deep-merged parameters for a given\n operation or resource-level if only resource-level handler is defined.\n\n 5) response-coercion is applied once, using merged responses for a given\n operation or resource-level if only resource-level handler is defined.\n\n Note: Swagger operations are generated only from declared operations (:get, :post, ..),\n despite the top-level handler could process more operations.\n\n Example:\n\n (resource\n {:parameters {:query-params {:x Long}}\n :responses {500 {:schema {:reason s/Str}}}\n :get {:parameters {:query-params {:y Long}}\n :responses {200 {:schema {:total Long}}}\n :handler (fn [request]\n (ok {:total (+ (-> request :query-params :x)\n (-> request :query-params :y))}))}\n :post {}\n :handler (constantly\n (internal-server-error {:reason \"not implemented\"}))})"} resource compojure.api.resource/resource)
20+
(defmacro path-for {:doc "Extracts the lookup-table from request and finds a route by name."} [route-name & arg2] (list* (quote compojure.api.routes/path-for) route-name arg2))
21+
(def ^{:arglists (quote ([] [options])), :doc "Returns routes for swagger-articats (ui & spec). Accepts an options map, with the\n following options:\n **:ui** Path for the swagger-ui (defaults to \"/\").\n Setting the value to nil will cause the swagger-ui not to be mounted\n **:spec** Path for the swagger-spec (defaults to \"/swagger.json\")\n Setting the value to nil will cause the swagger-ui not to be mounted\n **:data** Swagger data in the Ring-Swagger format.\n **:options**\n **:ui** Options to configure the ui\n **:spec** Options to configure the spec. Nada at the moment.\n Example options:\n {:ui \"/api-docs\"\n :spec \"/swagger.json\"\n :options {:ui {:jsonEditor true}\n :spec {}}\n :data {:basePath \"/app\"\n :info {:version \"1.0.0\"\n :title \"Sausages\"\n :description \"Sausage description\"\n :termsOfService \"http://helloreverb.com/terms/\"\n :contact {:name \"My API Team\"\n :email \"[email protected]\"\n :url \"http://www.metosin.fi\"}\n :license {:name: \"Eclipse Public License\"\n :url: \"http://www.eclipse.org/legal/epl-v10.html\"}}\n :tags [{:name \"sausages\", :description \"Sausage api-set\"}]}}"} swagger-routes compojure.api.swagger/swagger-routes)
22+
(def ^{:arglists (quote ([schema desc & kvs])), :doc "Attach description and possibly other meta-data to a schema."} describe ring.swagger.json-schema/describe)

test/compojure/api/dev/gen.clj

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
(import-fn sym))))
100100
syms)))
101101

102-
(def impl-info
102+
(def compojure-api-sweet-impl-info
103103
{:vars '([compojure.api.core routes defroutes let-routes undocumented middleware
104104
context GET ANY HEAD PATCH DELETE OPTIONS POST PUT]
105105
[compojure.api.api api defapi]
@@ -109,17 +109,24 @@
109109
[ring.swagger.json-schema describe])})
110110

111111
(defn gen-compojure-api-sweet-ns-forms [nsym]
112-
(let [require-macros (into #{} (map first) (:macros impl-info))]
113-
(concat
114-
[";; NOTE: This namespace is generated by compojure.api.dev.gen"
115-
`(~'ns ~nsym
116-
(:require compojure.api.core
117-
compojure.api.api
118-
compojure.api.routes
119-
compojure.api.resource
120-
compojure.api.swagger
121-
ring.swagger.json-schema))]
122-
(apply import-vars (:vars impl-info)))))
112+
(concat
113+
[";; NOTE: This namespace is generated by compojure.api.dev.gen"
114+
`(~'ns ~nsym
115+
(:require compojure.api.core
116+
compojure.api.api
117+
compojure.api.routes
118+
compojure.api.resource
119+
compojure.api.swagger
120+
ring.swagger.json-schema))]
121+
(apply import-vars (:vars compojure-api-sweet-impl-info))))
122+
123+
(defn gen-compojure-api-upload-ns-forms [nsym]
124+
(concat
125+
[";; NOTE: This namespace is generated by compojure.api.dev.gen"
126+
`(~'ns ~nsym
127+
(:require ring.middleware.multipart-params
128+
ring.swagger.upload))]
129+
(apply import-vars (:vars impl-info))))
123130

124131
(defn print-form [form]
125132
(with-bindings
@@ -147,21 +154,30 @@
147154
form)))))
148155
nil)
149156

150-
(defn print-compojure-api-ns [nsym]
151-
(run! print-form (gen-compojure-api-sweet-ns-forms nsym)))
157+
(defn print-compojure-api-ns [{:keys [f nsym]}]
158+
(run! print-form (f nsym)))
152159

153160
(def compojure-api-sweet-nsym
154161
(with-meta
155162
'compojure.api.sweet
156163
;;TODO ns meta
157164
nil))
158165

166+
(def compojure-api-upload-nsym
167+
(with-meta
168+
'compojure.api.upload
169+
;;TODO ns meta
170+
nil))
171+
159172
(def gen-source->nsym
160-
{"src/compojure/api/sweet.clj" compojure-api-sweet-nsym})
173+
{"src/compojure/api/sweet.clj" {:nsym compojure-api-sweet-nsym
174+
:f #'gen-compojure-api-sweet-ns-forms}
175+
"src/compojure/api/upload.clj" {:nsym compojure-api-upload-nsym
176+
:f #'gen-compojure-api-upload-ns-forms}})
161177

162178
(defn spit-compojure-api-ns []
163-
(doseq [[source nsym] gen-source->nsym]
164-
(spit source (with-out-str (print-compojure-api-ns nsym)))))
179+
(doseq [[source conf] gen-source->nsym]
180+
(spit source (with-out-str (print-compojure-api-ns conf)))))
165181

166182
(comment
167183
(print-compojure-api-ns compojure-api-sweet-nsym)

0 commit comments

Comments
 (0)