@@ -1,6 +1,31 @@
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.
1Elm.HTTP = function() {
8Elm.HTTP = function() {
2 var JS = Elm.JavaScript;
9 var JS = Elm.JavaScript;
3 var toElmString = Elm.JavaScript.castJSStringToString;
10 var toElmString = Elm.JavaScript.castJSStringToString;
12+ /*[Creating Requests]*/
14+ /** get :: String -> Request String
15+ Create a GET request to the given url.
17+ function get(url) { return request("GET")(url)(null)(["Nil"]); }
19+ /** post :: String -> String -> Request String
20+ Create a POST request to the given url, carrying the given data.
22+ function post(url) { return function(data) {
23+ return request("POST")(url)(data)(["Nil"]); }; }
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.
4 function request(verb) { return function(url) { return function(data) {
29 function request(verb) { return function(url) { return function(data) {
5 return function(headers) {
30 return function(headers) {
6 return {0 : "Request",
31 return {0 : "Request",
@@ -10,12 +35,6 @@ Elm.HTTP = function() {
10 data : data === null ? null : JS.castStringToJSString(data),
35 data : data === null ? null : JS.castStringToJSString(data),
11 headers : headers }; }; }; };
36 headers : headers }; }; }; };
13− /** get :: String -> Request String
14− Create a GET request to the given url.
16− function get(url) { return request("GET")(url)(null)(["Nil"]); }
17− function post(url) { return function(data) {
18− return request("POST")(url)(data)(["Nil"]); }; }
20 function registerReq(queue,responses) { return function(req) {
39 function registerReq(queue,responses) { return function(req) {
21 if (req.url !== "") { sendReq(queue,responses,req); }
40 if (req.url !== "") { sendReq(queue,responses,req); }
@@ -58,6 +77,19 @@ Elm.HTTP = function() {
58 return null;
77 return null;
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.
87+ /*[Sending Requests]*/
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.
61 function send(requests) {
93 function send(requests) {
62 var responses = Elm.Signal.constant(["Waiting"]);
94 var responses = Elm.Signal.constant(["Waiting"]);
63 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
95 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
@@ -65,6 +97,11 @@ Elm.HTTP = function() {
65 return Elm.Signal.lift2(f)(responses)(sender);
97 return Elm.Signal.lift2(f)(responses)(sender);
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.
68 return {get : get,
105 return {get : get,
69 post : post,
106 post : post,
70 request : request,
107 request : request,