Skip to content

Commit 423ee7a

Browse files
authored
Merge pull request #129 from slamdata/request-name
Rename RequestOptions to Request
2 parents 36e8b71 + 4378332 commit 423ee7a

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Effect.Aff (launchAff)
3737
import Effect.Class.Console (log)
3838
3939
main = launchAff $ do
40-
res <- AX.request ResponseFormat.json (AX.defaultRequest { url = "/api", method = Left GET })
40+
res <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json })
4141
case res.body of
4242
Left err -> log $ "GET /api response failed to decode: " <> AX.printResponseFormatError err
4343
Right json -> log $ "GET /api response: " <> J.stringify json

src/Affjax.purs

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module 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
7778
defaultRequest =
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.
99101
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 })
101103

102104
-- | Makes a `POST` request to the specified URL, sending data.
103105
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 })
105107

106108
-- | Makes a `POST` request to the specified URL with the option to send data.
107109
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 })
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.
121123
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 })
123125

124126
-- | Makes a `PUT` request to the specified URL with the option to send data.
125127
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 })
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.
139141
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 })
141143

142144
-- | Makes a `DELETE` request to the specified URL and ignores the response.
143145
delete_ :: URL -> Aff (Response Unit)
144146
delete_ = map (_ { body = unit }) <<< delete ResponseFormat.ignore
145147

146148
-- | Makes a `PATCH` request to the specified URL, sending data.
147149
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 })
149151

150152
-- | Makes a `PATCH` request to the specified URL with the option to send data.
151153
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 })
153155

154156
-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
155157
-- | response.
@@ -183,7 +185,7 @@ defaultRetryPolicy =
183185
type 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)
187189
retry 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"

test/DocExamples.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Effect.Class.Console (log)
1414

1515
main :: Effect Unit
1616
main = void $ launchAff $ do
17-
res <- AX.request ResponseFormat.json (AX.defaultRequest { url = "/api", method = Left GET })
17+
res <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json })
1818
case res.body of
1919
Left err -> log $ "GET /api response failed to decode: " <> AX.printResponseFormatError err
2020
Right json -> log $ "GET /api response: " <> J.stringify json

test/Main.purs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ main = void $ runAff (either (\e -> logShow e *> throwException e) (const $ log
6767
let notJson = prefix "/not-json"
6868

6969
A.log "GET /does-not-exist: should be 404 Not found after retries"
70-
(attempt $ AX.retry retryPolicy (AX.request ResponseFormat.ignore) $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
70+
(attempt $ AX.retry retryPolicy AX.request $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
7171
assertEq notFound404 res.status
7272

7373
A.log "GET /mirror: should be 200 OK"
74-
(attempt $ AX.request ResponseFormat.ignore $ AX.defaultRequest { url = mirror }) >>= assertRight >>= \res -> do
74+
(attempt $ AX.request $ AX.defaultRequest { url = mirror }) >>= assertRight >>= \res -> do
7575
assertEq ok200 res.status
7676

7777
A.log "GET /does-not-exist: should be 404 Not found"
78-
(attempt $ AX.request ResponseFormat.ignore $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
78+
(attempt $ AX.request $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
7979
assertEq notFound404 res.status
8080

8181
A.log "GET /not-json: invalid JSON with Foreign response should return an error"

0 commit comments

Comments
 (0)