A fast data-driven routing library for Clojure/Script
Find a file
Stig Brautaset f262daa454
Add separate validation options to Malli coercer
The motivation for this is adding a new API to a legacy service whose
data model has evolved over the years. Maybe we cannot guarantee the
presence of all fields that are considered mandatory now. Rather than
blowing up when attempting to return legacy entries we may want to
disable response validation.

However, we do want to ensure *new* entries added to the system are
valid, so we do not want to disable request validation.

Retain the ability to turn off both request and response validation with
a single setting for backwards compatibility.
2023-10-30 10:44:22 +00:00
.clj-kondo Setup clj-kondo files for each module 2022-01-24 12:39:42 +02:00
.github/workflows Fix interceptors deps 2023-06-12 18:15:53 +03:00
.lsp format-ns 2022-02-14 16:59:20 +02:00
dev-resources Handle URL-encoded paths in file and resource handlers 2021-04-30 14:38:18 +03:00
doc Merge pull request #655 from stig/fix-link-to-jira 2023-10-30 12:34:48 +02:00
examples Correct "effects" to "affects" in comments & docs 2023-10-13 21:10:20 +01:00
modules Add separate validation options to Malli coercer 2023-10-30 10:44:22 +00:00
perf-test/clj/reitit Faster path conflict resolution, O(n2) -> O(n) 2020-04-27 08:38:27 +03:00
scripts Try skipping module without src folder 2023-08-28 16:00:03 +03:00
test Add separate validation options to Malli coercer 2023-10-30 10:44:22 +00:00
.gitignore update deps 2022-02-12 22:30:42 +02:00
book.json initial rework 2018-09-02 19:23:18 +03:00
CHANGELOG.md 0.7.0-alpha7 2023-10-03 13:34:36 +03:00
CONTRIBUTING.md Fix typos 2019-05-22 19:17:10 +02:00
Justfile Add clj-kondo and fix most linting warnings 2020-03-05 13:32:48 +02:00
LICENSE Initial commit 2017-08-07 14:15:45 +03:00
lint.sh Add clj-kondo and fix most linting warnings 2020-03-05 13:32:48 +02:00
package-lock.json chore: openapi-schema-validator dev dependency 2023-03-06 16:10:48 +02:00
package.json chore: openapi-schema-validator dev dependency 2023-03-06 16:10:48 +02:00
project.clj 0.7.0-alpha7 2023-10-03 13:34:36 +03:00
README.md 0.7.0-alpha7 2023-10-03 13:34:36 +03:00

reitit Build Status cljdoc badge Slack

A fast data-driven router for Clojure(Script).

Presentations:

Status: stable

Full Documentation

There is #reitit in Clojurians Slack for discussion & help.

Main Modules

  • reitit - all bundled
  • reitit-core - the routing core
  • reitit-ring - a ring router
  • reitit-middleware - common middleware
  • reitit-spec clojure.spec coercion
  • reitit-malli malli coercion
  • reitit-schema Schema coercion
  • reitit-swagger Swagger2 apidocs
  • reitit-swagger-ui Integrated Swagger UI
  • reitit-frontend Tools for frontend routing
  • reitit-http http-routing with Interceptors
  • reitit-interceptors - common interceptors
  • reitit-sieppari support for Sieppari
  • reitit-dev - development utilities

Extra modules

Latest version

All main modules bundled:

[metosin/reitit "0.7.0-alpha7"]

Optionally, the parts can be required separately.

Quick start

(require '[reitit.core :as r])

(def router
  (r/router
    [["/api/ping" ::ping]
     ["/api/orders/:id" ::order]]))

(r/match-by-path router "/api/ping")
; #Match{:template "/api/ping"
;        :data {:name ::ping}
;        :result nil
;        :path-params {}
;        :path "/api/ping"}

(r/match-by-name router ::order {:id 2})
; #Match{:template "/api/orders/:id",
;        :data {:name ::order},
;        :result nil,
;        :path-params {:id 2},
;        :path "/api/orders/2"}

Ring example

A Ring routing app with input & output coercion using data-specs.

(require '[muuntaja.core :as m])
(require '[reitit.ring :as ring])
(require '[reitit.coercion.spec])
(require '[reitit.ring.coercion :as rrc])
(require '[reitit.ring.middleware.muuntaja :as muuntaja])
(require '[reitit.ring.middleware.parameters :as parameters])

(def app
  (ring/ring-handler
    (ring/router
      ["/api"
       ["/math" {:get {:parameters {:query {:x int?, :y int?}}
                       :responses  {200 {:body {:total int?}}}
                       :handler    (fn [{{{:keys [x y]} :query} :parameters}]
                                     {:status 200
                                      :body   {:total (+ x y)}})}}]]
      ;; router data affecting all routes
      {:data {:coercion   reitit.coercion.spec/coercion
              :muuntaja   m/instance
              :middleware [parameters/parameters-middleware
                           rrc/coerce-request-middleware
                           muuntaja/format-response-middleware
                           rrc/coerce-response-middleware]}})))

Valid request:

(app {:request-method :get
      :uri "/api/math"
      :query-params {:x "1", :y "2"}})
; {:status 200
;  :body {:total 3}}

Invalid request:

(app {:request-method :get
      :uri "/api/math"
      :query-params {:x "1", :y "a"}})
;{:status 400,
; :body {:type :reitit.coercion/request-coercion,
;        :coercion :spec,
;        :spec "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:$spec20745/x :$spec20745/y]), :type :map, :keys #{:y :x}, :keys/req #{:y :x}})",
;        :problems [{:path [:y],
;                    :pred "clojure.core/int?",
;                    :val "a",
;                    :via [:$spec20745/y],
;                    :in [:y]}],
;        :value {:x "1", :y "a"},
;        :in [:request :query-params]}}

More examples

All examples are in https://github.com/metosin/reitit/tree/master/examples

External resources

More info

Check out the full documentation!

Join #reitit channel in Clojurians slack.

Roadmap is mostly written in issues.

Special thanks

License

Copyright © 2017-2023 Metosin Oy

Distributed under the Eclipse Public License, the same as Clojure.