A tiny library converting OpenAPI route definitions to Reitit routes.
Suitable for spec-first servers.
Experimental and work in progress. APIs subject to change.
Currently unsupported:
Any contributions are much much welcome and appreciated!
Leiningen/Boot
[org.clojars.lispyclouds/navi "0.0.1"]
Clojure CLI/deps.edn
{org.clojars.lispyclouds/navi {:mvn/version "0.0.1"}}
Gradle
compile 'org.clojars.lispyclouds:navi:0.0.1'
Maven
<dependency>
<groupId>org.clojars.lispyclouds</groupId>
<artifactId>navi</artifactId>
<version>0.0.1</version>
</dependency>
Given a api.yaml
:
openapi: "3.0.0"
info:
title: My calculator
version: "0.1.0"
description: My awesome calc!
paths:
"/add/{n1}/{n2}":
get:
operationId: AddGet
summary: Adds two numbers
parameters:
- name: n1
required: true
in: path
description: The first number
schema:
type: integer
- name: n2
required: true
in: path
description: The second number
schema:
type: integer
post:
operationId: AddPost
summary: Adds two numbers via POST
requestBody:
description: The numebers map
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/NumbersMap"
"/health":
get:
operationId: HealthCheck
summary: Returns Ok if all is well
components:
schemas:
NumbersMap:
type: object
required:
- n1
- n2
properties:
n1:
type: integer
description: The first number
n2:
type: integer
description: The second number
A clojure map of OperationId to handler fns:
(def handlers
{"AddGet" (fn [{{{:keys [n1 n2]} :path} :parameters}]
{:status 200
:body (+ n1 n2)})
"AddPost" (fn [{{{:keys [n1 n2]} :body} :parameters}]
{:status 200
:body (+ n1 n2)})
"HealthCheck" (fn [_]
{:status 200
:body "Ok"})})
Generate the routes:
(require '[navi.core :as navi])
(navi/routes-from (slurp "api.yaml") handlers)
=>
[["/add/{n1}/{n2}"
{:get
{:handler nil,
:parameters
{:path
[:map
[:n1 #function[clojure.core/int?]]
[:n2 #function[clojure.core/int?]]]}},
:post
{:handler nil,
:parameters
{:body
[:map
{:closed false}
[:n1 #function[clojure.core/int?]]
[:n2 #function[clojure.core/int?]]]}}}]
["/health" {:get {:handler nil}}]]
[["/add/{n1}/{n2}"
{:get
{:handler #function[navi.core/fn--8260],
:parameters
{:path
[:map
[:n1 #function[clojure.core/int?]]
[:n2 #function[clojure.core/int?]]]}},
:post
{:handler #function[navi.core/fn--8266],
:parameters
{:body
[:map
{:closed false}
[:n1 #function[clojure.core/int?]]
[:n2 #function[clojure.core/int?]]]}}}]
["/health" {:get {:handler #function[navi.core/fn--8271]}}]]
clojure -M:test
to run all testsCopyright © 2020-2021 Rahul De
Distributed under the EPL License, same as Clojure. See LICENSE.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close