From b88981cf146355819426a74473ff1b4ffce1c213 Mon Sep 17 00:00:00 2001 From: Freezeboy Date: Mon, 2 Nov 2015 19:55:13 +0100 Subject: [PATCH] Replace servant-jquery with servant-js in remaining files --- servant-js/README.md | 39 +++++++++++++++++++++---------------- servant-js/docs.sh | 6 +++--- servant-js/servant-js.cabal | 8 ++++---- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/servant-js/README.md b/servant-js/README.md index 53d09880..5a5d071b 100644 --- a/servant-js/README.md +++ b/servant-js/README.md @@ -2,11 +2,14 @@ ![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png) -This library lets you derive automatically (JQuery based) Javascript functions that let you query each endpoint of a *servant* webservice. +This library lets you derive automatically Javascript functions that let you query each endpoint of a *servant* webservice. + +It contains a powerful system allowing you to generate functions for several frameworks (Angular, AXios, JQuery) as well as +vanilla (framework-free) javascript code. ## Example -Read more about the following example [here](https://github.com/haskell-servant/servant/tree/master/servant-jquery/tree/master/examples#examples). +Read more about the following example [here](https://github.com/haskell-servant/servant/tree/master/servant-js/tree/master/examples#examples). ``` haskell {-# LANGUAGE DataKinds #-} @@ -21,7 +24,7 @@ import Data.Proxy import GHC.Generics import Network.Wai.Handler.Warp (run) import Servant -import Servant.JQuery +import Servant.JS import System.FilePath -- * A simple Counter data type @@ -48,13 +51,21 @@ currentValue :: MonadIO m => TVar Counter -> m Counter currentValue counter = liftIO $ readTVarIO counter -- * Our API type -type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter - :<|> "counter" :> Get Counter -- endpoint to get the current value - :<|> Raw -- used for serving static files +type TestApi = "counter" :> Post '[JSON] Counter -- endpoint for increasing the counter + :<|> "counter" :> Get '[JSON] Counter -- endpoint to get the current value +type TestApi' = TestApi -- The API we want a JS handler for + :<|> Raw -- used for serving static files + +-- this proxy only targets the proper endpoints of our API, +-- not the static file serving bit testApi :: Proxy TestApi testApi = Proxy +-- this proxy targets everything +testApi' :: Proxy TestApi' +testApi' = Proxy + -- * Server-side handler -- where our static files reside @@ -65,26 +76,20 @@ www = "examples/www" server :: TVar Counter -> Server TestApi server counter = counterPlusOne counter -- (+1) on the TVar :<|> currentValue counter -- read the TVar + +server' :: TVar Counter -> Server TestApi' +server counter = server counter :<|> serveDirectory www -- serve static files runServer :: TVar Counter -- ^ shared variable for the counter -> Int -- ^ port the server should listen on -> IO () -runServer var port = run port (serve testApi $ server var) - --- * Generating the JQuery code - -incCounterJS :<|> currentValueJS :<|> _ = jquery testApi - -writeJS :: FilePath -> [AjaxReq] -> IO () -writeJS fp functions = writeFile fp $ - concatMap generateJS functions +runServer var port = run port (serve testApi' $ server' var) main :: IO () main = do -- write the JS code to www/api.js at startup - writeJS (www "api.js") - [ incCounterJS, currentValueJS ] + writeJSForAPI testApi jquery (www "api.js") -- setup a shared counter cnt <- newCounter diff --git a/servant-js/docs.sh b/servant-js/docs.sh index 58844b4f..9e91c132 100644 --- a/servant-js/docs.sh +++ b/servant-js/docs.sh @@ -1,4 +1,4 @@ -SERVANT_DIR=/tmp/servant-jquery-gh-pages +SERVANT_DIR=/tmp/servant-js-gh-pages # Make a temporary clone @@ -10,7 +10,7 @@ cd $SERVANT_DIR # Make sure to pull the latest -git remote add haskell-servant git@github.com:haskell-servant/servant-jquery.git +git remote add haskell-servant git@github.com:haskell-servant/servant-js.git git fetch haskell-servant @@ -36,7 +36,7 @@ cd $SERVANT_DIR rm * rm -rf build -mv doc/html/servant-jquery/* . +mv doc/html/servant-js/* . rm -r doc/ # Add everything diff --git a/servant-js/servant-js.cabal b/servant-js/servant-js.cabal index 4cb95107..d52b5c36 100644 --- a/servant-js/servant-js.cabal +++ b/servant-js/servant-js.cabal @@ -2,15 +2,15 @@ name: servant-js version: 0.5 synopsis: Automatically derive javascript functions to query servant webservices. description: - Automatically derive jquery-based javascript functions to query servant webservices. + Automatically derive javascript functions to query servant webservices. . - Supports deriving functions using vanilla javascript AJAX requests, Angular or JQuery. + Supports deriving functions using vanilla javascript AJAX requests, Angulari, Axios or JQuery. . - You can find an example + You can find an example which serves the generated javascript to a webpage that allows you to trigger webservice calls. . - + license: BSD3 license-file: LICENSE author: Alp Mestanogullari