From 2070a22be303fa86202d204de2b1568027dbc672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Hol=C3=BD?= Date: Tue, 28 Sep 2021 15:08:43 +0200 Subject: [PATCH] decision: docs on how to implement one --- doc/decisions.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/decisions.md b/doc/decisions.md index 20ad145..1ee5912 100644 --- a/doc/decisions.md +++ b/doc/decisions.md @@ -8,6 +8,22 @@ Liberator supports a whole lot of decisions points. Some of them are needed for next to every resource definition. Others are seldom used or there is no other sensible implementation. +## Implementing a decision + +A decision is a `(fn [{:keys [request resource] :as context}] ...)` that is expected to return a truthy value. In addition, it can also update the context - if the returned value is a map then it is merged into the context, if it is a function than the context is passed through it, if it is a vector then its second element is checked for being a map/function. Example: + +```clojure +(defresource foo + :allowed-methods [:get] + :allowed? (fn [{:keys [request resource]}] + (let [user (my/fetch-user! request)] + (when (my/authorized? user resource) + {::user user}))) + :handle-ok (fn [ctx] + (log/info "user" (-> ctx ::user :name) "accessed foo") + (foo/get (:request ctx)))) +``` + ## Typical decision points for resource implementors The following decisions points will be typically specified depending