0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

Improved repo abstraction for proper encode and send query-string.

This commit is contained in:
Andrey Antukh 2016-03-27 18:27:54 +03:00
parent 3c6b553f2f
commit 3f1a14c94e

View file

@ -7,13 +7,15 @@
(ns uxbox.repo.core (ns uxbox.repo.core
"A main interface for access to remote resources." "A main interface for access to remote resources."
(:refer-clojure :exclude [do]) (:refer-clojure :exclude [do])
(:require [httpurr.client.xhr :as http] (:require [clojure.walk :as walk]
[httpurr.client.xhr :as http]
[httpurr.status :as http.status] [httpurr.status :as http.status]
[hodgepodge.core :refer (local-storage)] [hodgepodge.core :refer (local-storage)]
[promesa.core :as p :include-macros true] [promesa.core :as p :include-macros true]
[beicon.core :as rx] [beicon.core :as rx]
[uxbox.transit :as t] [uxbox.transit :as t]
[uxbox.state :as ust])) [uxbox.state :as ust])
(:import [goog.Uri QueryData]))
(goog-define url "http://127.0.0.1:5050/api") (goog-define url "http://127.0.0.1:5050/api")
@ -49,19 +51,28 @@
(def ^:private ^:const +headers+ (def ^:private ^:const +headers+
{"content-type" "application/transit+json"}) {"content-type" "application/transit+json"})
(defn auth-headers (defn- auth-headers
[] []
(when-let [auth (:auth @ust/state)] (when-let [auth (:auth @ust/state)]
{"authorization" (str "Token " (:token auth "no-token"))})) {"authorization" (str "Token " (:token auth "no-token"))}))
(defn- encode-query
[params]
(let [data (QueryData.)]
(.extend data (clj->js params))
(.toString data)))
(defn- send! (defn- send!
[{:keys [body headers auth method] :or {auth true} :as request}] [{:keys [body headers auth method query url] :or {auth true} :as request}]
(let [headers (merge {} (let [headers (merge {}
(when body +headers+) (when body +headers+)
headers headers
(when auth (auth-headers))) (when auth (auth-headers)))
request (merge (assoc request :headers headers) request {:method method
(when body {:body (t/encode body)}))] :url url
:headers headers
:query-string (when query (encode-query query))
:body (when body (t/encode body))}]
(->> (http/send! request) (->> (http/send! request)
(rx/from-promise) (rx/from-promise)
(rx/map conditional-decode) (rx/map conditional-decode)