;; project.clj or build.boot
[clj-unifier "RELEASE"]
;; deps.edn
{:deps {clj-unifier {:mvn/version "RELEASE"}}}
A Clojure(Script) library for unified responses.
Add the following dependency in your project:
;; project.clj or build.boot
[clj-unifier "RELEASE"]
;; deps.edn
{:deps {clj-unifier {:mvn/version "RELEASE"}}}
(ns example
(:require
[clojure.string :as str]
[unifier.response :as r]))
;; TODO: add example with meta (e.g. i18n)
;;;;
;; Database layer
;;;;
(defonce users [{:user/id 1, :user/email "john@doe.com"}
{:user/id 2, :user/email "jane@doe.com"}])
;;;;
;; Business logic layer
;;;;
(def prepare-email (comp str/lower-case str/trim))
(defn get-user-by-email [email]
(let [email (prepare-email email)]
(if-some [user (->> users
(filter #(= (:user/email %) email))
first)]
(r/as-success :ok user)
(r/as-error :not-found "user not found"))))
;;;;
;; HTTP layer
;;;;
;; HTTP helpers
(defn with-status [status body]
{:status status
:body body})
(defn with-type [type x]
(if (vector? type)
(update x :type #(->> % (conj type)))
(assoc x :type type)))
(defn as-http-response [x]
(r/as-response (with-type [:http] x)))
;; HTTP wrappers
(defmethod r/as-response [:http :ok] [x]
(with-status 200 (r/get-data x)))
(defmethod r/as-response [:http :not-found] [x]
(with-status 404 (r/get-data x)))
;; HTTP handlers
(defn handler [email]
(as-http-response (get-user-by-email email)))
;;;;
;; Usage examples
;;;;
(handler "john@doe.com")
;; => {:status 200, :body #:user{:id 1, :email "john@doe.com"}}
(handler "jane@doe.com")
;; => {:status 200, :body #:user{:id 2, :email "jane@doe.com"}}
(handler "andrew@doe.com")
;; => {:status 404, :body "user not found"}
# Run REPL & connect from your IDE
$ make dev
# Run tests
$ make test
# create a new git tag (available types `patch`, `minor`, `major`)
$ make patch
# push a new git tag
$ make release
$ make help
help Show help
clean Clean
dev Run REPL
lint Run linter
test Run tests
jar Build jar
install Install locally
deploy Deploy to repository
init Init first version
patch Increment patch version
minor Increment minor version
major Increment major version
release Release a new version
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close