git.iko.soy/typeable/servant
on master help sign in
drop the dependency on interpolate and hence on haskell-src-exts, it's becoming too annoying to keep
3e76058Alp Mestanogullari
examples/counter.hs+1−6
@@ -19,9 +19,6 @@ newtype Counter = Counter { value :: Int }
19
19
20instance ToJSON Counter
20instance ToJSON Counter
21
21
22−instance ToSample Counter where
23− toSample = Just 0
24−
25-- * Shared counter operations
22-- * Shared counter operations
26
23
27-- Creating a counter that starts from 0
24-- Creating a counter that starts from 0
@@ -42,7 +39,6 @@ currentValue counter = liftIO $ readTVarIO counter
42-- * Our API type
39-- * Our API type
43type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
40type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
44 :<|> "counter" :> Get Counter -- endpoint to get the current value
41 :<|> "counter" :> Get Counter -- endpoint to get the current value
45− :<|> "doc" :> Raw -- serve the documentation
46 :<|> Raw -- used for serving static files
42 :<|> Raw -- used for serving static files
47
43
48testApi :: Proxy TestApi
44testApi :: Proxy TestApi
@@ -58,7 +54,6 @@ www = "examples/www"
58server :: TVar Counter -> Server TestApi
54server :: TVar Counter -> Server TestApi
59server counter = counterPlusOne counter -- (+1) on the TVar
55server counter = counterPlusOne counter -- (+1) on the TVar
60 :<|> currentValue counter -- read the TVar
56 :<|> currentValue counter -- read the TVar
61− :<|> serveDocumentation testApi -- serve the API docs
62 :<|> serveDirectory www -- serve static files
57 :<|> serveDirectory www -- serve static files
63
58
64runServer :: TVar Counter -- ^ shared variable for the counter
59runServer :: TVar Counter -- ^ shared variable for the counter
@@ -70,7 +65,7 @@ runServer var port = run port (serve testApi $ server var)
70incCounterNamed :: FunctionName -> AjaxReq
65incCounterNamed :: FunctionName -> AjaxReq
71currentValueNamed :: FunctionName -> AjaxReq
66currentValueNamed :: FunctionName -> AjaxReq
72
67
73−incCounterNamed :<|> currentValueNamed :<|> _ :<|> _ = jquery testApi
68+incCounterNamed :<|> currentValueNamed :<|> _ = jquery testApi
74
69
75writeJS :: FilePath -> [AjaxReq] -> IO ()
70writeJS :: FilePath -> [AjaxReq] -> IO ()
76writeJS fp functions = writeFile fp $
71writeJS fp functions = writeFile fp $
servant-jquery.cabal+1−1
@@ -16,7 +16,7 @@ library
16 exposed-modules: Servant.JQuery
16 exposed-modules: Servant.JQuery
17 other-modules: Servant.JQuery.Internal
17 other-modules: Servant.JQuery.Internal
18 -- other-extensions:
18 -- other-extensions:
19− build-depends: base >=4.5 && <5, servant >= 0.2, lens >= 4, interpolate
19+ build-depends: base >=4.5 && <5, servant >= 0.2, lens >= 4
20 hs-source-dirs: src
20 hs-source-dirs: src
21 default-language: Haskell2010
21 default-language: Haskell2010
22 ghc-options: -O2 -Wall
22 ghc-options: -O2 -Wall
src/Servant/JQuery.hs+13−17
@@ -1,5 +1,4 @@
1{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE QuasiQuotes #-}
3{-# LANGUAGE TypeOperators #-}
2{-# LANGUAGE TypeOperators #-}
4{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
5-----------------------------------------------------------------------------
4-----------------------------------------------------------------------------
@@ -10,8 +9,6 @@
10-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
9-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
11-- Stability : experimental
10-- Stability : experimental
12-- Portability : non-portable
11-- Portability : non-portable
13−--
14−-- Usage:
15module Servant.JQuery
12module Servant.JQuery
16 ( jquery
13 ( jquery
17 , generateJS
14 , generateJS
@@ -21,8 +18,8 @@ module Servant.JQuery
21
18
22import Control.Lens
19import Control.Lens
23import Data.List
20import Data.List
21+import Data.Monoid
24import Data.Proxy
22import Data.Proxy
25−import Data.String.Interpolate
26import Servant.JQuery.Internal
23import Servant.JQuery.Internal
27
24
28jquery :: HasJQ layout => Proxy layout -> JQ layout
25jquery :: HasJQ layout => Proxy layout -> JQ layout
@@ -30,18 +27,17 @@ jquery p = jqueryFor p defReq
30
27
31-- js codegen
28-- js codegen
32generateJS :: AjaxReq -> String
29generateJS :: AjaxReq -> String
33−generateJS req =
30+generateJS req = "\n" <>
34− [i|
35−function #{fname}(#{argsStr})
31+ "function " <> fname <> "(" <> argsStr <> ")\n"
36−{
32+ <> "{\n"
37− $.ajax(
33+ <> " $.ajax(\n"
38− { url: #{url}
34+ <> " { url: " <> url <> "\n"
39− , success: onSuccess #{dataBody}
35+ <> " , success: onSuccess\n"
36+ <> dataBody
40− , error: onError
37+ <> " , error: onError\n"
41− , type: '#{method}'
38+ <> " , type: '" <> method <> "'\n"
42− });
39+ <> " });\n"
43−}
40+ <> "}\n"
44− |]
45
41
46 where argsStr = intercalate ", " args
42 where argsStr = intercalate ", " args
47 args = captures
43 args = captures
@@ -61,7 +57,7 @@ function #{fname}(#{argsStr})
61
57
62 dataBody =
58 dataBody =
63 if req ^. reqBody
59 if req ^. reqBody
64− then "\n , data: JSON.stringify(body)"
60+ then "\n , data: JSON.stringify(body)\n"
65 else ""
61 else ""
66
62
67 fname = req ^. funcName
63 fname = req ^. funcName