Skip to content

Commit 747968b

Browse files
committed
Tidy up
1 parent df55778 commit 747968b

File tree

4 files changed

+124
-107
lines changed

4 files changed

+124
-107
lines changed

src/Network/HTTP/Affjax.purs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Network.HTTP.Affjax
22
( Affjax
33
, AffjaxRequest, defaultRequest
4-
, AffjaxResponseT
4+
, AffjaxResponse
55
, URL
66
, affjax
77
, get
@@ -13,7 +13,6 @@ module Network.HTTP.Affjax
1313
, RetryPolicy(..)
1414
, defaultRetryPolicy
1515
, retry
16-
, module Exports
1716
) where
1817

1918
import Prelude
@@ -42,21 +41,20 @@ import Effect.Exception (Error, error)
4241
import Effect.Ref as Ref
4342
import Foreign (F, Foreign, ForeignError(..), fail, unsafeReadTagged, unsafeToForeign)
4443
import Math as Math
45-
import Network.HTTP.Affjax.Request (RequestContent(..), defaultMediaType)
46-
import Network.HTTP.Affjax.Request (RequestContent(..)) as Exports
47-
import Network.HTTP.Affjax.Response (AffjaxResponse(..), responseTypeToMediaType, responseTypeToString)
44+
import Network.HTTP.Affjax.Request as Request
45+
import Network.HTTP.Affjax.Response as Response
4846
import Network.HTTP.RequestHeader (RequestHeader(..), requestHeaderName, requestHeaderValue)
4947
import Network.HTTP.ResponseHeader (ResponseHeader, responseHeader)
5048
import Network.HTTP.StatusCode (StatusCode(..))
5149

5250
-- | The result type for Affjax requests.
53-
type Affjax a = Aff (AffjaxResponseT a)
51+
type Affjax a = Aff (AffjaxResponse a)
5452

5553
type AffjaxRequest =
5654
{ method :: Either Method CustomMethod
5755
, url :: URL
5856
, headers :: Array RequestHeader
59-
, content :: Maybe RequestContent
57+
, content :: Maybe Request.Request
6058
, username :: Maybe String
6159
, password :: Maybe String
6260
, withCredentials :: Boolean
@@ -74,7 +72,7 @@ defaultRequest =
7472
}
7573

