1- module Network.HTTP. Affjax
1+ module Affjax
22 ( RequestOptions , defaultRequest
33 , Response
44 , URL
@@ -12,16 +12,22 @@ module Network.HTTP.Affjax
1212 , RetryPolicy (..)
1313 , defaultRetryPolicy
1414 , retry
15+ , module Affjax.ResponseFormat
1516 ) where
1617
1718import Prelude
1819
20+ import Affjax.RequestBody as RequestBody
21+ import Affjax.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
22+ import Affjax.ResponseFormat (ResponseFormatError (..), printResponseFormatError )
23+ import Affjax.ResponseFormat as ResponseFormat
24+ import Affjax.ResponseHeader (ResponseHeader , responseHeader )
25+ import Affjax.StatusCode (StatusCode (..))
1926import Control.Monad.Except (runExcept , throwError )
2027import Control.Parallel (parOneOf )
2128import Data.Argonaut.Core (Json )
2229import Data.Argonaut.Core as J
2330import Data.Argonaut.Parser (jsonParser )
24- import Data.Array (intercalate )
2531import Data.Array as Arr
2632import Data.Either (Either (..), either )
2733import Data.Foldable (any )
@@ -31,6 +37,7 @@ import Data.Function.Uncurried (Fn2, runFn2)
3137import Data.HTTP.Method (Method (..), CustomMethod )
3238import Data.HTTP.Method as Method
3339import Data.Int (toNumber )
40+ import Data.List.NonEmpty as NEL
3441import Data.Maybe (Maybe (..))
3542import Data.Nullable (Nullable , toNullable )
3643import Data.Time.Duration (Milliseconds (..))
@@ -39,13 +46,8 @@ import Effect.Aff.Compat as AC
3946import Effect.Class (liftEffect )
4047import Effect.Exception (Error , error )
4148import Effect.Ref as Ref
42- import Foreign (F , Foreign , ForeignError (..), fail , renderForeignError , unsafeReadTagged , unsafeToForeign )
49+ import Foreign (F , Foreign , ForeignError (..), fail , unsafeReadTagged , unsafeToForeign )
4350import Math as Math
44- import Network.HTTP.Affjax.RequestBody as RequestBody
45- import Network.HTTP.Affjax.ResponseFormat as ResponseFormat
46- import Network.HTTP.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
47- import Network.HTTP.ResponseHeader (ResponseHeader , responseHeader )
48- import Network.HTTP.StatusCode (StatusCode (..))
4951
5052-- | A record that contains all the information to perform an HTTP request.
5153-- | Instead of constructing the record from scratch it is often easier to build
@@ -87,77 +89,77 @@ type Response a =
8789 { status :: StatusCode
8890 , statusText :: String
8991 , headers :: Array ResponseHeader
90- , response :: a
92+ , body :: a
9193 }
9294
9395-- | Type alias for URL strings to aid readability of types.
9496type URL = String
9597
9698-- | Makes a `GET` request to the specified URL.
97- get :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff (Response a )
99+ get :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff (Response ( Either ResponseFormatError a ) )
98100get rt u = request rt $ defaultRequest { url = u }
99101
100102-- | Makes a `POST` request to the specified URL, sending data.
101- post :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response a )
103+ post :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response ( Either ResponseFormatError a ) )
102104post rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = Just c }
103105
104106-- | Makes a `POST` request to the specified URL with the option to send data.
105- post' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response a )
107+ post' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response ( Either ResponseFormatError a ) )
106108post' rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = c }
107109
108110-- | Makes a `POST` request to the specified URL, sending data and ignoring the
109111-- | response.
110112post_ :: URL -> RequestBody.RequestBody -> Aff (Response Unit )
111- post_ = post ResponseFormat .ignore
113+ post_ url = map (_ { body = unit }) <<< post ResponseFormat .ignore url
112114
113115-- | Makes a `POST` request to the specified URL with the option to send data,
114116-- | and ignores the response.
115117post_' :: URL -> Maybe RequestBody.RequestBody -> Aff (Response Unit )
116- post_' = post' ResponseFormat .ignore
118+ post_' url = map (_ { body = unit }) <<< post' ResponseFormat .ignore url
117119
118120-- | Makes a `PUT` request to the specified URL, sending data.
119- put :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response a )
121+ put :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response ( Either ResponseFormatError a ) )
120122put rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
121123
122124-- | Makes a `PUT` request to the specified URL with the option to send data.
123- put' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response a )
125+ put' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response ( Either ResponseFormatError a ) )
124126put' rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = c }
125127
126128-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
127129-- | response.
128130put_ :: URL -> RequestBody.RequestBody -> Aff (Response Unit )
129- put_ = put ResponseFormat .ignore
131+ put_ url = map (_ { body = unit }) <<< put ResponseFormat .ignore url
130132
131133-- | Makes a `PUT` request to the specified URL with the option to send data,
132134-- | and ignores the response.
133135put_' :: URL -> Maybe RequestBody.RequestBody -> Aff (Response Unit )
134- put_' = put' ResponseFormat .ignore
136+ put_' url = map (_ { body = unit }) <<< put' ResponseFormat .ignore url
135137
136138-- | Makes a `DELETE` request to the specified URL.
137- delete :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff (Response a )
139+ delete :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff (Response ( Either ResponseFormatError a ) )
138140delete rt u = request rt $ defaultRequest { method = Left DELETE , url = u }
139141
140142-- | Makes a `DELETE` request to the specified URL and ignores the response.
141143delete_ :: URL -> Aff (Response Unit )
142- delete_ = delete ResponseFormat .ignore
144+ delete_ = map (_ { body = unit }) <<< delete ResponseFormat .ignore
143145
144146-- | Makes a `PATCH` request to the specified URL, sending data.
145- patch :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response a )
147+ patch :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response ( Either ResponseFormatError a ) )
146148patch rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
147149
148150-- | Makes a `PATCH` request to the specified URL with the option to send data.
149- patch' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response a )
151+ patch' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response ( Either ResponseFormatError a ) )
150152patch' rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = c }
151153
152154-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
153155-- | response.
154156patch_ :: URL -> RequestBody.RequestBody -> Aff (Response Unit )
155- patch_ = patch ResponseFormat .ignore
157+ patch_ url = map (_ { body = unit }) <<< patch ResponseFormat .ignore url
156158
157159-- | Makes a `PATCH` request to the specified URL with the option to send data,
158160-- | and ignores the response.
159161patch_' :: URL -> Maybe RequestBody.RequestBody -> Aff (Response Unit )
160- patch_' = patch' ResponseFormat .ignore
162+ patch_' url = map (_ { body = unit }) <<< patch' ResponseFormat .ignore url
161163
162164-- | A sequence of retry delays, in milliseconds.
163165type RetryDelayCurve = Int -> Milliseconds
@@ -236,12 +238,14 @@ retry policy run req = do
236238-- | ```purescript
237239-- | get json "/resource"
238240-- | ```
239- request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff (Response a )
241+ request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff (Response ( Either ResponseFormatError a ) )
240242request rt req = do
241243 res <- AC .fromEffectFnAff $ runFn2 _ajax responseHeader req'
242- case runExcept (fromResponse' res.response) of
243- Left err -> throwError $ error $ intercalate " \n " (map renderForeignError err)
244- Right res' -> pure (res { response = res' })
244+ case runExcept (fromResponse' res.body) of
245+ Left err -> do
246+ pure (res { body = Left (ResponseFormatError (NEL .head err) res.body) })
247+ Right res' -> do
248+ pure (res { body = Right res' })
245249 where
246250
247251 req' :: AjaxRequest a
0 commit comments