@@ -2,11 +2,14 @@
3
3
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.
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.
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).
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
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
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
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
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
65+-- this proxy targets everything
66+testApi' :: Proxy TestApi'
58-- * Server-side handler
69-- * Server-side handler
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
80+server' :: TVar Counter -> Server TestApi'
81+server counter = server counter
68 :<|> serveDirectory www -- serve static files
82 :<|> serveDirectory www -- serve static files
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
73−runServer var port = run port (serve testApi $ server var)
87+runServer var port = run port (serve testApi' $ server' var)
75−-- * Generating the JQuery code
77−incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
79−writeJS :: FilePath -> [AjaxReq] -> IO ()
80−writeJS fp functions = writeFile fp $
81− concatMap generateJS functions
83main :: IO ()
89main :: IO ()
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 ]
89 -- setup a shared counter
94 -- setup a shared counter
90 cnt <- newCounter
95 cnt <- newCounter