7674
-- | The type of records that will be received as an Affjax response.
77-
type AffjaxResponseT a =
75+
type AffjaxResponse a =
7876
{ status :: StatusCode
7977
, headers :: Array ResponseHeader
8078
, response :: a
@@ -84,70 +82,70 @@ type AffjaxResponseT a =
8482
type URL = String
8583

8684
-- | Makes a `GET` request to the specified URL.
87-
get :: forall a. AffjaxResponse a -> URL -> Affjax a
85+
get :: forall a. Response.Response a -> URL -> Affjax a
8886
get rt u = affjax rt $ defaultRequest { url = u }
8987

9088
-- | Makes a `POST` request to the specified URL, sending data.
91-
post :: forall a. AffjaxResponse a -> URL -> RequestContent -> Affjax a
89+
post :: forall a. Response.Response a -> URL -> Request.Request -> Affjax a
9290
post rt u c = affjax rt $ defaultRequest { method = Left POST, url = u, content = Just c }
9391

9492
-- | Makes a `POST` request to the specified URL with the option to send data.
95-
post' :: forall a. AffjaxResponse a -> URL -> Maybe RequestContent -> Affjax a
93+
post' :: forall a. Response.Response a -> URL -> Maybe Request.Request -> Affjax a
9694
post' rt u c = affjax rt $ defaultRequest { method = Left POST, url = u, content = c }
9795

9896
-- | Makes a `POST` request to the specified URL, sending data and ignoring the
9997
-- | response.
100-
post_ :: URL -> RequestContent -> Affjax Unit
101-
post_ = post (IgnoredResponse identity)
98+
post_ :: URL -> Request.Request -> Affjax Unit
99+
post_ = post Response.ignore
102100

103101
-- | Makes a `POST` request to the specified URL with the option to send data,
104102
-- | and ignores the response.
105-
post_' :: URL -> Maybe RequestContent -> Affjax Unit
106-
post_' = post' (IgnoredResponse identity)
103+
post_' :: URL -> Maybe Request.Request -> Affjax Unit
104+
post_' = post' Response.ignore
107105

108106
-- | Makes a `PUT` request to the specified URL, sending data.
109-
put :: forall a. AffjaxResponse a -> URL -> RequestContent -> Affjax a
107+
put :: forall a. Response.Response a -> URL -> Request.Request -> Affjax a
110108
put rt u c = affjax rt $ defaultRequest { method = Left PUT, url = u, content = Just c }
111109

112110
-- | Makes a `PUT` request to the specified URL with the option to send data.
113-
put' :: forall a. AffjaxResponse a -> URL -> Maybe RequestContent -> Affjax a
111+
put' :: forall a. Response.Response a -> URL -> Maybe Request.Request -> Affjax a
114112
put' rt u c = affjax rt $ defaultRequest { method = Left PUT, url = u, content = c }
115113

116114
-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
117115
-- | response.
118-
put_ :: URL -> RequestContent -> Affjax Unit
119-
put_ = put (IgnoredResponse identity)
116+
put_ :: URL -> Request.Request -> Affjax Unit
117+
put_ = put Response.ignore
120118

121119
-- | Makes a `PUT` request to the specified URL with the option to send data,
122120
-- | and ignores the response.
123-
put_' :: URL -> Maybe RequestContent -> Affjax Unit
124-
put_' = put' (IgnoredResponse identity)
121+
put_' :: URL -> Maybe Request.Request -> Affjax Unit
122+
put_' = put' Response.ignore
125123

126124
-- | Makes a `DELETE` request to the specified URL.
127-
delete :: forall a. AffjaxResponse a -> URL -> Affjax a
125+
delete :: forall a. Response.Response a -> URL -> Affjax a
128126
delete rt u = affjax rt $ defaultRequest { method = Left DELETE, url = u }
129127

130128
-- | Makes a `DELETE` request to the specified URL and ignores the response.
131129
delete_ :: URL -> Affjax Unit
132-
delete_ = delete (IgnoredResponse identity)
130+
delete_ = delete Response.ignore
133131

134132
-- | Makes a `PATCH` request to the specified URL, sending data.
135-
patch :: forall a. AffjaxResponse a -> URL -> RequestContent -> Affjax a
133+
patch :: forall a. Response.Response a -> URL -> Request.Request -> Affjax a
136134
patch rt u c = affjax rt $ defaultRequest { method = Left PATCH, url = u, content = Just c }
137135

138136
-- | Makes a `PATCH` request to the specified URL with the option to send data.
139-
patch' :: forall a. AffjaxResponse a -> URL -> Maybe RequestContent -> Affjax a
137+
patch' :: forall a. Response.Response a -> URL -> Maybe Request.Request -> Affjax a
140138
patch' rt u c = affjax rt $ defaultRequest { method = Left PATCH, url = u, content = c }
141139

142140
-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
143141
-- | response.
144-
patch_ :: URL -> RequestContent -> Affjax Unit
145-
patch_ = patch (IgnoredResponse identity)
142+
patch_ :: URL -> Request.Request -> Affjax Unit
143+
patch_ = patch Response.ignore
146144

147145
-- | Makes a `PATCH` request to the specified URL with the option to send data,
148146
-- | and ignores the response.
149-
patch_' :: URL -> Maybe RequestContent -> Affjax Unit
150-
patch_' = patch' (IgnoredResponse identity)
147+
patch_' :: URL -> Maybe Request.Request -> Affjax Unit
148+
patch_' = patch' Response.ignore
151149

152150
-- | A sequence of retry delays, in milliseconds.
153151
type RetryDelayCurve = Int -> Milliseconds
@@ -189,8 +187,8 @@ retry policy run req = do
189187
Just resp -> pure resp
190188
where
191189
retryState
192-
:: Either Error (AffjaxResponseT a)
193-
-> RetryState Error (AffjaxResponseT a)
190+
:: Either Error (AffjaxResponse a)
191+
-> RetryState Error (AffjaxResponse a)
194192
retryState (Left exn) = Left $ Left exn
195193
retryState (Right resp) =
196194
case resp.status of
@@ -211,7 +209,7 @@ retry policy run req = do
211209
Right resp -> pure resp
212210

213211
-- | Makes an `Affjax` request.
214-
affjax :: forall a. AffjaxResponse a -> AffjaxRequest -> Affjax a
212+
affjax :: forall a. Response.Response a -> AffjaxRequest -> Affjax a
215213
affjax rt req = do
216214
res <- AC.fromEffectFnAff $ runFn2 _ajax responseHeader req'
217215
case runExcept (fromResponse' res.response) of
@@ -225,26 +223,26 @@ affjax rt req = do
225223
, url: req.url
226224
, headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> headers req.content
227225
, content: toNullable (extractContent <$> req.content)
228-
, responseType: responseTypeToString rt
226+
, responseType: Response.toResponseType rt
229227
, username: toNullable req.username
230228
, password: toNullable req.password
231229
, withCredentials: req.withCredentials
232230
}
233231

234-
extractContent :: RequestContent -> Foreign
232+
extractContent :: Request.Request -> Foreign
235233
extractContent = case _ of
236-
ArrayView f → f unsafeToForeign
237-
BlobRequest x → unsafeToForeign x
238-
DocumentRequest x → unsafeToForeign x
239-
StringRequest x → unsafeToForeign x
240-
FormDataRequest x → unsafeToForeign x
241-
FormURLEncodedRequest x → unsafeToForeign (FormURLEncoded.encode x)
242-
JsonRequest x → unsafeToForeign (J.stringify x)
243-
244-
headers :: Maybe RequestContent -> Array RequestHeader
234+
Request.ArrayView f → f unsafeToForeign
235+
Request.Blob x → unsafeToForeign x
236+
Request.Document x → unsafeToForeign x
237+
Request.String x → unsafeToForeign x
238+
Request.FormData x → unsafeToForeign x
239+
Request.FormURLEncoded x → unsafeToForeign (FormURLEncoded.encode x)
240+
Request.Json x → unsafeToForeign (J.stringify x)
241+
242+
headers :: Maybe Request.Request -> Array RequestHeader
245243
headers reqContent =
246-
addHeader (ContentType <$> (defaultMediaType =<< reqContent)) $
247-
addHeader (Accept <$> responseTypeToMediaType rt)
244+
addHeader (ContentType <$> (Request.toMediaType =<< reqContent)) $
245+
addHeader (Accept <$> Response.toMediaType rt)
248246
req.headers
249247

250248
addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
@@ -256,13 +254,13 @@ affjax rt req = do
256254
parseJSON = either (fail <<< ForeignError) pure <<< jsonParser
257255

258256
fromResponse' :: Foreign -> F a
259-
fromResponse' rc = case rt of
260-
ArrayBufferResponse coe -> unsafeReadTagged "ArrayBuffer" rc
261-
BlobResponse coe -> unsafeReadTagged "Blob" rc
262-
DocumentResponse coe -> unsafeReadTagged "Document" rc
263-
JSONResponse coe -> coe $ parseJSON =<< unsafeReadTagged "String" rc
264-
StringResponse coe -> unsafeReadTagged "String" rc
265-
IgnoredResponse coe -> coe $ pure unit
257+
fromResponse' = case rt of
258+
Response.ArrayBuffer _ -> unsafeReadTagged "ArrayBuffer"
259+
Response.Blob _ -> unsafeReadTagged "Blob"
260+
Response.Document _ -> unsafeReadTagged "Document"
261+
Response.Json coe -> coe <<< parseJSON <=< unsafeReadTagged "String"
262+
Response.String _ -> unsafeReadTagged "String"
263+
Response.Ignore coe -> const $ coe (pure unit)
266264

267265
type AjaxRequest a =
268266
{ method :: String
@@ -280,4 +278,4 @@ foreign import _ajax
280278
. Fn2
281279
(String -> String -> ResponseHeader)
282280
(AjaxRequest a)
283-
(AC.EffectFnAff (AffjaxResponseT Foreign))
281+
(AC.EffectFnAff (AffjaxResponse Foreign))

src/Network/HTTP/Affjax/Request.purs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,38 @@ import Web.DOM.Document (Document)
1010
import Web.File.Blob (Blob)
1111
import Web.XHR.FormData (FormData)
1212

13-
data RequestContent
13+
data Request
1414
= ArrayView (forall r. (forall a. A.ArrayView a -> r) -> r)
15-
| BlobRequest Blob
16-
| DocumentRequest Document
17-
| StringRequest String
18-
| FormDataRequest FormData
19-
| FormURLEncodedRequest FormURLEncoded
20-
| JsonRequest Json
21-
22-
arrayView :: forall a. A.ArrayView a -> RequestContent
15+
| Blob Blob
16+
| Document Document
17+
| String String
18+
| FormData FormData
19+
| FormURLEncoded FormURLEncoded
20+
| Json Json
21+
22+
arrayView :: forall a. A.ArrayView a -> Request
2323
arrayView av = ArrayView \f -> f av
2424

25-
defaultMediaType :: RequestContent -> Maybe MediaType
26-
defaultMediaType = case _ of
27-
FormURLEncodedRequest _ -> Just applicationFormURLEncoded
28-
JsonRequest _ -> Just applicationJSON
25+
blob :: Blob -> Request
26+
blob = Blob
27+
28+
document :: Document -> Request
29+
document = Document
30+
31+
string :: String -> Request
32+
string = String
33+
34+
formData :: FormData -> Request
35+
formData = FormData
36+
37+
formURLEncoded :: FormURLEncoded -> Request
38+
formURLEncoded = FormURLEncoded
39+
40+
json :: Json -> Request
41+
json = Json
42+
43+
toMediaType :: Request -> Maybe MediaType
44+
toMediaType = case _ of
45+
FormURLEncoded _ -> Just applicationFormURLEncoded
46+
Json _ -> Just applicationJSON
2947
_ -> Nothing

src/Network/HTTP/Affjax/Response.purs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,44 @@ import Data.MediaType.Common (applicationJSON)
1010
import Web.DOM.Document (Document)
1111
import Web.File.Blob (Blob)
1212

13-
data AffjaxResponse a
14-
= ArrayBufferResponse (forall f. f ArrayBuffer -> f a)
15-
| BlobResponse (forall f. f Blob -> f a)
16-
| DocumentResponse (forall f. f Document -> f a)
17-
| JSONResponse (forall f. f Json -> f a)
18-
| StringResponse (forall f. f String -> f a)
19-
| IgnoredResponse (forall f. f Unit -> f a)
13+
data Response a
14+
= ArrayBuffer (forall f. f ArrayBuffer -> f a)
15+
| Blob (forall f. f Blob -> f a)
16+
| Document (forall f. f Document -> f a)
17+
| Json (forall f. f Json -> f a)
18+
| String (forall f. f String -> f a)
19+
| Ignore (forall f. f Unit -> f a)
2020

21-
arrayBuffer :: AffjaxResponse ArrayBuffer
22-
arrayBuffer = ArrayBufferResponse identity
21+
arrayBuffer :: Response ArrayBuffer
22+
arrayBuffer = ArrayBuffer identity
2323

24-
blob :: AffjaxResponse Blob
25-
blob = BlobResponse identity
24+
blob :: Response Blob
25+
blob = Blob identity
2626

27-
document :: AffjaxResponse Document
28-
document = DocumentResponse identity
27+
document :: Response Document
28+
document = Document identity
2929

30-
json :: AffjaxResponse Json
31-
json = JSONResponse identity
30+
json :: Response Json
31+
json = Json identity
3232

33-
string :: AffjaxResponse String
34-
string = StringResponse identity
33+
string :: Response String
34+
string = String identity
3535

36-
ignored :: AffjaxResponse Unit
37-
ignored = IgnoredResponse identity
36+
ignore :: Response Unit
37+
ignore = Ignore identity
3838

39-
responseTypeToString :: forall a. AffjaxResponse a -> String
40-
responseTypeToString =
39+
toResponseType :: forall a. Response a -> String
40+
toResponseType =
4141
case _ of
42-
ArrayBufferResponse _ -> "arraybuffer"
43-
BlobResponse _ -> "blob"
44-
DocumentResponse _ -> "document"
45-
JSONResponse _ -> "text" -- IE doesn't support "json" responseType
46-
StringResponse _ -> "text"
47-
IgnoredResponse _ -> "text"
48-
49-
responseTypeToMediaType :: forall a. AffjaxResponse a -> Maybe MediaType
50-
responseTypeToMediaType =
42+
ArrayBuffer _ -> "arraybuffer"
43+
Blob _ -> "blob"
44+
Document _ -> "document"
45+
Json _ -> "text" -- IE doesn't support "json" responseType
46+
String _ -> "text"
47+
Ignore _ -> ""
48+
49+
toMediaType :: forall a. Response a -> Maybe MediaType
50+
toMediaType =
5151
case _ of
52-
JSONResponse _ -> Just applicationJSON
52+
Json _ -> Just applicationJSON
5353
_ -> Nothing

0 commit comments

Comments
 (0)