Bartors
Bartors

Reputation: 190

Is there a way to extend a liberator resource with additional decisions without modifying the resource?

I want to extend an existing defresource with additional decision.

Let's say that I have (note that this is not the actual code but an example to showcase what I am trying to do):


(defresource get-something [{:keys [service]} ctx]
  resource-defaults
  :allowed-methods [:get]
  :authorized? (authorized? ctx)
  :exists? (fn [_]
             true)
  :handle-ok (fn [{:keys [::result]}]
               result))

then I want to "extend" get-something with a additional decision, e.g


malformed? (fn [_] false)

By "extend" I mean add the decision to the workflow of the handler without modifying get-something, thus obtaining a resource that uses all the decisions.

(resource
    (get-something service ctx)
    malformed? (fn [_] false))

Is it even possible?

Upvotes: 0

Views: 36

Answers (1)

ordnungswidrig
ordnungswidrig

Reputation: 3186

In the end resource definitions are map and data and can be manipulated as such:

(def get-something 
  (merge resource-defaults
         { :allowed-methods [:get]
           :authorized? #authorized?
           :exists? true
           :handle-ok ::result}))


(defresource get-something-handler get-resource)
(defresource get-something-extended-handler get-resource :malformed? false)

;; or

(defresource get-something-extended-handler 
   (merge get-resource {:malformed? false})

Upvotes: 0

Related Questions