@@ -8,30 +8,72 @@
8#ifdef USE_TEMPLATE_HASKELL
8#ifdef USE_TEMPLATE_HASKELL
9{-# LANGUAGE TemplateHaskell #-}
9{-# LANGUAGE TemplateHaskell #-}
12+-- | A module for performing asynchronous HTTP calls from JavaScript
14+-- <https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest XMLHttpRequest>
15+-- API (essentially AJAX). Despite the name, there is nothing whatsoever specific to XML.
17+-- The API has two components:
19+-- * convenient functions for common usecases like GET and POST
20+-- requests to APIs using JSON.
22+-- * a flexible set of functions for creating and executing arbitrary
23+-- requests and handling responses.
11module Reflex.Dom.Xhr
25module Reflex.Dom.Xhr
12− ( XMLHttpRequest
26+ ( -- * Common Patterns
14− , XhrRequestConfig (..)
28+ -- | Functions that conveniently expose common uses like GET and
15− , XhrResponse (..)
29+ -- POST to JSON APIs.
16− , XhrResponseBody (..)
17− , XhrResponseHeaders (..)
18− , XhrResponseType (..)
24− , getAndDecode
30+ getAndDecode
27− , newXMLHttpRequestWithError
28− , performMkRequestAsync
29− , performMkRequestsAsync
30− , performRequestAsync
31− , performRequestAsyncWithError
32− , performRequestsAsync
33− , performRequestsAsyncWithError
34 , postJson
32 , postJson
37+ -- * General Request API
39+ -- | This is the most general flow for sending XHR requests:
41+ -- 1. Create an 'Event' stream of 'XhrRequest' records (ie
42+ -- @Event t (XhrRequest a)@). The records configure the request,
43+ -- and the 'Event' controls when the request or requests are
46+ -- 2. Plug the @Event t (XhrRequest a)@ into one of the functions
47+ -- for performing requests like 'performRequestAsync'.
49+ -- 3. Consume the resulting stream of 'XhrResponse' events,
50+ -- parsing the body of the response however appropriate. A really
51+ -- common pattern is turning the 'Event' into a 'Dynamic' with
52+ -- 'holdDyn' or a related function.
54+ -- Here is an example of calling a search API whenever the user
55+ -- types in a text input field and printing the result on the page:
58+ -- url query = "http:\/\/example.com\/search?query=" \<> query
60+ -- search queries = do
61+ -- responses \<- performRequestAsync $ toRequest \<$> queries
62+ -- return $ view xhrResponse_responseText \<$> responses
63+ -- where toRequest query = XhrRequest \"GET" (url query) def
65+ -- main = mainWidget $ do
66+ -- input \<- textInput def
67+ -- let queries = updated $ input ^. textInput_value
68+ -- results \<- search queries
69+ -- asText \<- holdDyn "No results." $ pack . show \<$> results
75+ , XhrRequestConfig (..)
35 , xhrRequest
77 , xhrRequest
36 , xhrRequestConfig_headers
78 , xhrRequestConfig_headers
37 , xhrRequestConfig_password
79 , xhrRequestConfig_password
@@ -43,12 +85,48 @@ module Reflex.Dom.Xhr
43 , xhrRequest_config
85 , xhrRequest_config
44 , xhrRequest_method
86 , xhrRequest_method
45 , xhrRequest_url
87 , xhrRequest_url
46− , xhrResponse_body
88+
89+ -- ** Performing Requests
90+ , performMkRequestAsync
91+ , performMkRequestsAsync
92+ , performRequestAsync
93+ , performRequestAsyncWithError
94+ , performRequestsAsync
95+ , performRequestsAsyncWithError
99+ , XhrResponseBody (..)
100+ , XhrResponseHeaders (..)
101+ , XhrResponseType (..)
47 , xhrResponse_response
103 , xhrResponse_response
48 , xhrResponse_responseText
104 , xhrResponse_responseText
49 , xhrResponse_status
105 , xhrResponse_status
50 , xhrResponse_statusText
106 , xhrResponse_statusText
51 , xhrResponse_headers
107 , xhrResponse_headers
113+ -- ** Error Handling
117+ -- * JavaScript XMLHttpRequest Objects
119+ -- | 'XMLHttpRequest' is the type of JavaScript's underlying runtime
120+ -- objects that represent XHR requests.
122+ -- Chances are you shouldn't need these in day-to-day code.
127+ , newXMLHttpRequestWithError
52 , xmlHttpRequestGetReadyState
130 , xmlHttpRequestGetReadyState
53 , xmlHttpRequestGetResponseText
131 , xmlHttpRequestGetResponseText
54 , xmlHttpRequestGetStatus
132 , xmlHttpRequestGetStatus