Compojure-api Api Documentation

Version: 2.0.0-alpha28

compojure.api.api

api

(api & body)

Returns a ring handler wrapped in compojure.api.middleware/api-middlware. Creates the route-table at api creation time and injects that into the request via middlewares. Api and the mounted api-middleware can be configured by optional options map as the first parameter:

(api
  {:exceptions {:handlers {:compojure.api.exception/default my-logging-handler}}
   :api {:invalid-routes-fn (constantly nil)}
   :swagger {:spec "/swagger.json"
             :ui "/api-docs"
             :data {:info {:version "1.0.0"
                           :title "My API"
                           :description "the description"}}}}
  (context "/api" []
    ...))

direct api options:

  • :api All api options are under :api.
  • :invalid-routes-fn A 2-arity function taking handler and a sequence of invalid routes (not satisfying compojure.api.route.Routing) setting value to nil ignores invalid routes completely. defaults to compojure.api.routes/log-invalid-child-routes
  • :disable-api-middleware? boolean to disable the api-middleware from api.
  • :swagger Options to configure the Swagger-routes. Defaults to nil. See compojure.api.swagger/swagger-routes for details.

api-middleware options

See compojure.api.middleware/api-middleware for more available options.

Opinionated chain of middlewares for web apis. Takes optional options-map.

Exception handlers

An error handler is a function of exception, ex-data and request to response.

When defining these options, it is suggested to use alias for the exceptions namespace, e.g. [compojure.api.exception :as ex].

Default:

{::ex/request-validation  ex/request-validation-handler
 ::ex/request-parsing     ex/request-parsing-handler
 ::ex/response-validation ex/response-validation-handler
 ::ex/default             ex/safe-handler}

Note: Because the handlers are merged into default handlers map, to disable default handler you need to provide nil value as handler.

Note: To catch Schema errors use {:schema.core/error ex/schema-error-handler}.

Options

  • :exceptions for compojure.api.middleware/wrap-exceptions (nil to unmount it)

    • :handlers Map of error handlers for different exception types, type refers to :type key in ExceptionInfo data.
  • :formats for Muuntaja middleware. Value can be a valid muuntaja options-map, a Muuntaja instance or nil (to unmount it). See https://github.com/metosin/muuntaja/wiki/Configuration for details.

  • :middleware vector of extra middleware to be applied last (just before the handler).

  • :ring-swagger options for ring-swagger’s swagger-json method. e.g. {:ignore-missing-mappings? true}

  • :coercion A function from request->type->coercion-matcher, used in endpoint coercion for types :body, :string and :response. Defaults to compojure.api.middleware/default-coercion Setting value to nil disables all coercion.

  • :components Components which should be accessible to handlers using :components restructuring. (If you are using api, you might want to take look at using wrap-components middleware manually.). Defaults to nil (middleware not mounted).