git.iko.soy/typeable/servant
on master help sign in
Merge pull request #260 from freezeboy/clean-js
6c50bebJulian Arni
servant-js/README.md+22−17
@@ -2,11 +2,14 @@
2
2
3![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)
3![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)
4
4
5−This library lets you derive automatically (JQuery based) Javascript functions that let you query each endpoint of a *servant* webservice.
5+This library lets you derive automatically Javascript functions that let you query each endpoint of a *servant* webservice.
6+
7+It contains a powerful system allowing you to generate functions for several frameworks (Angular, AXios, JQuery) as well as
8+vanilla (framework-free) javascript code.
6
9
7## Example
10## Example
8
11
9−Read more about the following example [here](https://github.com/haskell-servant/servant/tree/master/servant-jquery/tree/master/examples#examples).
12+Read more about the following example [here](https://github.com/haskell-servant/servant/tree/master/servant-js/tree/master/examples#examples).
10
13
11``` haskell
14``` haskell
12{-# LANGUAGE DataKinds #-}
15{-# LANGUAGE DataKinds #-}
@@ -21,7 +24,7 @@ import Data.Proxy
21import GHC.Generics
24import GHC.Generics
22import Network.Wai.Handler.Warp (run)
25import Network.Wai.Handler.Warp (run)
23import Servant
26import Servant
24−import Servant.JQuery
27+import Servant.JS
25import System.FilePath
28import System.FilePath
26
29
27-- * A simple Counter data type
30-- * A simple Counter data type
@@ -48,13 +51,21 @@ currentValue :: MonadIO m => TVar Counter -> m Counter
48currentValue counter = liftIO $ readTVarIO counter
51currentValue counter = liftIO $ readTVarIO counter
49
52
50-- * Our API type
53-- * Our API type
51−type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
54+type TestApi = "counter" :> Post '[JSON] Counter -- endpoint for increasing the counter
52− :<|> "counter" :> Get Counter -- endpoint to get the current value
55+ :<|> "counter" :> Get '[JSON] Counter -- endpoint to get the current value
56+
57+type TestApi' = TestApi -- The API we want a JS handler for
53− :<|> Raw -- used for serving static files
58+ :<|> Raw -- used for serving static files
54
59
60+-- this proxy only targets the proper endpoints of our API,
61+-- not the static file serving bit
55testApi :: Proxy TestApi
62testApi :: Proxy TestApi
56testApi = Proxy
63testApi = Proxy
57
64
65+-- this proxy targets everything
66+testApi' :: Proxy TestApi'
67+testApi' = Proxy
68+
58-- * Server-side handler
69-- * Server-side handler
59
70
60-- where our static files reside
71-- where our static files reside
@@ -65,26 +76,20 @@ www = "examples/www"
65server :: TVar Counter -> Server TestApi
76server :: TVar Counter -> Server TestApi
66server counter = counterPlusOne counter -- (+1) on the TVar
77server counter = counterPlusOne counter -- (+1) on the TVar
67 :<|> currentValue counter -- read the TVar
78 :<|> currentValue counter -- read the TVar
79+
80+server' :: TVar Counter -> Server TestApi'
81+server counter = server counter
68 :<|> serveDirectory www -- serve static files
82 :<|> serveDirectory www -- serve static files
69
83
70runServer :: TVar Counter -- ^ shared variable for the counter
84runServer :: TVar Counter -- ^ shared variable for the counter
71 -> Int -- ^ port the server should listen on
85 -> Int -- ^ port the server should listen on
72 -> IO ()
86 -> IO ()
73−runServer var port = run port (serve testApi $ server var)
87+runServer var port = run port (serve testApi' $ server' var)
74−
75−-- * Generating the JQuery code
76−
77−incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
78−
79−writeJS :: FilePath -> [AjaxReq] -> IO ()
80−writeJS fp functions = writeFile fp $
81− concatMap generateJS functions
82
88
83main :: IO ()
89main :: IO ()
84main = do
90main = do
85 -- write the JS code to www/api.js at startup
91 -- write the JS code to www/api.js at startup
86− writeJS (www </> "api.js")
92+ writeJSForAPI testApi jquery (www </> "api.js")
87− [ incCounterJS, currentValueJS ]
88
93
89 -- setup a shared counter
94 -- setup a shared counter
90 cnt <- newCounter
95 cnt <- newCounter
servant-js/docs.sh+3−3
@@ -1,4 +1,4 @@
1−SERVANT_DIR=/tmp/servant-jquery-gh-pages
1+SERVANT_DIR=/tmp/servant-js-gh-pages
2
2
3# Make a temporary clone
3# Make a temporary clone
4
4
@@ -10,7 +10,7 @@ cd $SERVANT_DIR
10
10
11# Make sure to pull the latest
11# Make sure to pull the latest
12
12
13−git remote add haskell-servant git@github.com:haskell-servant/servant-jquery.git
13+git remote add haskell-servant git@github.com:haskell-servant/servant-js.git
14
14
15git fetch haskell-servant
15git fetch haskell-servant
16
16
@@ -36,7 +36,7 @@ cd $SERVANT_DIR
36
36
37rm *
37rm *
38rm -rf build
38rm -rf build
39−mv doc/html/servant-jquery/* .
39+mv doc/html/servant-js/* .
40rm -r doc/
40rm -r doc/
41
41
42# Add everything
42# Add everything
servant-js/servant-js.cabal+4−4
@@ -2,15 +2,15 @@ name: servant-js
2version: 0.5
2version: 0.5
3synopsis: Automatically derive javascript functions to query servant webservices.
3synopsis: Automatically derive javascript functions to query servant webservices.
4description:
4description:
5− Automatically derive jquery-based javascript functions to query servant webservices.
5+ Automatically derive javascript functions to query servant webservices.
6 .
6 .
7− Supports deriving functions using vanilla javascript AJAX requests, Angular or JQuery.
7+ Supports deriving functions using vanilla javascript AJAX requests, Angulari, Axios or JQuery.
8 .
8 .
9− You can find an example <https://github.com/haskell-servant/servant/blob/master/servant-jquery/examples/counter.hs here>
9+ You can find an example <https://github.com/haskell-servant/servant/blob/master/servant-js/examples/counter.hs here>
10 which serves the generated javascript to a webpage that allows you to trigger
10 which serves the generated javascript to a webpage that allows you to trigger
11 webservice calls.
11 webservice calls.
12 .
12 .
13− <https://github.com/haskell-servant/servant/blob/master/servant-jquery/CHANGELOG.md CHANGELOG>
13+ <https://github.com/haskell-servant/servant/blob/master/servant-js/CHANGELOG.md CHANGELOG>
14license: BSD3
14license: BSD3
15license-file: LICENSE
15license-file: LICENSE
16author: Alp Mestanogullari
16author: Alp Mestanogullari