@@ -2,11 +2,14 @@
22
33
44
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.
69
710## Example
811
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).
1013
1114``` haskell
1215{-# LANGUAGE DataKinds #-}
@@ -21,7 +24,7 @@ import Data.Proxy
2124import GHC.Generics
2225import Network.Wai.Handler.Warp (run)
2326import Servant
24−import Servant.JQuery
27+import Servant.JS
2528import System.FilePath
2629
2730-- * A simple Counter data type
@@ -48,13 +51,21 @@ currentValue :: MonadIO m => TVar Counter -> m Counter
4851currentValue counter = liftIO $ readTVarIO counter
4952
5053-- * Our API type
51−type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
52− :<|> "counter" :> Get Counter -- endpoint to get the current value
53− :<|> Raw -- used for serving static files
54+type TestApi = "counter" :> Post '[JSON] Counter -- endpoint for increasing the counter
55+ :<|> "counter" :> Get '[JSON] Counter -- endpoint to get the current value
56+
57+type TestApi' = TestApi -- The API we want a JS handler for
58+ :<|> Raw -- used for serving static files
5459
60+-- this proxy only targets the proper endpoints of our API,
61+-- not the static file serving bit
5562testApi :: Proxy TestApi
5663testApi = Proxy
5764
65+-- this proxy targets everything
66+testApi' :: Proxy TestApi'
67+testApi' = Proxy
68+
5869-- * Server-side handler
5970
6071-- where our static files reside
@@ -65,26 +76,20 @@ www = "examples/www"
6576server :: TVar Counter -> Server TestApi
6677server counter = counterPlusOne counter -- (+1) on the TVar
6778 :<|> currentValue counter -- read the TVar
79+
80+server' :: TVar Counter -> Server TestApi'
81+server counter = server counter
6882 :<|> serveDirectory www -- serve static files
6983
7084runServer :: TVar Counter -- ^ shared variable for the counter
7185 -> Int -- ^ port the server should listen on
7286 -> IO ()
73−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
87+runServer var port = run port (serve testApi' $ server' var)
8288
8389main :: IO ()
8490main = do
8591 -- write the JS code to www/api.js at startup
86− writeJS (www </> "api.js")
87− [ incCounterJS, currentValueJS ]
92+ writeJSForAPI testApi jquery (www </> "api.js")
8893
8994 -- setup a shared counter
9095 cnt <- newCounter