Skip to content

Commit

Permalink
Merge pull request #81 from Flexiana/doc_strings
Browse files Browse the repository at this point in the history
add some docstrings
  • Loading branch information
g-krisztian authored Apr 29, 2021
2 parents 4d1e6c5 + 4267e2b commit ea4da99
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 40 deletions.
15 changes: 14 additions & 1 deletion src/framework/components/interceptors.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns framework.components.interceptors
(:require
[clojure.core :as core]
[clojure.walk :refer [keywordize-keys]]
[framework.acl.core :as acl]
[framework.components.interceptors.muuntaja :as m-int]
Expand Down Expand Up @@ -37,6 +36,8 @@
:role :guest})))

(defn require-logged-in
"Tries to extract userId from headers/authorization, and adds it as user/id into session-data.
If fails, it executes 'or-else' parameter function. By default it inserts a new guest user"
([]
(require-logged-in guest))
([or-else]
Expand All @@ -48,6 +49,9 @@
(or-else state)))}))

(def session-interceptor
":enter Extracts session-id from headers. Tries to get last session with the same id from session backend
:leave Stores actual session in session backend
TODO session Time To Leave"
{:enter (fn [{request :request :as state}]
(let [sessions-backend (-> state
:deps
Expand Down Expand Up @@ -76,6 +80,10 @@
#(keywordize-keys ((par/wrap-params identity) %)))))})

(defn db-access
"Runs HoneySQL query provided in (:query state)
Injects the result to (state [:response-data :db-data])
Optional parameter function: executed on :enter, if session interceptors creates new-session key in session data
for instance fetching user from db by it's id"
([]
{:leave (fn [{query :query
:as state}]
Expand All @@ -92,18 +100,22 @@
(xiana/ok state))))))

(def view
"Executes view function to create response"
{:leave (fn [{view :view :as state}]
(if view
(view state)
(xiana/ok state)))})

(defn muuntaja
"Muuntaja interceptor wrapped into xiana monad"
([]
(muuntaja m-int/muun-instance))
([instance]
(wrap/interceptor instance)))

(defn acl-restrict
":enter checks access control
:leave place for tightening db query via provided owner-fn"
([]
(acl-restrict {}))
([m]
Expand All @@ -118,6 +130,7 @@
state)))}))

(def side-effect
"Place for business logic based on db-data, before rendering view"
{:leave (fn [{f :side-effect :as state}]
(if f
(f state)
Expand Down
1 change: 1 addition & 0 deletions src/framework/components/interceptors/muuntaja.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns framework.components.interceptors.muuntaja
"Prepare default munntaja instance"
(:require
[clojure.data.xml :as xml]
[clojure.string]
Expand Down
3 changes: 3 additions & 0 deletions src/framework/components/interceptors/wrap.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
[xiana.core :as xiana]))

(defn interceptor
"Interceptor wrapper to use xiana monad"
[in]
(cond-> {}
(:enter in) (assoc :enter (fn [state] (xiana/ok ((:enter in) state))))
(:leave in) (assoc :leave (fn [state] (xiana/ok ((:leave in) state))))
(:error in) (assoc :error (fn [state] (xiana/error ((:error in) state))))))

(defn middleware->enter
"add middleware as :enter of interceptor"
([middleware]
(middleware->enter {} middleware))
([interceptor middleware]
(assoc interceptor :enter (fn [{req :request :as state}]
(xiana/ok (assoc state :request ((middleware identity) req)))))))

(defn middleware->leave
"add middleware as :leave of interceptor"
([middleware]
(middleware->leave {} middleware))
([interceptor middleware]
Expand Down
2 changes: 2 additions & 0 deletions src/framework/components/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
(xiana/ok state)))

(defn run
"Executes interceptors around a function. Be aware of raw recursion
TODO: refactor it to use reduce instead of recursion"
([state action]
(run state [] action))
([state interceptors action]
Expand Down
9 changes: 0 additions & 9 deletions src/framework/components/session/auth.clj

This file was deleted.

30 changes: 0 additions & 30 deletions src/framework/components/session/middleware.clj

This file was deleted.

0 comments on commit ea4da99

Please sign in to comment.