git.iko.soy/typeable/servant
on master help sign in
first shot at generating function names automatically
commit99215cd6548e83880bb288061971afe60ac38e88 authorAlp Mestanogullari <alpmestan@gmail.com> dateMon 1 Dec 2014, 17:36 (12 years ago) parent3e76058
4 files changed+26−30
examples/counter.hs+2−6 examples/www/api.js+5−6 examples/www/index.html+3−3 src/Servant/JQuery/Internal.hs+16−15
examples/counter.hs+2−6
@@ -62,10 +62,8 @@ runServer :: TVar Counter -- ^ shared variable for the counter
6262runServer var port = run port (serve testApi $ server var)
6363
6464-- * Generating the JQuery code
65−incCounterNamed :: FunctionName -> AjaxReq
66−currentValueNamed :: FunctionName -> AjaxReq
6765
68−incCounterNamed :<|> currentValueNamed :<|> _ = jquery testApi
66+incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
6967
7068writeJS :: FilePath -> [AjaxReq] -> IO ()
7169writeJS fp functions = writeFile fp $
@@ -75,9 +73,7 @@ main :: IO ()
7573main = do
7674 -- write the JS code to www/api.js at startup
7775 writeJS (www </> "api.js")
78− [ incCounterNamed "increaseCounter"
79− , currentValueNamed "getCurrentValue"
80− ]
76+ [ incCounterJS, currentValueJS ]
8177
8278 -- setup a shared counter
8379 cnt <- newCounter
examples/www/api.js+5−6
@@ -1,21 +1,20 @@
11
2−function increaseCounter(onSuccess, onError)
2+function postcounter(onSuccess, onError)
33{
44 $.ajax(
55 { url: '/counter'
6− , success: onSuccess
6+ , success: onSuccess
77 , error: onError
88 , type: 'POST'
99 });
1010}
11−
12−function getCurrentValue(onSuccess, onError)
11+
12+function getcounter(onSuccess, onError)
1313{
1414 $.ajax(
1515 { url: '/counter'
16− , success: onSuccess
16+ , success: onSuccess
1717 , error: onError
1818 , type: 'GET'
1919 });
2020}
21−
examples/www/index.html+3−3
@@ -17,11 +17,11 @@ or <a href="/doc">view the docs</a>
1717<script type="text/javascript">
1818$(document).ready(function() {
1919 // we get the current value stored by the server when the page is loaded
20− getCurrentValue(updateCounter, alert);
20+ getcounter(updateCounter, alert);
2121
2222 // we update the value every 1sec, in the same way
2323 window.setInterval(function() {
24− getCurrentValue(updateCounter, alert);
24+ getcounter(updateCounter, alert);
2525 }, 1000);
2626});
2727
@@ -33,7 +33,7 @@ function updateCounter(response)
3333// when the button is clicked, ask the server to increase
3434// the value by one
3535$('#inc').click(function() {
36− increaseCounter(updateCounter, alert);
36+ postcounter(updateCounter, alert);
3737});
3838</script>
3939</body>
src/Servant/JQuery/Internal.hs+16−15
@@ -7,6 +7,7 @@
77module Servant.JQuery.Internal where
88
99import Control.Lens
10+import Data.Monoid
1011import Data.Proxy
1112import GHC.TypeLits
1213import Servant.API
@@ -120,31 +121,31 @@ instance (KnownSymbol sym, HasJQ sublayout)
120121 where str = symbolVal (Proxy :: Proxy sym)
121122
122123instance HasJQ Delete where
123− type JQ Delete = FunctionName -> AjaxReq
124+ type JQ Delete = AjaxReq
124125
125− jqueryFor Proxy req fName =
126− req & funcName .~ fName
126+ jqueryFor Proxy req =
127+ req & funcName %~ ("delete" <>)
127128 & reqMethod .~ "DELETE"
128129
129130instance HasJQ (Get a) where
130− type JQ (Get a) = FunctionName -> AjaxReq
131+ type JQ (Get a) = AjaxReq
131132
132− jqueryFor Proxy req fName =
133− req & funcName .~ fName
133+ jqueryFor Proxy req =
134+ req & funcName %~ ("get" <>)
134135 & reqMethod .~ "GET"
135136
136137instance HasJQ (Post a) where
137− type JQ (Post a) = FunctionName -> AjaxReq
138+ type JQ (Post a) = AjaxReq
138139
139− jqueryFor Proxy req fName =
140− req & funcName .~ fName
140+ jqueryFor Proxy req =
141+ req & funcName %~ ("post" <>)
141142 & reqMethod .~ "POST"
142143
143144instance HasJQ (Put a) where
144− type JQ (Put a) = FunctionName -> AjaxReq
145+ type JQ (Put a) = AjaxReq
145146
146− jqueryFor Proxy req fName =
147− req & funcName .~ fName
147+ jqueryFor Proxy req =
148+ req & funcName %~ ("put" <>)
148149 & reqMethod .~ "PUT"
149150
150151instance (KnownSymbol sym, HasJQ sublayout)
@@ -179,11 +180,10 @@ instance (KnownSymbol sym, HasJQ sublayout)
179180 where str = symbolVal (Proxy :: Proxy sym)
180181
181182instance HasJQ Raw where
182− type JQ Raw = Method -> FunctionName -> AjaxReq
183+ type JQ Raw = Method -> AjaxReq
183184
184− jqueryFor Proxy req method fName =
185+ jqueryFor Proxy req method =
185186 req & reqMethod .~ method
186− & funcName .~ fName
187187
188188instance HasJQ sublayout => HasJQ (ReqBody a :> sublayout) where
189189 type JQ (ReqBody a :> sublayout) = JQ sublayout
@@ -199,5 +199,6 @@ instance (KnownSymbol path, HasJQ sublayout)
199199 jqueryFor Proxy req =
200200 jqueryFor (Proxy :: Proxy sublayout) $
201201 req & reqUrl.path <>~ [Static str]
202+ & funcName %~ (str <>)
202203
203204 where str = symbolVal (Proxy :: Proxy path)