1
1
module Network.HTTP.Affjax
2
- ( Affjax
3
- , AffjaxRequest , defaultRequest
4
- , AffjaxResponse
2
+ ( RequestOptions , defaultRequest
3
+ , Response
5
4
, URL
6
- , affjax
5
+ , request
7
6
, get
8
7
, post , post_ , post' , post_'
9
8
, put , put_ , put' , put_'
@@ -42,29 +41,26 @@ import Effect.Exception (Error, error)
42
41
import Effect.Ref as Ref
43
42
import Foreign (F , Foreign , ForeignError (..), fail , renderForeignError , unsafeReadTagged , unsafeToForeign )
44
43
import Math as Math
45
- import Network.HTTP.Affjax.Request as Request
46
- import Network.HTTP.Affjax.Response as Response
44
+ import Network.HTTP.Affjax.RequestBody as RequestBody
45
+ import Network.HTTP.Affjax.ResponseFormat as ResponseFormat
47
46
import Network.HTTP.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
48
47
import Network.HTTP.ResponseHeader (ResponseHeader , responseHeader )
49
48
import Network.HTTP.StatusCode (StatusCode (..))
50
49
51
- -- | The result type for Affjax requests.
52
- type Affjax a = Aff (AffjaxResponse a )
53
-
54
50
-- | A record that contains all the information to perform an HTTP request.
55
51
-- | Instead of constructing the record from scratch it is often easier to build
56
52
-- | one based on `defaultRequest`.
57
- type AffjaxRequest =
53
+ type RequestOptions =
58
54
{ method :: Either Method CustomMethod
59
55
, url :: URL
60
56
, headers :: Array RequestHeader
61
- , content :: Maybe Request.Request
57
+ , content :: Maybe RequestBody.RequestBody
62
58
, username :: Maybe String
63
59
, password :: Maybe String
64
60
, withCredentials :: Boolean
65
61
}
66
62
67
- -- | A record of the type `AffjaxRequest ` that has all fields set to default
63
+ -- | A record of the type `RequestOptions ` that has all fields set to default
68
64
-- | values. This record can be used as the foundation for constructing
69
65
-- | custom requests.
70
66
-- |
@@ -75,7 +71,7 @@ type AffjaxRequest =
75
71
-- | ```
76
72
-- |
77
73
-- | Represents a POST request to the URL `/api/user`.
78
- defaultRequest :: AffjaxRequest
74
+ defaultRequest :: RequestOptions
79
75
defaultRequest =
80
76
{ method: Left GET
81
77
, url: " /"
@@ -86,8 +82,8 @@ defaultRequest =
86
82
, withCredentials: false
87
83
}
88
84
89
- -- | The type of records that will be received as an Affjax response.
90
- type AffjaxResponse a =
85
+ -- | The type of records that represents a received HTTP response.
86
+ type Response a =
91
87
{ status :: StatusCode
92
88
, statusText :: String
93
89
, headers :: Array ResponseHeader
@@ -98,75 +94,75 @@ type AffjaxResponse a =
98
94
type URL = String
99
95
100
96
-- | Makes a `GET` request to the specified URL.
101
- get :: forall a . Response.Response a -> URL -> Affjax a
102
- get rt u = affjax rt $ defaultRequest { url = u }
97
+ get :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff ( Response a )
98
+ get rt u = request rt $ defaultRequest { url = u }
103
99
104
100
-- | Makes a `POST` request to the specified URL, sending data.
105
- post :: forall a . Response.Response a -> URL -> Request.Request -> Affjax a
106
- post rt u c = affjax rt $ defaultRequest { method = Left POST , url = u, content = Just c }
101
+ post :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff ( Response a )
102
+ post rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = Just c }
107
103
108
104
-- | Makes a `POST` request to the specified URL with the option to send data.
109
- post' :: forall a . Response.Response a -> URL -> Maybe Request.Request -> Affjax a
110
- post' rt u c = affjax rt $ defaultRequest { method = Left POST , url = u, content = c }
105
+ post' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff ( Response a )
106
+ post' rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = c }
111
107
112
108
-- | Makes a `POST` request to the specified URL, sending data and ignoring the
113
109
-- | response.
114
- post_ :: URL -> Request.Request -> Affjax Unit
115
- post_ = post Response .ignore
110
+ post_ :: URL -> RequestBody.RequestBody -> Aff ( Response Unit )
111
+ post_ = post ResponseFormat .ignore
116
112
117
113
-- | Makes a `POST` request to the specified URL with the option to send data,
118
114
-- | and ignores the response.
119
- post_' :: URL -> Maybe Request.Request -> Affjax Unit
120
- post_' = post' Response .ignore
115
+ post_' :: URL -> Maybe RequestBody.RequestBody -> Aff ( Response Unit )
116
+ post_' = post' ResponseFormat .ignore
121
117
122
118
-- | Makes a `PUT` request to the specified URL, sending data.
123
- put :: forall a . Response.Response a -> URL -> Request.Request -> Affjax a
124
- put rt u c = affjax rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
119
+ put :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff ( Response a )
120
+ put rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
125
121
126
122
-- | Makes a `PUT` request to the specified URL with the option to send data.
127
- put' :: forall a . Response.Response a -> URL -> Maybe Request.Request -> Affjax a
128
- put' rt u c = affjax rt $ defaultRequest { method = Left PUT , url = u, content = c }
123
+ put' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff ( Response a )
124
+ put' rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = c }
129
125
130
126
-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
131
127
-- | response.
132
- put_ :: URL -> Request.Request -> Affjax Unit
133
- put_ = put Response .ignore
128
+ put_ :: URL -> RequestBody.RequestBody -> Aff ( Response Unit )
129
+ put_ = put ResponseFormat .ignore
134
130
135
131
-- | Makes a `PUT` request to the specified URL with the option to send data,
136
132
-- | and ignores the response.
137
- put_' :: URL -> Maybe Request.Request -> Affjax Unit
138
- put_' = put' Response .ignore
133
+ put_' :: URL -> Maybe RequestBody.RequestBody -> Aff ( Response Unit )
134
+ put_' = put' ResponseFormat .ignore
139
135
140
136
-- | Makes a `DELETE` request to the specified URL.
141
- delete :: forall a . Response.Response a -> URL -> Affjax a
142
- delete rt u = affjax rt $ defaultRequest { method = Left DELETE , url = u }
137
+ delete :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff ( Response a )
138
+ delete rt u = request rt $ defaultRequest { method = Left DELETE , url = u }
143
139
144
140
-- | Makes a `DELETE` request to the specified URL and ignores the response.
145
- delete_ :: URL -> Affjax Unit
146
- delete_ = delete Response .ignore
141
+ delete_ :: URL -> Aff ( Response Unit )
142
+ delete_ = delete ResponseFormat .ignore
147
143
148
144
-- | Makes a `PATCH` request to the specified URL, sending data.
149
- patch :: forall a . Response.Response a -> URL -> Request.Request -> Affjax a
150
- patch rt u c = affjax rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
145
+ patch :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff ( Response a )
146
+ patch rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
151
147
152
148
-- | Makes a `PATCH` request to the specified URL with the option to send data.
153
- patch' :: forall a . Response.Response a -> URL -> Maybe Request.Request -> Affjax a
154
- patch' rt u c = affjax rt $ defaultRequest { method = Left PATCH , url = u, content = c }
149
+ patch' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff ( Response a )
150
+ patch' rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = c }
155
151
156
152
-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
157
153
-- | response.
158
- patch_ :: URL -> Request.Request -> Affjax Unit
159
- patch_ = patch Response .ignore
154
+ patch_ :: URL -> RequestBody.RequestBody -> Aff ( Response Unit )
155
+ patch_ = patch ResponseFormat .ignore
160
156
161
157
-- | Makes a `PATCH` request to the specified URL with the option to send data,
162
158
-- | and ignores the response.
163
- patch_' :: URL -> Maybe Request.Request -> Affjax Unit
164
- patch_' = patch' Response .ignore
159
+ patch_' :: URL -> Maybe RequestBody.RequestBody -> Aff ( Response Unit )
160
+ patch_' = patch' ResponseFormat .ignore
165
161
166
162
-- | A sequence of retry delays, in milliseconds.
167
163
type RetryDelayCurve = Int -> Milliseconds
168
164
169
- -- | Expresses a policy for retrying Affjax requests with backoff.
165
+ -- | Expresses a policy for retrying HTTP requests with backoff.
170
166
type RetryPolicy =
171
167
{ timeout :: Maybe Milliseconds -- ^ the timeout in milliseconds, optional
172
168
, delayCurve :: RetryDelayCurve
@@ -185,7 +181,7 @@ defaultRetryPolicy =
185
181
type RetryState e a = Either (Either e a ) a
186
182
187
183
-- | 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.
188
- retry :: forall a . RetryPolicy -> (AffjaxRequest -> Affjax a ) -> AffjaxRequest -> Affjax a
184
+ retry :: forall a . RetryPolicy -> (RequestOptions -> Aff ( Response a )) -> RequestOptions -> Aff ( Response a )
189
185
retry policy run req = do
190
186
-- failureRef is either an exception or a failed request
191
187
failureRef <- liftEffect $ Ref .new Nothing
@@ -203,8 +199,8 @@ retry policy run req = do
203
199
Just resp -> pure resp
204
200
where
205
201
retryState
206
- :: Either Error (AffjaxResponse a )
207
- -> RetryState Error (AffjaxResponse a )
202
+ :: Either Error (Response a )
203
+ -> RetryState Error (Response a )
208
204
retryState (Left exn) = Left $ Left exn
209
205
retryState (Right resp) =
210
206
case resp.status of
@@ -231,17 +227,17 @@ retry policy run req = do
231
227
-- | interprets the response body as JSON.
232
228
-- |
233
229
-- | ```purescript
234
- -- | affjax json (defaultRequest { url = "/resource", method = Left GET })
230
+ -- | request json (defaultRequest { url = "/resource", method = Left GET })
235
231
-- | ```
236
232
-- |
237
- -- | For common cases helper functions can often be used instead of `affjax ` .
233
+ -- | For common cases helper functions can often be used instead of `request ` .
238
234
-- | For instance, the above example is equivalent to the following.
239
235
-- |
240
236
-- | ```purescript
241
237
-- | get json "/resource"
242
238
-- | ```
243
- affjax :: forall a . Response.Response a -> AffjaxRequest -> Affjax a
244
- affjax rt req = do
239
+ request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff ( Response a )
240
+ request rt req = do
245
241
res <- AC .fromEffectFnAff $ runFn2 _ajax responseHeader req'
246
242
case runExcept (fromResponse' res.response) of
247
243
Left err -> throwError $ error $ intercalate " \n " (map renderForeignError err)
@@ -254,26 +250,26 @@ affjax rt req = do
254
250
, url: req.url
255
251
, headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> headers req.content
256
252
, content: toNullable (extractContent <$> req.content)
257
- , responseType: Response .toResponseType rt
253
+ , responseType: ResponseFormat .toResponseType rt
258
254
, username: toNullable req.username
259
255
, password: toNullable req.password
260
256
, withCredentials: req.withCredentials
261
257
}
262
258
263
- extractContent :: Request.Request -> Foreign
259
+ extractContent :: RequestBody.RequestBody -> Foreign
264
260
extractContent = case _ of
265
- Request .ArrayView f → f unsafeToForeign
266
- Request .Blob x → unsafeToForeign x
267
- Request .Document x → unsafeToForeign x
268
- Request .String x → unsafeToForeign x
269
- Request .FormData x → unsafeToForeign x
270
- Request .FormURLEncoded x → unsafeToForeign (FormURLEncoded .encode x)
271
- Request .Json x → unsafeToForeign (J .stringify x)
272
-
273
- headers :: Maybe Request.Request -> Array RequestHeader
261
+ RequestBody .ArrayView f → f unsafeToForeign
262
+ RequestBody .Blob x → unsafeToForeign x
263
+ RequestBody .Document x → unsafeToForeign x
264
+ RequestBody .String x → unsafeToForeign x
265
+ RequestBody .FormData x → unsafeToForeign x
266
+ RequestBody .FormURLEncoded x → unsafeToForeign (FormURLEncoded .encode x)
267
+ RequestBody .Json x → unsafeToForeign (J .stringify x)
268
+
269
+ headers :: Maybe RequestBody.RequestBody -> Array RequestHeader
274
270
headers reqContent =
275
- addHeader (ContentType <$> (Request .toMediaType =<< reqContent)) $
276
- addHeader (Accept <$> Response .toMediaType rt)
271
+ addHeader (ContentType <$> (RequestBody .toMediaType =<< reqContent)) $
272
+ addHeader (Accept <$> ResponseFormat .toMediaType rt)
277
273
req.headers
278
274
279
275
addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
@@ -288,12 +284,12 @@ affjax rt req = do
288
284
289
285
fromResponse' :: Foreign -> F a
290
286
fromResponse' = case rt of
291
- Response .ArrayBuffer _ -> unsafeReadTagged " ArrayBuffer"
292
- Response .Blob _ -> unsafeReadTagged " Blob"
293
- Response .Document _ -> unsafeReadTagged " Document"
294
- Response .Json coe -> coe <<< parseJSON <=< unsafeReadTagged " String"
295
- Response .String _ -> unsafeReadTagged " String"
296
- Response .Ignore coe -> const $ coe (pure unit)
287
+ ResponseFormat .ArrayBuffer _ -> unsafeReadTagged " ArrayBuffer"
288
+ ResponseFormat .Blob _ -> unsafeReadTagged " Blob"
289
+ ResponseFormat .Document _ -> unsafeReadTagged " Document"
290
+ ResponseFormat .Json coe -> coe <<< parseJSON <=< unsafeReadTagged " String"
291
+ ResponseFormat .String _ -> unsafeReadTagged " String"
292
+ ResponseFormat .Ignore coe -> const $ coe (pure unit)
297
293
298
294
type AjaxRequest a =
299
295
{ method :: String
@@ -311,4 +307,4 @@ foreign import _ajax
311
307
. Fn2
312
308
(String -> String -> ResponseHeader )
313
309
(AjaxRequest a )
314
- (AC.EffectFnAff (AffjaxResponse Foreign ))
310
+ (AC.EffectFnAff (Response Foreign ))
0 commit comments