@@ -1,6 +1,31 @@
1+
2+/*! HTTP
3+A library for asynchronous HTTP requests (AJAX). See the
4+[WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
5+you have very strict latency requirements.
6+!*/
7+
18Elm.HTTP = function() {
29 var JS = Elm.JavaScript;
310 var toElmString = Elm.JavaScript.castJSStringToString;
11+
12+ /*[Creating Requests]*/
13+
14+ /** get :: String -> Request String
15+ Create a GET request to the given url.
16+ **/
17+ function get(url) { return request("GET")(url)(null)(["Nil"]); }
18+
19+ /** post :: String -> String -> Request String
20+ Create a POST request to the given url, carrying the given data.
21+ **/
22+ function post(url) { return function(data) {
23+ return request("POST")(url)(data)(["Nil"]); }; }
24+
25+ /** request :: String -> String -> String -> [(String,String)] -> Request String
26+ Create a customized request. Arguments are request type (get, post, put,
27+ delete, etc.), target url, data, and a list of additional headers.
28+ **/
429 function request(verb) { return function(url) { return function(data) {
530 return function(headers) {
631 return {0 : "Request",
@@ -10,12 +35,6 @@ Elm.HTTP = function() {
1035 data : data === null ? null : JS.castStringToJSString(data),
1136 headers : headers }; }; }; };
1237 }
13− /** get :: String -> Request String
14− Create a GET request to the given url.
15− **/
16− function get(url) { return request("GET")(url)(null)(["Nil"]); }
17− function post(url) { return function(data) {
18− return request("POST")(url)(data)(["Nil"]); }; }
1938
2039 function registerReq(queue,responses) { return function(req) {
2140 if (req.url !== "") { sendReq(queue,responses,req); }
@@ -58,6 +77,19 @@ Elm.HTTP = function() {
5877 return null;
5978 }
6079
80+ /*[Responses]*/
81+
82+ /** data Response a = Waiting | Success a | Failure Int String
83+ The datatype for responses. Success contains only the returned message.
84+ Failures contain both an error code and an error message.
85+ **/
86+
87+ /*[Sending Requests]*/
88+
89+ /** send :: Signal (Request a) -> Signal (Response String)
90+ Performs an HTTP request with the given requests. Produces a signal
91+ that carries the responses.
92+ **/
6193 function send(requests) {
6294 var responses = Elm.Signal.constant(["Waiting"]);
6395 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
@@ -65,6 +97,11 @@ Elm.HTTP = function() {
6597 return Elm.Signal.lift2(f)(responses)(sender);
6698 }
6799
100+ /** sendGet :: Signal String -> Signal (Response String)
101+ Performs an HTTP GET request with the given urls. Produces a signal
102+ that carries the responses.
103+ **/
104+
68105 return {get : get,
69106 post : post,
70107 request : request,