Merge pull request #260 from freezeboy/clean-js

Replace servant-jquery with servant-js in remaining files
This commit is contained in:
Julian Arni 2015-11-03 19:39:54 +01:00
commit 6c50beb0f8
3 changed files with 29 additions and 24 deletions

View file

@ -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
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

View file

@ -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

View file

@ -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 <https://github.com/haskell-servant/servant/blob/master/servant-jquery/examples/counter.hs here>
You can find an example <https://github.com/haskell-servant/servant/blob/master/servant-js/examples/counter.hs here>
which serves the generated javascript to a webpage that allows you to trigger
webservice calls.
.
<https://github.com/haskell-servant/servant/blob/master/servant-jquery/CHANGELOG.md CHANGELOG>
<https://github.com/haskell-servant/servant/blob/master/servant-js/CHANGELOG.md CHANGELOG>
license: BSD3
license-file: LICENSE
author: Alp Mestanogullari