Liking cljdoc? Tell your friends :D
Clojure only.

compojure.api.resource


resourceclj

(resource info)
(resource info options)

Creates a nested compojure-api Route from enchanced ring-swagger operations map and options. By default, applies both request- and response-coercion based on those definitions.

Options:

  • :coercion A function from request->type->coercion-matcher, used in resource coercion for :body, :string and :response. Setting value to (constantly nil) disables both request- & response coercion. See tests and wiki for details.

Enchancements to ring-swagger operations map:

  1. :parameters use ring request keys (query-params, path-params, ...) instead of swagger-params (query, path, ...). This keeps things simple as ring keys are used in the handler when destructuring the request.

  2. at resource root, one can add any ring-swagger operation definitions, which will be available for all operations, using the following rules:

2.1) :parameters are deep-merged into operation :parameters 2.2) :responses are merged into operation :responses (operation can fully override them) 2.3) all others (:produces, :consumes, :summary,...) are deep-merged by compojure-api

  1. special key :handler either under operations or at top-level. Value should be a ring-handler function, responsible for the actual request processing. Handler lookup order is the following: operations-level, top-level.

  2. request-coercion is applied once, using deep-merged parameters for a given operation or resource-level if only resource-level handler is defined.

  3. response-coercion is applied once, using merged responses for a given operation or resource-level if only resource-level handler is defined.

Note: Swagger operations are generated only from declared operations (:get, :post, ..), despite the top-level handler could process more operations.

Example:

(resource {:parameters {:query-params {:x Long}} :responses {500 {:schema {:reason s/Str}}} :get {:parameters {:query-params {:y Long}} :responses {200 {:schema {:total Long}}} :handler (fn [request] (ok {:total (+ (-> request :query-params :x) (-> request :query-params :y))}))} :post {} :handler (constantly (internal-server-error {:reason "not implemented"}))})

Creates a nested compojure-api Route from enchanced ring-swagger operations map and options.
By default, applies both request- and response-coercion based on those definitions.

Options:

- **:coercion**       A function from request->type->coercion-matcher, used
                      in resource coercion for :body, :string and :response.
                      Setting value to `(constantly nil)` disables both request- &
                      response coercion. See tests and wiki for details.

Enchancements to ring-swagger operations map:

1) :parameters use ring request keys (query-params, path-params, ...) instead of
swagger-params (query, path, ...). This keeps things simple as ring keys are used in
the handler when destructuring the request.

2) at resource root, one can add any ring-swagger operation definitions, which will be
available for all operations, using the following rules:

  2.1) :parameters are deep-merged into operation :parameters
  2.2) :responses are merged into operation :responses (operation can fully override them)
  2.3) all others (:produces, :consumes, :summary,...) are deep-merged by compojure-api

3) special key `:handler` either under operations or at top-level. Value should be a
ring-handler function, responsible for the actual request processing. Handler lookup
order is the following: operations-level, top-level.

4) request-coercion is applied once, using deep-merged parameters for a given
operation or resource-level if only resource-level handler is defined.

5) response-coercion is applied once, using merged responses for a given
operation or resource-level if only resource-level handler is defined.

Note: Swagger operations are generated only from declared operations (:get, :post, ..),
despite the top-level handler could process more operations.

Example:

(resource
  {:parameters {:query-params {:x Long}}
   :responses {500 {:schema {:reason s/Str}}}
   :get {:parameters {:query-params {:y Long}}
         :responses {200 {:schema {:total Long}}}
         :handler (fn [request]
                    (ok {:total (+ (-> request :query-params :x)
                                   (-> request :query-params :y))}))}
   :post {}
   :handler (constantly
              (internal-server-error {:reason "not implemented"}))})
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close