1
- module Network.HTTP. Affjax
1
+ module Affjax
2
2
( RequestOptions , defaultRequest
3
3
, Response
4
4
, URL
@@ -12,16 +12,22 @@ module Network.HTTP.Affjax
12
12
, RetryPolicy (..)
13
13
, defaultRetryPolicy
14
14
, retry
15
+ , module Affjax.ResponseFormat
15
16
) where
16
17
17
18
import Prelude
18
19
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 (..))
19
26
import Control.Monad.Except (runExcept , throwError )
20
27
import Control.Parallel (parOneOf )
21
28
import Data.Argonaut.Core (Json )
22
29
import Data.Argonaut.Core as J
23
30
import Data.Argonaut.Parser (jsonParser )
24
- import Data.Array (intercalate )
25
31
import Data.Array as Arr
26
32
import Data.Either (Either (..), either )
27
33
import Data.Foldable (any )
@@ -31,6 +37,7 @@ import Data.Function.Uncurried (Fn2, runFn2)
31
37
import Data.HTTP.Method (Method (..), CustomMethod )
32
38
import Data.HTTP.Method as Method
33
39
import Data.Int (toNumber )
40
+ import Data.List.NonEmpty as NEL
34
41
import Data.Maybe (Maybe (..))
35
42
import Data.Nullable (Nullable , toNullable )
36
43
import Data.Time.Duration (Milliseconds (..))
@@ -39,13 +46,8 @@ import Effect.Aff.Compat as AC
39
46
import Effect.Class (liftEffect )
40
47
import Effect.Exception (Error , error )
41
48
import Effect.Ref as Ref
42
- import Foreign (F , Foreign , ForeignError (..), fail , renderForeignError , unsafeReadTagged , unsafeToForeign )
49
+ import Foreign (F , Foreign , ForeignError (..), fail , unsafeReadTagged , unsafeToForeign )
43
50
import 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 (..))
49
51
50
52
-- | A record that contains all the information to perform an HTTP request.
51
53
-- | Instead of constructing the record from scratch it is often easier to build
@@ -87,77 +89,77 @@ type Response a =
87
89
{ status :: StatusCode
88
90
, statusText :: String
89
91
, headers :: Array ResponseHeader
90
- , response :: a
92
+ , body :: a
91
93
}
92
94
93
95
-- | Type alias for URL strings to aid readability of types.
94
96
type URL = String
95
97
96
98
-- | 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 ) )
98
100
get rt u = request rt $ defaultRequest { url = u }
99
101
100
102
-- | 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 ) )
102
104
post rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = Just c }
103
105
104
106
-- | 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 ) )
106
108
post' rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = c }
107
109
108
110
-- | Makes a `POST` request to the specified URL, sending data and ignoring the
109
111
-- | response.
110
112
post_ :: URL -> RequestBody.RequestBody -> Aff (Response Unit )
111
- post_ = post ResponseFormat .ignore
113
+ post_ url = map (_ { body = unit }) <<< post ResponseFormat .ignore url
112
114
113
115
-- | Makes a `POST` request to the specified URL with the option to send data,
114
116
-- | and ignores the response.
115
117
post_' :: URL -> Maybe RequestBody.RequestBody -> Aff (Response Unit )
116
- post_' = post' ResponseFormat .ignore
118
+ post_' url = map (_ { body = unit }) <<< post' ResponseFormat .ignore url
117
119
118
120
-- | 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 ) )
120
122
put rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
121
123
122
124
-- | 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 ) )
124
126
put' rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = c }
125
127
126
128
-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
127
129
-- | response.
128
130
put_ :: URL -> RequestBody.RequestBody -> Aff (Response Unit )
129
- put_ = put ResponseFormat .ignore
131
+ put_ url = map (_ { body = unit }) <<< put ResponseFormat .ignore url
130
132
131
133
-- | Makes a `PUT` request to the specified URL with the option to send data,
132
134
-- | and ignores the response.
133
135
put_' :: URL -> Maybe RequestBody.RequestBody -> Aff (Response Unit )
134
- put_' = put' ResponseFormat .ignore
136
+ put_' url = map (_ { body = unit }) <<< put' ResponseFormat .ignore url
135
137
136
138
-- | 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 ) )
138
140
delete rt u = request rt $ defaultRequest { method = Left DELETE , url = u }
139
141
140
142
-- | Makes a `DELETE` request to the specified URL and ignores the response.
141
143
delete_ :: URL -> Aff (Response Unit )
142
- delete_ = delete ResponseFormat .ignore
144
+ delete_ = map (_ { body = unit }) <<< delete ResponseFormat .ignore
143
145
144
146
-- | 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 ) )
146
148
patch rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
147
149
148
150
-- | 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 ) )
150
152
patch' rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = c }
151
153
152
154
-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
153
155
-- | response.
154
156
patch_ :: URL -> RequestBody.RequestBody -> Aff (Response Unit )
155
- patch_ = patch ResponseFormat .ignore
157
+ patch_ url = map (_ { body = unit }) <<< patch ResponseFormat .ignore url
156
158
157
159
-- | Makes a `PATCH` request to the specified URL with the option to send data,
158
160
-- | and ignores the response.
159
161
patch_' :: URL -> Maybe RequestBody.RequestBody -> Aff (Response Unit )
160
- patch_' = patch' ResponseFormat .ignore
162
+ patch_' url = map (_ { body = unit }) <<< patch' ResponseFormat .ignore url
161
163
162
164
-- | A sequence of retry delays, in milliseconds.
163
165
type RetryDelayCurve = Int -> Milliseconds
@@ -236,12 +238,14 @@ retry policy run req = do
236
238
-- | ```purescript
237
239
-- | get json "/resource"
238
240
-- | ```
239
- request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff (Response a )
241
+ request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff (Response ( Either ResponseFormatError a ) )
240
242
request rt req = do
241
243
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' })
245
249
where
246
250
247
251
req' :: AjaxRequest a
0 commit comments