11module Network.HTTP.Affjax
2- ( Affjax
3- , AffjaxRequest , defaultRequest
4- , AffjaxResponse
2+ ( RequestOptions , defaultRequest
3+ , Response
54 , URL
6- , affjax
5+ , request
76 , get
87 , post , post_ , post' , post_'
98 , put , put_ , put' , put_'
@@ -42,29 +41,26 @@ import Effect.Exception (Error, error)
4241import Effect.Ref as Ref
4342import Foreign (F , Foreign , ForeignError (..), fail , renderForeignError , unsafeReadTagged , unsafeToForeign )
4443import Math as Math
45- import Network.HTTP.Affjax.Request as Request
46- import Network.HTTP.Affjax.Response as Response
44+ import Network.HTTP.Affjax.RequestBody as RequestBody
45+ import Network.HTTP.Affjax.ResponseFormat as ResponseFormat
4746import Network.HTTP.RequestHeader (RequestHeader (..), requestHeaderName , requestHeaderValue )
4847import Network.HTTP.ResponseHeader (ResponseHeader , responseHeader )
4948import Network.HTTP.StatusCode (StatusCode (..))
5049
51- -- | The result type for Affjax requests.
52- type Affjax a = Aff (AffjaxResponse a )
53-
5450-- | A record that contains all the information to perform an HTTP request.
5551-- | Instead of constructing the record from scratch it is often easier to build
5652-- | one based on `defaultRequest`.
57- type AffjaxRequest =
53+ type RequestOptions =
5854 { method :: Either Method CustomMethod
5955 , url :: URL
6056 , headers :: Array RequestHeader
61- , content :: Maybe Request.Request
57+ , content :: Maybe RequestBody.RequestBody
6258 , username :: Maybe String
6359 , password :: Maybe String
6460 , withCredentials :: Boolean
6561 }
6662
67- -- | A record of the type `AffjaxRequest ` that has all fields set to default
63+ -- | A record of the type `RequestOptions ` that has all fields set to default
6864-- | values. This record can be used as the foundation for constructing
6965-- | custom requests.
7066-- |
@@ -75,7 +71,7 @@ type AffjaxRequest =
7571-- | ```
7672-- |
7773-- | Represents a POST request to the URL `/api/user`.
78- defaultRequest :: AffjaxRequest
74+ defaultRequest :: RequestOptions
7975defaultRequest =
8076 { method: Left GET
8177 , url: " /"
@@ -86,8 +82,8 @@ defaultRequest =
8682 , withCredentials: false
8783 }
8884
89- -- | The type of records that will be received as an Affjax response.
90- type AffjaxResponse a =
85+ -- | The type of records that represents a received HTTP response.
86+ type Response a =
9187 { status :: StatusCode
9288 , statusText :: String
9389 , headers :: Array ResponseHeader
@@ -98,75 +94,75 @@ type AffjaxResponse a =
9894type URL = String
9995
10096-- | Makes a `GET` request to the specified URL.
101- get :: forall a . Response.Response a -> URL -> Affjax a
102- get rt u = affjax rt $ defaultRequest { url = u }
97+ get :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff ( Response a )
98+ get rt u = request rt $ defaultRequest { url = u }
10399
104100-- | Makes a `POST` request to the specified URL, sending data.
105- post :: forall a . Response.Response a -> URL -> Request.Request -> Affjax a
106- post rt u c = affjax rt $ defaultRequest { method = Left POST , url = u, content = Just c }
101+ post :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff ( Response a )
102+ post rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = Just c }
107103
108104-- | Makes a `POST` request to the specified URL with the option to send data.
109- post' :: forall a . Response.Response a -> URL -> Maybe Request.Request -> Affjax a
110- post' rt u c = affjax rt $ defaultRequest { method = Left POST , url = u, content = c }
105+ post' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff ( Response a )
106+ post' rt u c = request rt $ defaultRequest { method = Left POST , url = u, content = c }
111107
112108-- | Makes a `POST` request to the specified URL, sending data and ignoring the
113109-- | response.
114- post_ :: URL -> Request.Request -> Affjax Unit
115- post_ = post Response .ignore
110+ post_ :: URL -> RequestBody.RequestBody -> Aff ( Response Unit )
111+ post_ = post ResponseFormat .ignore
116112
117113-- | Makes a `POST` request to the specified URL with the option to send data,
118114-- | and ignores the response.
119- post_' :: URL -> Maybe Request.Request -> Affjax Unit
120- post_' = post' Response .ignore
115+ post_' :: URL -> Maybe RequestBody.RequestBody -> Aff ( Response Unit )
116+ post_' = post' ResponseFormat .ignore
121117
122118-- | Makes a `PUT` request to the specified URL, sending data.
123- put :: forall a . Response.Response a -> URL -> Request.Request -> Affjax a
124- put rt u c = affjax rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
119+ put :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff ( Response a )
120+ put rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = Just c }
125121
126122-- | Makes a `PUT` request to the specified URL with the option to send data.
127- put' :: forall a . Response.Response a -> URL -> Maybe Request.Request -> Affjax a
128- put' rt u c = affjax rt $ defaultRequest { method = Left PUT , url = u, content = c }
123+ put' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff ( Response a )
124+ put' rt u c = request rt $ defaultRequest { method = Left PUT , url = u, content = c }
129125
130126-- | Makes a `PUT` request to the specified URL, sending data and ignoring the
131127-- | response.
132- put_ :: URL -> Request.Request -> Affjax Unit
133- put_ = put Response .ignore
128+ put_ :: URL -> RequestBody.RequestBody -> Aff ( Response Unit )
129+ put_ = put ResponseFormat .ignore
134130
135131-- | Makes a `PUT` request to the specified URL with the option to send data,
136132-- | and ignores the response.
137- put_' :: URL -> Maybe Request.Request -> Affjax Unit
138- put_' = put' Response .ignore
133+ put_' :: URL -> Maybe RequestBody.RequestBody -> Aff ( Response Unit )
134+ put_' = put' ResponseFormat .ignore
139135
140136-- | Makes a `DELETE` request to the specified URL.
141- delete :: forall a . Response.Response a -> URL -> Affjax a
142- delete rt u = affjax rt $ defaultRequest { method = Left DELETE , url = u }
137+ delete :: forall a . ResponseFormat.ResponseFormat a -> URL -> Aff ( Response a )
138+ delete rt u = request rt $ defaultRequest { method = Left DELETE , url = u }
143139
144140-- | Makes a `DELETE` request to the specified URL and ignores the response.
145- delete_ :: URL -> Affjax Unit
146- delete_ = delete Response .ignore
141+ delete_ :: URL -> Aff ( Response Unit )
142+ delete_ = delete ResponseFormat .ignore
147143
148144-- | Makes a `PATCH` request to the specified URL, sending data.
149- patch :: forall a . Response.Response a -> URL -> Request.Request -> Affjax a
150- patch rt u c = affjax rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
145+ patch :: forall a . ResponseFormat.ResponseFormat a -> URL -> RequestBody.RequestBody -> Aff ( Response a )
146+ patch rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = Just c }
151147
152148-- | Makes a `PATCH` request to the specified URL with the option to send data.
153- patch' :: forall a . Response.Response a -> URL -> Maybe Request.Request -> Affjax a
154- patch' rt u c = affjax rt $ defaultRequest { method = Left PATCH , url = u, content = c }
149+ patch' :: forall a . ResponseFormat.ResponseFormat a -> URL -> Maybe RequestBody.RequestBody -> Aff ( Response a )
150+ patch' rt u c = request rt $ defaultRequest { method = Left PATCH , url = u, content = c }
155151
156152-- | Makes a `PATCH` request to the specified URL, sending data and ignoring the
157153-- | response.
158- patch_ :: URL -> Request.Request -> Affjax Unit
159- patch_ = patch Response .ignore
154+ patch_ :: URL -> RequestBody.RequestBody -> Aff ( Response Unit )
155+ patch_ = patch ResponseFormat .ignore
160156
161157-- | Makes a `PATCH` request to the specified URL with the option to send data,
162158-- | and ignores the response.
163- patch_' :: URL -> Maybe Request.Request -> Affjax Unit
164- patch_' = patch' Response .ignore
159+ patch_' :: URL -> Maybe RequestBody.RequestBody -> Aff ( Response Unit )
160+ patch_' = patch' ResponseFormat .ignore
165161
166162-- | A sequence of retry delays, in milliseconds.
167163type RetryDelayCurve = Int -> Milliseconds
168164
169- -- | Expresses a policy for retrying Affjax requests with backoff.
165+ -- | Expresses a policy for retrying HTTP requests with backoff.
170166type RetryPolicy =
171167 { timeout :: Maybe Milliseconds -- ^ the timeout in milliseconds, optional
172168 , delayCurve :: RetryDelayCurve
@@ -185,7 +181,7 @@ defaultRetryPolicy =
185181type RetryState e a = Either (Either e a ) a
186182
187183-- | 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.
188- retry :: forall a . RetryPolicy -> (AffjaxRequest -> Affjax a ) -> AffjaxRequest -> Affjax a
184+ retry :: forall a . RetryPolicy -> (RequestOptions -> Aff ( Response a )) -> RequestOptions -> Aff ( Response a )
189185retry policy run req = do
190186 -- failureRef is either an exception or a failed request
191187 failureRef <- liftEffect $ Ref .new Nothing
@@ -203,8 +199,8 @@ retry policy run req = do
203199 Just resp -> pure resp
204200 where
205201 retryState
206- :: Either Error (AffjaxResponse a )
207- -> RetryState Error (AffjaxResponse a )
202+ :: Either Error (Response a )
203+ -> RetryState Error (Response a )
208204 retryState (Left exn) = Left $ Left exn
209205 retryState (Right resp) =
210206 case resp.status of
@@ -231,17 +227,17 @@ retry policy run req = do
231227-- | interprets the response body as JSON.
232228-- |
233229-- | ```purescript
234- -- | affjax json (defaultRequest { url = "/resource", method = Left GET })
230+ -- | request json (defaultRequest { url = "/resource", method = Left GET })
235231-- | ```
236232-- |
237- -- | For common cases helper functions can often be used instead of `affjax ` .
233+ -- | For common cases helper functions can often be used instead of `request ` .
238234-- | For instance, the above example is equivalent to the following.
239235-- |
240236-- | ```purescript
241237-- | get json "/resource"
242238-- | ```
243- affjax :: forall a . Response.Response a -> AffjaxRequest -> Affjax a
244- affjax rt req = do
239+ request :: forall a . ResponseFormat.ResponseFormat a -> RequestOptions -> Aff ( Response a )
240+ request rt req = do
245241 res <- AC .fromEffectFnAff $ runFn2 _ajax responseHeader req'
246242 case runExcept (fromResponse' res.response) of
247243 Left err -> throwError $ error $ intercalate " \n " (map renderForeignError err)
@@ -254,26 +250,26 @@ affjax rt req = do
254250 , url: req.url
255251 , headers: (\h -> { field: requestHeaderName h, value: requestHeaderValue h }) <$> headers req.content
256252 , content: toNullable (extractContent <$> req.content)
257- , responseType: Response .toResponseType rt
253+ , responseType: ResponseFormat .toResponseType rt
258254 , username: toNullable req.username
259255 , password: toNullable req.password
260256 , withCredentials: req.withCredentials
261257 }
262258
263- extractContent :: Request.Request -> Foreign
259+ extractContent :: RequestBody.RequestBody -> Foreign
264260 extractContent = case _ of
265- Request .ArrayView f → f unsafeToForeign
266- Request .Blob x → unsafeToForeign x
267- Request .Document x → unsafeToForeign x
268- Request .String x → unsafeToForeign x
269- Request .FormData x → unsafeToForeign x
270- Request .FormURLEncoded x → unsafeToForeign (FormURLEncoded .encode x)
271- Request .Json x → unsafeToForeign (J .stringify x)
272-
273- headers :: Maybe Request.Request -> Array RequestHeader
261+ RequestBody .ArrayView f → f unsafeToForeign
262+ RequestBody .Blob x → unsafeToForeign x
263+ RequestBody .Document x → unsafeToForeign x
264+ RequestBody .String x → unsafeToForeign x
265+ RequestBody .FormData x → unsafeToForeign x
266+ RequestBody .FormURLEncoded x → unsafeToForeign (FormURLEncoded .encode x)
267+ RequestBody .Json x → unsafeToForeign (J .stringify x)
268+
269+ headers :: Maybe RequestBody.RequestBody -> Array RequestHeader
274270 headers reqContent =
275- addHeader (ContentType <$> (Request .toMediaType =<< reqContent)) $
276- addHeader (Accept <$> Response .toMediaType rt)
271+ addHeader (ContentType <$> (RequestBody .toMediaType =<< reqContent)) $
272+ addHeader (Accept <$> ResponseFormat .toMediaType rt)
277273 req.headers
278274
279275 addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
@@ -288,12 +284,12 @@ affjax rt req = do
288284
289285 fromResponse' :: Foreign -> F a
290286 fromResponse' = case rt of
291- Response .ArrayBuffer _ -> unsafeReadTagged " ArrayBuffer"
292- Response .Blob _ -> unsafeReadTagged " Blob"
293- Response .Document _ -> unsafeReadTagged " Document"
294- Response .Json coe -> coe <<< parseJSON <=< unsafeReadTagged " String"
295- Response .String _ -> unsafeReadTagged " String"
296- Response .Ignore coe -> const $ coe (pure unit)
287+ ResponseFormat .ArrayBuffer _ -> unsafeReadTagged " ArrayBuffer"
288+ ResponseFormat .Blob _ -> unsafeReadTagged " Blob"
289+ ResponseFormat .Document _ -> unsafeReadTagged " Document"
290+ ResponseFormat .Json coe -> coe <<< parseJSON <=< unsafeReadTagged " String"
291+ ResponseFormat .String _ -> unsafeReadTagged " String"
292+ ResponseFormat .Ignore coe -> const $ coe (pure unit)
297293
298294type AjaxRequest a =
299295 { method :: String
@@ -311,4 +307,4 @@ foreign import _ajax
311307 . Fn2
312308 (String -> String -> ResponseHeader )
313309 (AjaxRequest a )
314- (AC.EffectFnAff (AffjaxResponse Foreign ))
310+ (AC.EffectFnAff (Response Foreign ))
0 commit comments