11module Network.HTTP.Affjax
22 ( Ajax ()
3+ , Affjax ()
34 , AffjaxOptions ()
45 , AffjaxResponse ()
56 , url , method , content , headers , username , password
67 , affjax
78 , affjax'
9+ , get
10+ , post , post_
11+ , put , put_
12+ , delete , delete_
813 ) where
914
1015import Control.Monad.Aff (Aff (), makeAff )
1116import Control.Monad.Eff (Eff ())
12- import Control.Monad.Eff.Exception (Error ())
13- import Data.Foreign (Foreign (..))
17+ import Control.Monad.Eff.Exception (Error (), error )
18+ import Data.Either (Either (..))
19+ import Data.Foreign (Foreign (..), F ())
1420import Data.Function (Fn4 (), runFn4 )
1521import Data.Options (Option (), Options (), IsOption , options , (:=), opt )
16- import Data.Proxy (Proxy (..))
1722import Network.HTTP.Affjax.Request
1823import Network.HTTP.Affjax.Response
1924import Network.HTTP.Affjax.ResponseType
20- import Network.HTTP.Method (Method ())
25+ import Network.HTTP.Method (Method (.. ))
2126import Network.HTTP.RequestHeader (RequestHeader ())
2227import Network.HTTP.ResponseHeader (ResponseHeader (), responseHeader )
2328import Network.HTTP.StatusCode (StatusCode ())
2429
2530-- | The effect type for AJAX requests made with Affjax.
2631foreign import data Ajax :: !
2732
33+ -- | The type for Affjax requests.
34+ type Affjax e a = Aff (ajax :: Ajax | e ) (AffjaxResponse a )
35+
2836-- | Options type for Affjax requests.
29- foreign import data AffjaxOptions :: * -> *
37+ foreign import data AffjaxOptions :: *
3038
3139-- | The type of records that will be received as an Affjax response.
3240type AffjaxResponse a =
@@ -36,49 +44,80 @@ type AffjaxResponse a =
3644 }
3745
3846-- | Sets the URL for a request.
39- url :: forall a . Option ( AffjaxOptions a ) String
47+ url :: Option AffjaxOptions String
4048url = opt " url"
4149
4250-- | Sets the HTTP method for a request.
43- method :: forall a . Option ( AffjaxOptions a ) Method
51+ method :: Option AffjaxOptions Method
4452method = opt " method"
4553
4654-- | Sets the content to send in a request.
47- content :: forall a . ( Requestable a , IsOption a ) => Option ( AffjaxOptions a ) a
55+ content :: Option AffjaxOptions RequestContent
4856content = opt " content"
4957
5058-- | Sets the headers to send with a request.
51- headers :: forall a . Option ( AffjaxOptions a ) [RequestHeader ]
59+ headers :: Option AffjaxOptions [RequestHeader ]
5260headers = opt " headers"
5361
5462-- | Sets the HTTP auth username to send with a request.
55- username :: forall a . Option ( AffjaxOptions a ) String
63+ username :: Option AffjaxOptions String
5664username = opt " username"
5765
5866-- | Sets the HTTP auth password to send with a request.
59- password :: forall a . Option ( AffjaxOptions a ) String
67+ password :: Option AffjaxOptions String
6068password = opt " password"
6169
6270-- | Sets the expected response type for a request. This is not exposed outside
6371-- | of the module as the `ResponseType` is set based on the `Responsable`
6472-- | instance for the expected result content type.
65- responseType = opt " responseType" :: forall a . Option ( AffjaxOptions a ) ResponseType
73+ responseType = opt " responseType" :: Option AffjaxOptions ResponseType
6674
6775-- | 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'
76+ affjax :: forall e a . Responsable a - >
77+ Options AffjaxOptions ->
78+ Affjax e a
79+ affjax r = makeAff <<< affjax' r
7280
7381-- | 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+ affjax' :: forall e a . Responsable a ->
83+ Options AffjaxOptions ->
84+ (Error -> Eff (ajax :: Ajax | e ) Unit ) ->
85+ (AffjaxResponse a -> Eff (ajax :: Ajax | e ) Unit ) ->
86+ Eff (ajax :: Ajax | e ) Unit
87+ affjax' (Responsable read ty) opts eb cb =
88+ runFn4 unsafeAjax responseHeader (options $ opts <> responseType := ty) eb cb'
89+ where
90+ cb' :: AffjaxResponse Foreign -> Eff (ajax :: Ajax | e ) Unit
91+ cb' res = case res { response = _ } <$> read res .response of
92+ Left err -> eb $ error (show err )
93+ Right res' -> cb res'
94+
95+ get :: forall e a . Responsable a -> String -> Affjax e a
96+ get r addr = affjax r $ method := GET
97+ <> url := addr
98+
99+ post :: forall e a . Responsable a -> String -> RequestContent -> Affjax e a
100+ post r u c = affjax r $ method := POST
101+ <> url := u
102+ <> content := c
103+
104+ post_ :: forall e . String -> RequestContent -> Affjax e Unit
105+ post_ = post rUnit
106+
107+ put :: forall e a . Responsable a -> String -> RequestContent -> Affjax e a
108+ put r u c = affjax r $ method := PUT
109+ <> url := u
110+ <> content := c
111+
112+ put_ :: forall e . String -> RequestContent -> Affjax e Unit
113+ put_ = put rUnit
114+
115+ delete :: forall e a . Responsable a -> String -> Affjax e a
116+ delete r u = affjax r $ method := DELETE
117+ <> url := u
118+
119+ delete_ :: forall e . String -> Affjax e Unit
120+ delete_ = delete rUnit
82121
83122foreign import unsafeAjax
84123 " " "
@@ -112,8 +151,8 @@ foreign import unsafeAjax
112151 xhr.send(options.content);
113152 };
114153 }
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 )
154+ " " " :: forall e a . Fn4 (String -> String -> ResponseHeader )
155+ Foreign
156+ (Error -> Eff (ajax :: Ajax | e ) Unit )
157+ (AffjaxResponse Foreign -> Eff (ajax :: Ajax | e ) Unit )
158+ (Eff (ajax :: Ajax | e ) Unit )
0 commit comments