1
1
module Network.HTTP.Affjax
2
2
( Ajax ()
3
+ , Affjax ()
3
4
, AffjaxOptions ()
4
5
, AffjaxResponse ()
5
6
, url , method , content , headers , username , password
6
7
, affjax
7
8
, affjax'
9
+ , get
10
+ , post , post_
11
+ , put , put_
12
+ , delete , delete_
8
13
) where
9
14
10
15
import Control.Monad.Aff (Aff (), makeAff )
11
16
import 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 ())
14
20
import Data.Function (Fn4 (), runFn4 )
15
21
import Data.Options (Option (), Options (), IsOption , options , (:=), opt )
16
- import Data.Proxy (Proxy (..))
17
22
import Network.HTTP.Affjax.Request
18
23
import Network.HTTP.Affjax.Response
19
24
import Network.HTTP.Affjax.ResponseType
20
- import Network.HTTP.Method (Method ())
25
+ import Network.HTTP.Method (Method (.. ))
21
26
import Network.HTTP.RequestHeader (RequestHeader ())
22
27
import Network.HTTP.ResponseHeader (ResponseHeader (), responseHeader )
23
28
import Network.HTTP.StatusCode (StatusCode ())
24
29
25
30
-- | The effect type for AJAX requests made with Affjax.
26
31
foreign import data Ajax :: !
27
32
33
+ -- | The type for Affjax requests.
34
+ type Affjax e a = Aff (ajax :: Ajax | e ) (AffjaxResponse a )
35
+
28
36
-- | Options type for Affjax requests.
29
- foreign import data AffjaxOptions :: * -> *
37
+ foreign import data AffjaxOptions :: *
30
38
31
39
-- | The type of records that will be received as an Affjax response.
32
40
type AffjaxResponse a =
@@ -36,49 +44,80 @@ type AffjaxResponse a =
36
44
}
37
45
38
46
-- | Sets the URL for a request.
39
- url :: forall a . Option ( AffjaxOptions a ) String
47
+ url :: Option AffjaxOptions String
40
48
url = opt " url"
41
49
42
50
-- | Sets the HTTP method for a request.
43
- method :: forall a . Option ( AffjaxOptions a ) Method
51
+ method :: Option AffjaxOptions Method
44
52
method = opt " method"
45
53
46
54
-- | 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
48
56
content = opt " content"
49
57
50
58
-- | Sets the headers to send with a request.
51
- headers :: forall a . Option ( AffjaxOptions a ) [RequestHeader ]
59
+ headers :: Option AffjaxOptions [RequestHeader ]
52
60
headers = opt " headers"
53
61
54
62
-- | Sets the HTTP auth username to send with a request.
55
- username :: forall a . Option ( AffjaxOptions a ) String
63
+ username :: Option AffjaxOptions String
56
64
username = opt " username"
57
65
58
66
-- | Sets the HTTP auth password to send with a request.
59
- password :: forall a . Option ( AffjaxOptions a ) String
67
+ password :: Option AffjaxOptions String
60
68
password = opt " password"
61
69
62
70
-- | Sets the expected response type for a request. This is not exposed outside
63
71
-- | of the module as the `ResponseType` is set based on the `Responsable`
64
72
-- | instance for the expected result content type.
65
- responseType = opt " responseType" :: forall a . Option ( AffjaxOptions a ) ResponseType
73
+ responseType = opt " responseType" :: Option AffjaxOptions ResponseType
66
74
67
75
-- | 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
72
80
73
81
-- | 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
82
121
83
122
foreign import unsafeAjax
84
123
" " "
@@ -112,8 +151,8 @@ foreign import unsafeAjax
112
151
xhr.send(options.content);
113
152
};
114
153
}
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