@@ -12,12 +12,14 @@ module Affjax
1212 , RetryPolicy (..)
1313 , defaultRetryPolicy
1414 , retry
15+ , module Affjax.ResponseFormat
1516 ) where
1617
1718import Prelude
1819
1920import Affjax.RequestBody as RequestBody
2021import Affjax.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
22+ import Affjax.ResponseFormat (ResponseFormatError (..), printResponseFormatError )
2123import Affjax.ResponseFormat as ResponseFormat
2224import Affjax.ResponseHeader (ResponseHeader , responseHeader )
2325import Affjax.StatusCode (StatusCode (..))
@@ -26,7 +28,6 @@ import Control.Parallel (parOneOf)
2628import Data.Argonaut.Core (Json )
2729import Data.Argonaut.Core as J
2830import Data.Argonaut.Parser (jsonParser )
29- import Data.Array (intercalate )
3031import Data.Array as Arr
3132import Data.Either (Either (..), either )
3233import Data.Foldable (any )
@@ -36,6 +37,7 @@ import Data.Function.Uncurried (Fn2, runFn2)
3637import Data.HTTP.Method (Method (..), CustomMethod )
3738import Data.HTTP.Method as Method
3839import Data.Int (toNumber )
40+ import Data.List.NonEmpty as NEL
3941import Data.Maybe (Maybe (..))
4042import Data.Nullable (Nullable , toNullable )
4143import Data.Time.Duration (Milliseconds (..))
@@ -44,7 +46,7 @@ import Effect.Aff.Compat as AC
4446import Effect.Class (liftEffect )
4547import Effect.Exception (Error , error )
4648import Effect.Ref as Ref
47- import Foreign (F , Foreign , ForeignError (..), fail , renderForeignError , unsafeReadTagged , unsafeToForeign )
49+ import Foreign (F , Foreign , ForeignError (..), fail , unsafeReadTagged , unsafeToForeign )
4850import Math as Math
4951
5052-- | A record that contains all the information to perform an HTTP request.
@@ -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