11module 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
1918import Prelude
@@ -42,21 +41,20 @@ import Effect.Exception (Error, error)
4241import Effect.Ref as Ref
4342import Foreign (F , Foreign , ForeignError (..), fail , unsafeReadTagged , unsafeToForeign )
4443import 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
4846import Network.HTTP.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
4947import Network.HTTP.ResponseHeader (ResponseHeader , responseHeader )
5048import 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
5553type 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 =
8482type 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
8886get 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
9290post 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
9694post' 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
110108put 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
114112put' 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
128126delete rt u = affjax rt $ defaultRequest { method = Left DELETE , url = u }
129127
130128-- | Makes a `DELETE` request to the specified URL and ignores the response.
131129delete_ :: 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
136134patch 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
140138patch' 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.
153151type 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
215213affjax 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
267265type 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 ))
0 commit comments