just-sultanov / Clj Unifier
Programming Languages
Labels
Projects that are alternatives of or similar to Clj Unifier
image:https://img.shields.io/github/license/just-sultanov/clj-unifier[license,link=LICENSE] image:https://codecov.io/gh/just-sultanov/clj-unifier/branch/master/graph/badge.svg[codecov,link=https://codecov.io/gh/just-sultanov/clj-unifier] image:https://github.com/just-sultanov/clj-unifier/workflows/build/badge.svg[build] image:https://github.com/just-sultanov/clj-unifier/workflows/deploy/badge.svg[deploy] image:https://img.shields.io/clojars/v/clj-unifier.svg[clojars,link=https://clojars.org/clj-unifier] image:https://cljdoc.org/badge/clj-unifier/clj-unifier[cljdoc,link=https://cljdoc.org/d/clj-unifier/clj-unifier/CURRENT]
Attention!
This library is DEPRECATED! Use the next library - link:https://github.com/just-sultanov/ninja.platform/blob/master/docs/ninja.tools/ninja.response.adoc[ninja.platform/response]
== clj-unifier
A Clojure(Script) library for unified responses.
=== Quick Start Guide
Add the following dependency in your project:
[source,clojure]
;; project.clj or build.boot [clj-unifier "0.0.15"]
;; deps.edn {:deps {clj-unifier {:mvn/version "0.0.15"}}}
=== Example
You can see a full example link:https://github.com/just-sultanov/clj-unifier/tree/master/examples/src/example[here].
[source,clojure]
(ns your.app (:require [unifier.response :as r] [unifier.response.http :as http]))
;;;; ;; Unified responses ;;;;
;; Unified success responses
(r/as-success "data") ;; data
with default response type
;; => #unifier.response.UnifiedSuccess{:type :unifier.response/success, :data "data", :meta nil}
(r/as-success "data" "meta") ;; data
and meta
with default response type
;; => #unifier.response.UnifiedSuccess{:type :unifier.response/success, :data "data", :meta "meta"}
(r/as-success ::pass "data" "meta") ;; data
and meta
with user-defined response type
;; => #unifier.response.UnifiedSuccess{:type :your.app/pass, :data "data", :meta "meta"}
;; Unified error responses
(r/as-error "data") ;; data
with default response type
;; => #unifier.response.UnifiedError{:type :unifier.response/error, :data "data", :meta nil}
(r/as-error "data" "meta") ;; data
and meta
with default response type
;; => #unifier.response.UnifiedError{:type :unifier.response/error, :data "data", :meta "meta"}
(r/as-error ::fail "data" "meta") ;; data
and meta
with user-defined response type
;; => #unifier.response.UnifiedError{:type :your.app/fail, :data "data", :meta "meta"}
;;;; ;; Public API ;;;;
;; Predicates
r/response?
r/error?
r/success?
;; Getters r/get-type r/get-data r/get-meta
;; Setters r/set-type r/set-data r/set-meta
;; Helpers r/unwrap r/-> r/->> r/?-> r/?->>
;; HTTP r/link! r/link r/as-http
;;;; ;; Examples ;;;;
(defn create [email] (if-some [user (db/create email)] (r/as-success :user/created user {:i18n/key :user/created :i18n/params email}) (r/as-error :user/not-created email {:i18n/key :user/exists :i18n/params email})))
(defmulti execute (fn [{:cmd/keys [version name]}] [version name]))
(defmethod execute :default [{:as cmd :cmd/keys [version name]}] (r/as-unsupported cmd {:i18n/key ::unsupported :i18n/params [version name]}))
;;;; ;; Unified http responses ;;;;
(r/as-http (r/as-success "data")) ;; => {:status 200, :headers {}, :body {:type :unifier.response/success, :data "data", :meta nil}}
(r/as-http (r/as-error "data")) ;; => {:status 500, :headers {}, :body {:type :unifier.response/error, :data "data", :meta nil}}
;;;;
;; Available predefined unified success
responses associated with http response types
;;;;
r/as-success -> :unifier.response/success -> :unifier.response.http/ok -> 200 r/as-created -> :unifier.response/created -> :unifier.response.http/created -> 201 r/as-deleted -> :unifier.response/deleted -> :unifier.response.http/no-content -> 204 r/as-accepted -> :unifier.response/accepted -> :unifier.response.http/accepted -> 202
;;;;
;; Available predefined unified error
responses associated with http response types
;;;;
r/as-error -> :unifier.response/error -> :unifier.response.http/internal-server-error -> 500 r/as-exception -> :unifier.response/exception -> :unifier.response.http/internal-server-error -> 500 r/as-unknown -> :unifier.response/unknown -> :unifier.response.http/bad-request -> 400 r/as-warning -> :unifier.response/warning -> :unifier.response.http/bad-request -> 400 r/as-unavailable -> :unifier.response/unavailable -> :unifier.response.http/service-unavailable -> 503 r/as-interrupted -> :unifier.response/interrupted -> :unifier.response.http/bad-request -> 400 r/as-incorrect -> :unifier.response/incorrect -> :unifier.response.http/bad-request -> 400 r/as-unauthorized -> :unifier.response/unauthorized -> :unifier.response.http/unauthorized -> 401 r/as-forbidden -> :unifier.response/forbidden -> :unifier.response.http/forbidden -> 403 r/as-unsupported -> :unifier.response/unsupported -> :unifier.response.http/method-not-allowed -> 405 r/as-not-found -> :unifier.response/not-found -> :unifier.response.http/not-found -> 404 r/as-conflict -> :unifier.response/conflict -> :unifier.response.http/conflict -> 409 r/as-busy -> :unifier.response/busy -> :unifier.response.http/service-unavailable -> 503
=== Development
[source,bash]
Run REPL & connect from your IDE
$ make repl
=== Testing
[source,bash]
Run all tests
$ make test
Run Clojure tests
$ make test-clj
Run ClojureScript tests
$ make test-cljs
=== Deploy
[source,bash]
create a new git tag
patch
, minor
, major
, minor-rc
, major-rc
, minor-release
, major-release
available types $ make patch
push a new git tag
$ make release
=== Available commands
[source,bash]
$ make help help Show help clean Clean repl Run REPL lint Run linter test-cljs Run ClojureScript tests test-clj Run Clojure tests 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 minor-rc Increment minor-rc version major-rc Increment major-rc version minor-release Increment minor-release version major-release Increment major-release version release Release a new version
=== License
link:LICENSE[Copyright © 2019-2020 Ilshat Sultanov]