Fork me on GitHub
#yada
<
2018-02-02
>
sofra01:02:45

Hi, I am new to yada and having trouble understanding how to use the interceptor-chain from the docs but the docs in that area seem incomplete/incorrect. My service at this point has been just using yada/resource to create resources so I assume it is somehow getting the default interceptor chain when interpreted by Yada. To modify the chain the only way I can see to do it is to wrap the resources in yada/handler to create a chain and then modify it. Is this correct?

(yada.handler/append-interceptor 
  (yada/handler
    (yada/resource {:methods {:get {:consumes "text/plain" 
                                    :produces "text/plain"
                                    :response (fn [ctx] "hello")}}}))
                   yada.interceptors/return 
                   identity)

danielcompton02:02:34

@sofra not sure if this is the 'official' way to do it, but I tend to walk my route tree when I'm building the routes and assoc the default interceptors, then modify them there

danielcompton02:02:32

(defn update-resources [routes f & args]
  (walk/postwalk
    (fn [x]
      (if (instance? Resource x)
        (resource (apply f x args))
        x))
    routes))


(-> ["" [(index/index-routes postgres config)]]
               (update-resources
                 (fn add-default-interceptors [resource]
                   (cond-> resource
                           (not (contains? resource :interceptor-chain)) (assoc :interceptor-chain yada/default-interceptor-chain)
                           (not (contains? resource :error-interceptor-chain)) (assoc :error-interceptor-chain yada/default-error-interceptor-chain))))

              ...
               (update-resources append-error-interceptor i/logging
                 (error-reporters/make-report-error-interceptor reporter))
               (update-resources yada.handler/append-interceptor sec/security-headers
                 private-cache-control-headers)
               (update-resources
                 ;; Add custom 401 page to tell people to login.
                 (fn [resource]
                   (if (some? (yada.util/get* (:responses resource) 401))
                     resource
                     (assoc-in resource [:responses 401] custom-401)))))

danielcompton02:02:31

This is an area of Yada that I think could be made better

sofra02:02:24

@danielcompton interesting, thanks for that

dominicm08:02:36

I believe there are some keys, append/prepend-interceptor-chain which can do basic modifications