11module Affjax
2- ( RequestOptions , defaultRequest
2+ ( Request , defaultRequest
33 , Response
44 , URL
55 , request
@@ -52,28 +52,29 @@ import Math as Math
5252-- | A record that contains all the information to perform an HTTP request.
5353-- | Instead of constructing the record from scratch it is often easier to build
5454-- | one based on `defaultRequest`.
55- type RequestOptions =
55+ type Request a =
5656 { method :: Either Method CustomMethod
5757 , url :: URL
5858 , headers :: Array RequestHeader
5959 , content :: Maybe RequestBody.RequestBody
6060 , username :: Maybe String
6161 , password :: Maybe String
6262 , withCredentials :: Boolean
63+ , responseFormat :: ResponseFormat.ResponseFormat a
6364 }
6465
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
6667-- | values. This record can be used as the foundation for constructing
6768-- | custom requests.
6869-- |
69- -- | As an example
70+ -- | As an example:
7071-- |
7172-- | ```purescript
7273-- | defaultRequest { url = "/api/user", method = Left POST }
7374-- | ```
7475-- |
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
7778defaultRequest =
7879 { method: Left GET
7980 , url: " /"
@@ -82,6 +83,7 @@ defaultRequest =
8283 , username: Nothing
8384 , password: Nothing
8485 , withCredentials: false
86+ , responseFormat: ResponseFormat .ignore
8587 }
8688
8789-- | The type of records that represents a received HTTP response.
@@ -97,15 +99,15 @@ type URL = String
9799
98100-- | Makes a `GET` request to the specified URL.
99101get :: 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 })
101103
102104-- | Makes a `POST` request to the specified URL, sending data.
103105post :: 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 })
105107
106108-- | Makes a `POST` request to the specified URL with the option to send data.
107109post' :: 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 })
109111
110112-- | Makes a `POST` request to the specified URL, sending data and ignoring the
111113-- | response.
@@ -119,11 +121,11 @@ post_' url = map (_ { body = unit }) <<< post' ResponseFormat.ignore url
119121
120122-- | Makes a `PUT` request to the specified URL, sending data.
121123put :: 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 })
123125
124126-- | Makes a `PUT` request to the specified URL with the option to send data.
125127put' :: 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 })
127129
128130-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
129131-- | response.
@@ -137,19 +139,19 @@ put_' url = map (_ { body = unit }) <<< put' ResponseFormat.ignore url
137139
138140-- | Makes a `DELETE` request to the specified URL.
139141delete :: 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 })
141143
142144-- | Makes a `DELETE` request to the specified URL and ignores the response.
143145delete_ :: URL -> Aff (Response Unit )
144146delete_ = map (_ { body = unit }) <<< delete ResponseFormat .ignore
145147
146148-- | Makes a `PATCH` request to the specified URL, sending data.
147149patch :: 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 })
149151
150152-- | Makes a `PATCH` request to the specified URL with the option to send data.
151153patch' :: 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 })
153155
154156-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
155157-- | response.
@@ -183,7 +185,7 @@ defaultRetryPolicy =
183185type RetryState e a = Either (Either e a ) a
184186
185187-- | 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 )
187189retry policy run req = do
188190 -- failureRef is either an exception or a failed request
189191 failureRef <- liftEffect $ Ref .new Nothing
@@ -201,8 +203,8 @@ retry policy run req = do
201203 Just resp -> pure resp
202204 where
203205 retryState
204- :: Either Error (Response a )
205- -> RetryState Error (Response a )
206+ :: Either Error (Response b )
207+ -> RetryState Error (Response b )
206208 retryState (Left exn) = Left $ Left exn
207209 retryState (Right resp) =
208210 case resp.status of
@@ -238,8 +240,8 @@ retry policy run req = do
238240-- | ```purescript
239241-- | get json "/resource"
240242-- | ```
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
243245 res <- AC .fromEffectFnAff $ runFn2 _ajax responseHeader req'
244246 case runExcept (fromResponse' res.body) of
245247 Left err -> do
@@ -254,7 +256,7 @@ request rt req = do
254256 , url: req.url
255257 , headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> headers req.content
256258 , content: toNullable (extractContent <$> req.content)
257- , responseType: ResponseFormat .toResponseType rt
259+ , responseType: ResponseFormat .toResponseType req.responseFormat
258260 , username: toNullable req.username
259261 , password: toNullable req.password
260262 , withCredentials: req.withCredentials
@@ -273,7 +275,7 @@ request rt req = do
273275 headers :: Maybe RequestBody.RequestBody -> Array RequestHeader
274276 headers reqContent =
275277 addHeader (ContentType <$> (RequestBody .toMediaType =<< reqContent)) $
276- addHeader (Accept <$> ResponseFormat .toMediaType rt )
278+ addHeader (Accept <$> ResponseFormat .toMediaType req.responseFormat )
277279 req.headers
278280
279281 addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
@@ -287,7 +289,7 @@ request rt req = do
287289 str -> either (fail <<< ForeignError ) pure (jsonParser str)
288290
289291 fromResponse' :: Foreign -> F a
290- fromResponse' = case rt of
292+ fromResponse' = case req.responseFormat of
291293 ResponseFormat.ArrayBuffer _ -> unsafeReadTagged " ArrayBuffer"
292294 ResponseFormat.Blob _ -> unsafeReadTagged " Blob"
293295 ResponseFormat.Document _ -> unsafeReadTagged " Document"
0 commit comments