Skip to content

Commit 7ac8f64

Browse files
committed
Set responseType in request
1 parent e3dc584 commit 7ac8f64

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,10 @@ instance showResponseType :: Show ResponseType
507507
```
508508

509509

510-
#### `ajaxResponseTypeToString`
510+
#### `responseTypeToString`
511511

512512
``` purescript
513-
ajaxResponseTypeToString :: ResponseType -> String
513+
responseTypeToString :: ResponseType -> String
514514
```
515515

516516

src/Network/HTTP/Affjax.purs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ module Network.HTTP.Affjax
1515
import Control.Monad.Aff (Aff(), makeAff)
1616
import Control.Monad.Eff (Eff())
1717
import Control.Monad.Eff.Exception (Error(), error)
18-
import Data.Maybe (Maybe(..))
1918
import Data.Either (Either(..))
2019
import Data.Foreign (Foreign(..), F())
2120
import Data.Function (Fn4(), runFn4)
21+
import Data.Maybe (Maybe(..), maybe)
2222
import Data.Nullable (Nullable(), toNullable)
2323
import Network.HTTP.Affjax.Request
2424
import Network.HTTP.Affjax.Response
@@ -27,6 +27,7 @@ import Network.HTTP.Method (Method(..), methodToString)
2727
import Network.HTTP.RequestHeader (RequestHeader(), requestHeaderName, requestHeaderValue)
2828
import Network.HTTP.ResponseHeader (ResponseHeader(), responseHeader)
2929
import Network.HTTP.StatusCode (StatusCode())
30+
import Type.Proxy (Proxy(..))
3031

3132
-- | The effect type for AJAX requests made with Affjax.
3233
foreign import data Ajax :: !
@@ -102,6 +103,7 @@ affjax' req eb cb =
102103
, url: req.url
103104
, headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> req.headers
104105
, content: toNullable (toRequest <$> req.content)
106+
, responseType: responseTypeToString $ responseType (Proxy :: Proxy b)
105107
, username: toNullable req.username
106108
, password: toNullable req.password
107109
}
@@ -115,6 +117,7 @@ type AjaxRequest =
115117
, url :: URL
116118
, headers :: [{ field :: String, value :: String }]
117119
, content :: Nullable RequestContent
120+
, responseType :: String
118121
, username :: Nullable String
119122
, password :: Nullable String
120123
}
@@ -147,7 +150,7 @@ foreign import unsafeAjax
147150
response: xhr.response
148151
})();
149152
};
150-
if (options.responseType) xhr.responseType = options.responseType;
153+
xhr.responseType = options.responseType;
151154
xhr.send(options.content);
152155
};
153156
}

src/Network/HTTP/Affjax/Response.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ instance responsableDocument :: Responsable Document where
5858

5959
instance responsableJSON :: Responsable Foreign where
6060
responseType _ = JSONResponse
61-
fromResponse = readString >=> parseJSON
61+
fromResponse = Right
6262

6363
instance responsableString :: Responsable String where
6464
responseType _ = StringResponse

src/Network/HTTP/Affjax/ResponseType.purs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ instance showResponseType :: Show ResponseType where
2525
show JSONResponse = "JSONResponse"
2626
show StringResponse = "StringResponse"
2727

28-
ajaxResponseTypeToString :: ResponseType -> String
29-
ajaxResponseTypeToString ArrayBufferResponse = "arraybuffer"
30-
ajaxResponseTypeToString BlobResponse = "blob"
31-
ajaxResponseTypeToString DocumentResponse = "document"
32-
ajaxResponseTypeToString JSONResponse = "json"
33-
ajaxResponseTypeToString StringResponse = "text"
28+
responseTypeToString :: ResponseType -> String
29+
responseTypeToString ArrayBufferResponse = "arraybuffer"
30+
responseTypeToString BlobResponse = "blob"
31+
responseTypeToString DocumentResponse = "document"
32+
responseTypeToString JSONResponse = "json"
33+
responseTypeToString StringResponse = "text"

0 commit comments

Comments
 (0)