1
1
module Affjax
2
- ( RequestOptions , defaultRequest
2
+ ( Request , defaultRequest
3
3
, Response
4
4
, URL
5
5
, request
@@ -52,28 +52,29 @@ import Math as Math
52
52
-- | A record that contains all the information to perform an HTTP request.
53
53
-- | Instead of constructing the record from scratch it is often easier to build
54
54
-- | one based on `defaultRequest`.
55
- type RequestOptions =
55
+ type Request a =
56
56
{ method :: Either Method CustomMethod
57
57
, url :: URL
58
58
, headers :: Array RequestHeader
59
59
, content :: Maybe RequestBody.RequestBody
60
60
, username :: Maybe String
61
61
, password :: Maybe String
62
62
, withCredentials :: Boolean
63
+ , responseFormat :: ResponseFormat.ResponseFormat a
63
64
}
64
65
65
- -- | A record of the type `RequestOptions ` that has all fields set to default
66
+ -- | A record of the type `Request ` that has all fields set to default
66
67
-- | values. This record can be used as the foundation for constructing
67
68
-- | custom requests.
68
69
-- |
69
- -- | As an example
70
+ -- | As an example:
70
71
-- |
71
72
-- | ```purescript
72
73
-- | defaultRequest { url = "/api/user", method = Left POST }
73
74
-- | ```
74
75
-- |
75
- -- | Represents a POST request to the URL `/api/user`.
76
- defaultRequest :: RequestOptions
76
+ -- | Would represents a POST request to the URL `/api/user`.
77
+ defaultRequest :: Request Unit
77
78
defaultRequest =
78
79
{ method: Left GET
79
80
, url: " /"
@@ -82,6 +83,7 @@ defaultRequest =
82
83
, username: Nothing
83
84
, password: Nothing
84
85
, withCredentials: false
86
+ , responseFormat: ResponseFormat .ignore
85
87
}
86
88
87
89
-- | The type of records that represents a received HTTP response.
@@ -97,15 +99,15 @@ type URL = String
97
99
98
100
-- | Makes a `GET` request to the specified URL.
99
101
get :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff (Response (Either ResponseFormatError a ))
100
- get rt u = request rt $ defaultRequest { url = u }
102
+ get rf u = request ( defaultRequest { url = u, responseFormat = rf })
101
103
102
104
-- | Makes a `POST` request to the specified URL, sending data.
103
105
post :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response (Either ResponseFormatError a ))
104
- post rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = Just c }
106
+ post rf u c = request ( defaultRequest { method = Left POST , url = u, content = Just c, responseFormat = rf })
105
107
106
108
-- | Makes a `POST` request to the specified URL with the option to send data.
107
109
post' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response (Either ResponseFormatError a ))
108
- post' rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = c }
110
+ post' rf u c = request ( defaultRequest { method = Left POST , url = u, content = c, responseFormat = rf })
109
111
110
112
-- | Makes a `POST` request to the specified URL, sending data and ignoring the
111
113
-- | response.
@@ -119,11 +121,11 @@ post_' url = map (_ { body = unit }) <<< post' ResponseFormat.ignore url
119
121
120
122
-- | Makes a `PUT` request to the specified URL, sending data.
121
123
put :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response (Either ResponseFormatError a ))
122
- put rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
124
+ put rf u c = request ( defaultRequest { method = Left PUT , url = u, content = Just c, responseFormat = rf })
123
125
124
126
-- | Makes a `PUT` request to the specified URL with the option to send data.
125
127
put' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response (Either ResponseFormatError a ))
126
- put' rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = c }
128
+ put' rf u c = request ( defaultRequest { method = Left PUT , url = u, content = c, responseFormat = rf })
127
129
128
130
-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
129
131
-- | response.
@@ -137,19 +139,19 @@ put_' url = map (_ { body = unit }) <<< put' ResponseFormat.ignore url
137
139
138
140
-- | Makes a `DELETE` request to the specified URL.
139
141
delete :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff (Response (Either ResponseFormatError a ))
140
- delete rt u = request rt $ defaultRequest { method = Left DELETE , url = u }
142
+ delete rf u = request ( defaultRequest { method = Left DELETE , url = u, responseFormat = rf })
141
143
142
144
-- | Makes a `DELETE` request to the specified URL and ignores the response.
143
145
delete_ :: URL -> Aff (Response Unit )
144
146
delete_ = map (_ { body = unit }) <<< delete ResponseFormat .ignore
145
147
146
148
-- | Makes a `PATCH` request to the specified URL, sending data.
147
149
patch :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff (Response (Either ResponseFormatError a ))
148
- patch rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
150
+ patch rf u c = request ( defaultRequest { method = Left PATCH , url = u, content = Just c, responseFormat = rf })
149
151
150
152
-- | Makes a `PATCH` request to the specified URL with the option to send data.
151
153
patch' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff (Response (Either ResponseFormatError a ))
152
- patch' rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = c }
154
+ patch' rf u c = request ( defaultRequest { method = Left PATCH , url = u, content = c, responseFormat = rf })
153
155
154
156
-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
155
157
-- | response.
@@ -183,7 +185,7 @@ defaultRetryPolicy =
183
185
type RetryState e a = Either (Either e a ) a
184
186
185
187
-- | Retry a request using a `RetryPolicy`. After the timeout, the last received response is returned; if it was not possible to communicate with the server due to an error, then this is bubbled up.
186
- retry :: forall a . RetryPolicy -> (RequestOptions -> Aff (Response a )) -> RequestOptions -> Aff (Response a )
188
+ retry :: forall a b . RetryPolicy -> (Request a -> Aff (Response b )) -> Request a -> Aff (Response b )
187
189
retry policy run req = do
188
190
-- failureRef is either an exception or a failed request
189
191
failureRef <- liftEffect $ Ref .new Nothing
@@ -201,8 +203,8 @@ retry policy run req = do
201
203
Just resp -> pure resp
202
204
where
203
205
retryState
204
- :: Either Error (Response a )
205
- -> RetryState Error (Response a )
206
+ :: Either Error (Response b )
207
+ -> RetryState Error (Response b )
206
208
retryState (Left exn) = Left $ Left exn
207
209
retryState (Right resp) =
208
210
case resp.status of
@@ -238,8 +240,8 @@ retry policy run req = do
238
240
-- | ```purescript
239
241
-- | get json "/resource"
240
242
-- | ```
241
- request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff (Response (Either ResponseFormatError a ))
242
- request rt req = do
243
+ request :: forall a . Request a -> Aff (Response (Either ResponseFormatError a ))
244
+ request req = do
243
245
res <- AC .fromEffectFnAff $ runFn2 _ajax responseHeader req'
244
246
case runExcept (fromResponse' res.body) of
245
247
Left err -> do
@@ -254,7 +256,7 @@ request rt req = do
254
256
, url: req.url
255
257
, headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> headers req.content
256
258
, content: toNullable (extractContent <$> req.content)
257
- , responseType: ResponseFormat .toResponseType rt
259
+ , responseType: ResponseFormat .toResponseType req.responseFormat
258
260
, username: toNullable req.username
259
261
, password: toNullable req.password
260
262
, withCredentials: req.withCredentials
@@ -273,7 +275,7 @@ request rt req = do
273
275
headers :: Maybe RequestBody.RequestBody -> Array RequestHeader
274
276
headers reqContent =
275
277
addHeader (ContentType <$> (RequestBody .toMediaType =<< reqContent)) $
276
- addHeader (Accept <$> ResponseFormat .toMediaType rt )
278
+ addHeader (Accept <$> ResponseFormat .toMediaType req.responseFormat )
277
279
req.headers
278
280
279
281
addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
@@ -287,7 +289,7 @@ request rt req = do
287
289
str -> either (fail <<< ForeignError ) pure (jsonParser str)
288
290
289
291
fromResponse' :: Foreign -> F a
290
- fromResponse' = case rt of
292
+ fromResponse' = case req.responseFormat of
291
293
ResponseFormat.ArrayBuffer _ -> unsafeReadTagged " ArrayBuffer"
292
294
ResponseFormat.Blob _ -> unsafeReadTagged " Blob"
293
295
ResponseFormat.Document _ -> unsafeReadTagged " Document"
0 commit comments