|
| 1 | +module Network.HTTP.Affjax |
| 2 | + ( Ajax() |
| 3 | + , AffjaxOptions() |
| 4 | + , AffjaxResponse() |
| 5 | + , url, method, content, headers, username, password |
| 6 | + , affjax |
| 7 | + , affjax' |
| 8 | + ) where |
| 9 | + |
| 10 | +import Control.Monad.Aff (Aff(), makeAff) |
| 11 | +import Control.Monad.Eff (Eff()) |
| 12 | +import Control.Monad.Eff.Exception (Error()) |
| 13 | +import Data.Foreign (Foreign(..)) |
| 14 | +import Data.Function (Fn4(), runFn4) |
| 15 | +import Data.Options (Option(), Options(), IsOption, options, (:=), opt) |
| 16 | +import Data.Proxy (Proxy(..)) |
| 17 | +import Network.HTTP.Affjax.Request |
| 18 | +import Network.HTTP.Affjax.Response |
| 19 | +import Network.HTTP.Affjax.ResponseType |
| 20 | +import Network.HTTP.Method (Method()) |
| 21 | +import Network.HTTP.RequestHeader (RequestHeader()) |
| 22 | +import Network.HTTP.ResponseHeader (ResponseHeader(), responseHeader) |
| 23 | +import Network.HTTP.StatusCode (StatusCode()) |
| 24 | + |
| 25 | +-- | The effect type for AJAX requests made with Affjax. |
| 26 | +foreign import data Ajax :: ! |
| 27 | + |
| 28 | +-- | Options type for Affjax requests. |
| 29 | +foreign import data AffjaxOptions :: * -> * |
| 30 | + |
| 31 | +-- | The type of records that will be received as an Affjax response. |
| 32 | +type AffjaxResponse a = |
| 33 | + { status :: StatusCode |
| 34 | + , headers :: [ResponseHeader] |
| 35 | + , response :: a |
| 36 | + } |
| 37 | + |
| 38 | +-- | Sets the URL for a request. |
| 39 | +url :: forall a. Option (AffjaxOptions a) String |
| 40 | +url = opt "url" |
| 41 | + |
| 42 | +-- | Sets the HTTP method for a request. |
| 43 | +method :: forall a. Option (AffjaxOptions a) Method |
| 44 | +method = opt "method" |
| 45 | + |
| 46 | +-- | Sets the content to send in a request. |
| 47 | +content :: forall a. (Requestable a, IsOption a) => Option (AffjaxOptions a) a |
| 48 | +content = opt "content" |
| 49 | + |
| 50 | +-- | Sets the headers to send with a request. |
| 51 | +headers :: forall a. Option (AffjaxOptions a) [RequestHeader] |
| 52 | +headers = opt "headers" |
| 53 | + |
| 54 | +-- | Sets the HTTP auth username to send with a request. |
| 55 | +username :: forall a. Option (AffjaxOptions a) String |
| 56 | +username = opt "username" |
| 57 | + |
| 58 | +-- | Sets the HTTP auth password to send with a request. |
| 59 | +password :: forall a. Option (AffjaxOptions a) String |
| 60 | +password = opt "password" |
| 61 | + |
| 62 | +-- | Sets the expected response type for a request. This is not exposed outside |
| 63 | +-- | of the module as the `ResponseType` is set based on the `Responsable` |
| 64 | +-- | instance for the expected result content type. |
| 65 | +responseType = opt "responseType" :: forall a. Option (AffjaxOptions a) ResponseType |
| 66 | + |
| 67 | +-- | Runs a request. |
| 68 | +affjax :: forall e a b. (Requestable a, Responsable b) => |
| 69 | + Options (AffjaxOptions a) -> |
| 70 | + Aff (ajax :: Ajax | e) (AffjaxResponse b) |
| 71 | +affjax = makeAff <<< affjax' |
| 72 | + |
| 73 | +-- | Runs a request directly in Eff. |
| 74 | +affjax' :: forall e a b. (Requestable a, Responsable b) => |
| 75 | + Options (AffjaxOptions a) -> |
| 76 | + (Error -> Eff (ajax :: Ajax | e) Unit) -> |
| 77 | + (AffjaxResponse b -> Eff (ajax :: Ajax | e) Unit) -> |
| 78 | + Eff (ajax :: Ajax | e) Unit |
| 79 | +affjax' opts eb cb = |
| 80 | + let opts' = opts <> responseType := toResponseType (Proxy :: Proxy b) |
| 81 | + in runFn4 unsafeAjax responseHeader (options opts') eb cb |
| 82 | + |
| 83 | +foreign import unsafeAjax |
| 84 | + """ |
| 85 | + function unsafeAjax (mkHeader, options, errback, callback) { |
| 86 | + return function () { |
| 87 | + var xhr = new XMLHttpRequest(); |
| 88 | + xhr.open(options.method || "GET", options.url || "/", true, options.username, options.password); |
| 89 | + if (options.headers) { |
| 90 | + for (var i = 0, header; header = options.headers[i]; i++) { |
| 91 | + xhr.setRequestHeader(header.field, header.value); |
| 92 | + } |
| 93 | + } |
| 94 | + xhr.onerror = function (err) { |
| 95 | + errback(err)(); |
| 96 | + }; |
| 97 | + xhr.onload = function () { |
| 98 | + callback({ |
| 99 | + status: xhr.status, |
| 100 | + headers: xhr.getAllResponseHeaders().split("\n") |
| 101 | + .filter(function (header) { |
| 102 | + return header.length > 0; |
| 103 | + }) |
| 104 | + .map(function (header) { |
| 105 | + var i = header.indexOf(":"); |
| 106 | + return mkHeader(header.substring(0, i))(header.substring(i + 2)); |
| 107 | + }), |
| 108 | + response: xhr.response |
| 109 | + })(); |
| 110 | + }; |
| 111 | + if (options.responseType) xhr.responseType = options.responseType; |
| 112 | + xhr.send(options.content); |
| 113 | + }; |
| 114 | + } |
| 115 | + """ :: forall e a b. Fn4 (String -> String -> ResponseHeader) |
| 116 | + Foreign |
| 117 | + (Error -> Eff (ajax :: Ajax | e) Unit) |
| 118 | + (AffjaxResponse b -> Eff (ajax :: Ajax | e) Unit) |
| 119 | + (Eff (ajax :: Ajax | e) Unit) |
0 commit comments