Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .setResponseType handling #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
/pom.xml.asc
/.repl
/.nrepl-port
*.iml
/.idea
/.cljs_rhino_repl
15 changes: 10 additions & 5 deletions src/cljs_http/core.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns cljs-http.core
(:import [goog.net EventType ErrorCode XhrIo]
[goog.net Jsonp])
[goog.net Jsonp]
[goog.net.XhrIo ResponseType])
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs-http.util :as util]
[cljs.core.async :as async]
Expand Down Expand Up @@ -31,15 +32,17 @@

(defn build-xhr
"Builds an XhrIo object from the request parameters."
[{:keys [with-credentials? default-headers] :as request}]
[{:keys [with-credentials? response-type default-headers] :as request
:or {response-type goog.net.XhrIo.ResponseType.DEFAULT}}]
(let [timeout (or (:timeout request) 0)
send-credentials (if (nil? with-credentials?)
true
with-credentials?)]
(doto (XhrIo.)
(apply-default-headers! default-headers)
(.setTimeoutInterval timeout)
(.setWithCredentials send-credentials))))
(.setWithCredentials send-credentials)
(.setResponseType response-type))))

;; Reverses the goog.net.ErrorCode constants to map to CLJS keywords
(def error-kw
Expand All @@ -55,7 +58,7 @@
(defn xhr
"Execute the HTTP request corresponding to the given Ring request
map and return a core.async channel."
[{:keys [request-method headers body with-credentials? cancel] :as request}]
[{:keys [request-method headers body response-type with-credentials? cancel] :as request}]
(let [channel (async/chan)
request-url (util/build-url request)
method (name (or request-method :get))
Expand All @@ -67,7 +70,9 @@
(let [target (.-target evt)
response {:status (.getStatus target)
:success (.isSuccess target)
:body (.getResponseText target)
:body (if response-type
(.getResponse target)
(.getResponseText target))
:headers (util/parse-headers (.getAllResponseHeaders target))
:trace-redirects [request-url (.getLastUri target)]
:error-code (error-kw (.getLastErrorCode target))
Expand Down