1
1
module Network.HTTP.Affjax
2
2
( Affjax
3
3
, AffjaxRequest , defaultRequest
4
- , AffjaxResponseT
4
+ , AffjaxResponse
5
5
, URL
6
6
, affjax
7
7
, get
@@ -13,7 +13,6 @@ module Network.HTTP.Affjax
13
13
, RetryPolicy (..)
14
14
, defaultRetryPolicy
15
15
, retry
16
- , module Exports
17
16
) where
18
17
19
18
import Prelude
@@ -42,21 +41,20 @@ import Effect.Exception (Error, error)
42
41
import Effect.Ref as Ref
43
42
import Foreign (F , Foreign , ForeignError (..), fail , unsafeReadTagged , unsafeToForeign )
44
43
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
48
46
import Network.HTTP.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
49
47
import Network.HTTP.ResponseHeader (ResponseHeader , responseHeader )
50
48
import Network.HTTP.StatusCode (StatusCode (..))
51
49
52
50
-- | The result type for Affjax requests.
53
- type Affjax a = Aff (AffjaxResponseT a )
51
+ type Affjax a = Aff (AffjaxResponse a )
54
52
55
53
type AffjaxRequest =
56
54
{ method :: Either Method CustomMethod
57
55
, url :: URL
58
56
, headers :: Array RequestHeader
59
- , content :: Maybe RequestContent
57
+ , content :: Maybe Request.Request
60
58
, username :: Maybe String
61
59
, password :: Maybe String
62
60
, withCredentials :: Boolean
@@ -74,7 +72,7 @@ defaultRequest =
74
72
}
75
73
76
74
-- | The type of records that will be received as an Affjax response.
77
- type AffjaxResponseT a =
75
+ type AffjaxResponse a =
78
76
{ status :: StatusCode
79
77
, headers :: Array ResponseHeader
80
78
, response :: a
@@ -84,70 +82,70 @@ type AffjaxResponseT a =
84
82
type URL = String
85
83
86
84
-- | 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
88
86
get rt u = affjax rt $ defaultRequest { url = u }
89
87
90
88
-- | 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
92
90
post rt u c = affjax rt $ defaultRequest { method = Left POST , url = u, content = Just c }
93
91
94
92
-- | 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
96
94
post' rt u c = affjax rt $ defaultRequest { method = Left POST , url = u, content = c }
97
95
98
96
-- | Makes a `POST` request to the specified URL, sending data and ignoring the
99
97
-- | response.
100
- post_ :: URL -> RequestContent -> Affjax Unit
101
- post_ = post ( IgnoredResponse identity)
98
+ post_ :: URL -> Request.Request -> Affjax Unit
99
+ post_ = post Response .ignore
102
100
103
101
-- | Makes a `POST` request to the specified URL with the option to send data,
104
102
-- | 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
107
105
108
106
-- | 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
110
108
put rt u c = affjax rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
111
109
112
110
-- | 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
114
112
put' rt u c = affjax rt $ defaultRequest { method = Left PUT , url = u, content = c }
115
113
116
114
-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
117
115
-- | response.
118
- put_ :: URL -> RequestContent -> Affjax Unit
119
- put_ = put ( IgnoredResponse identity)
116
+ put_ :: URL -> Request.Request -> Affjax Unit
117
+ put_ = put Response .ignore
120
118
121
119
-- | Makes a `PUT` request to the specified URL with the option to send data,
122
120
-- | 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
125
123
126
124
-- | 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
128
126
delete rt u = affjax rt $ defaultRequest { method = Left DELETE , url = u }
129
127
130
128
-- | Makes a `DELETE` request to the specified URL and ignores the response.
131
129
delete_ :: URL -> Affjax Unit
132
- delete_ = delete ( IgnoredResponse identity)
130
+ delete_ = delete Response .ignore
133
131
134
132
-- | 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
136
134
patch rt u c = affjax rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
137
135
138
136
-- | 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
140
138
patch' rt u c = affjax rt $ defaultRequest { method = Left PATCH , url = u, content = c }
141
139
142
140
-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
143
141
-- | response.
144
- patch_ :: URL -> RequestContent -> Affjax Unit
145
- patch_ = patch ( IgnoredResponse identity)
142
+ patch_ :: URL -> Request.Request -> Affjax Unit
143
+ patch_ = patch Response .ignore
146
144
147
145
-- | Makes a `PATCH` request to the specified URL with the option to send data,
148
146
-- | 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
151
149
152
150
-- | A sequence of retry delays, in milliseconds.
153
151
type RetryDelayCurve = Int -> Milliseconds
@@ -189,8 +187,8 @@ retry policy run req = do
189
187
Just resp -> pure resp
190
188
where
191
189
retryState
192
- :: Either Error (AffjaxResponseT a )
193
- -> RetryState Error (AffjaxResponseT a )
190
+ :: Either Error (AffjaxResponse a )
191
+ -> RetryState Error (AffjaxResponse a )
194
192
retryState (Left exn) = Left $ Left exn
195
193
retryState (Right resp) =
196
194
case resp.status of
@@ -211,7 +209,7 @@ retry policy run req = do
211
209
Right resp -> pure resp
212
210
213
211
-- | Makes an `Affjax` request.
214
- affjax :: forall a . AffjaxResponse a -> AffjaxRequest -> Affjax a
212
+ affjax :: forall a . Response.Response a -> AffjaxRequest -> Affjax a
215
213
affjax rt req = do
216
214
res <- AC .fromEffectFnAff $ runFn2 _ajax responseHeader req'
217
215
case runExcept (fromResponse' res.response) of
@@ -225,26 +223,26 @@ affjax rt req = do
225
223
, url: req.url
226
224
, headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> headers req.content
227
225
, content: toNullable (extractContent <$> req.content)
228
- , responseType: responseTypeToString rt
226
+ , responseType: Response .toResponseType rt
229
227
, username: toNullable req.username
230
228
, password: toNullable req.password
231
229
, withCredentials: req.withCredentials
232
230
}
233
231
234
- extractContent :: RequestContent -> Foreign
232
+ extractContent :: Request.Request -> Foreign
235
233
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
245
243
headers reqContent =
246
- addHeader (ContentType <$> (defaultMediaType =<< reqContent)) $
247
- addHeader (Accept <$> responseTypeToMediaType rt)
244
+ addHeader (ContentType <$> (Request .toMediaType =<< reqContent)) $
245
+ addHeader (Accept <$> Response .toMediaType rt)
248
246
req.headers
249
247
250
248
addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
@@ -256,13 +254,13 @@ affjax rt req = do
256
254
parseJSON = either (fail <<< ForeignError ) pure <<< jsonParser
257
255
258
256
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)
266
264
267
265
type AjaxRequest a =
268
266
{ method :: String
@@ -280,4 +278,4 @@ foreign import _ajax
280
278
. Fn2
281
279
(String -> String -> ResponseHeader )
282
280
(AjaxRequest a )
283
- (AC.EffectFnAff (AffjaxResponseT Foreign ))
281
+ (AC.EffectFnAff (AffjaxResponse Foreign ))
0 commit comments