This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-04-21
Channels
- # babashka (16)
- # beginners (182)
- # calva (13)
- # chlorine-clover (43)
- # clj-kondo (17)
- # cljs-dev (14)
- # cljsrn (19)
- # clojure (97)
- # clojure-argentina (7)
- # clojure-dev (45)
- # clojure-europe (7)
- # clojure-germany (5)
- # clojure-nl (4)
- # clojure-portugal (4)
- # clojure-romania (5)
- # clojure-spec (46)
- # clojure-uk (21)
- # clojuredesign-podcast (2)
- # clojurescript (159)
- # conjure (28)
- # core-async (7)
- # cursive (13)
- # datomic (17)
- # defnpodcast (9)
- # duct (1)
- # fulcro (45)
- # graphql (6)
- # jobs (7)
- # jobs-discuss (1)
- # juxt (3)
- # kaocha (4)
- # leiningen (12)
- # malli (5)
- # observability (1)
- # off-topic (50)
- # pathom (15)
- # re-frame (16)
- # reitit (5)
- # remote-jobs (21)
- # ring (7)
- # shadow-cljs (166)
- # test-check (6)
- # tools-deps (27)
- # xtdb (2)
Here's some simple example that should work:
(->interceptor
:id :add-cofx
:before (fn [ctx]
(assoc-coeffect ctx :my-cofx "hello")))
OK, something isn't working - probably I don't understand something. I was doing:
(rf/reg-event-fx :authenticate
[(rf/inject-cofx :cookie/get [:token])]
(fn [{cookie :cookie/get} _]
{:http-xhrio {:method :get
:uri (api/api "logged-in")
:headers "Authorization" (str "Token " (:token cookie))}
:response-format (ajax/transit-response-format)
:on-success [:save-user-name]
:on-failure [:clear-auth]}})
What I am trying to do is to create an interceptor:
(def with-auth
(rf/->interceptor
:id :with-auth
:before (fn [ctx]
(rf/assoc-coeffect ctx :cookie/get [:token]))
:after (fn [ctx]
(js/console.log ctx)
(-> (assoc-in ctx
[:effects :http-xhrio :headers "Authorization"]
(str "Token" (get-in ctx [:cookie/get :token])))))))
I don't understand what you want to end up with.
inject-cofx
creates an interceptor that injects a cofx derived from a function previously registered with reg-cofx
.
rf/->interceptor
creates a different interceptor that can do anything you want. It's not related to reg-cofx
in any way.
Just check their source code. :) The whole re-frame codebase is not that complicated. And where it is, there are abundant comments.
OK, so
(def with-auth
(rf/->interceptor
:id :with-auth
:before (fn [ctx]
(update ctx :coeffects (registrar/get-handler :cofx :cookie/get) [:token]))
:after (fn [ctx]
(-> (assoc-in ctx
[:effects :http-xhrio :headers "Authorization"]
(str "Token " (get-in ctx [:coeffects :cookie/get :token])))))))
I am replicating parts of inject-cofx
, using something outside of re-frame.core
- I don't want to be doing inventing wheel again 🙂