drop the dependency on interpolate and hence on haskell-src-exts, it's becoming too annoying to keep
This commit is contained in:
parent
91fc74b70e
commit
3e76058d25
3 changed files with 15 additions and 24 deletions
|
@ -19,9 +19,6 @@ newtype Counter = Counter { value :: Int }
|
||||||
|
|
||||||
instance ToJSON Counter
|
instance ToJSON Counter
|
||||||
|
|
||||||
instance ToSample Counter where
|
|
||||||
toSample = Just 0
|
|
||||||
|
|
||||||
-- * Shared counter operations
|
-- * Shared counter operations
|
||||||
|
|
||||||
-- Creating a counter that starts from 0
|
-- Creating a counter that starts from 0
|
||||||
|
@ -42,7 +39,6 @@ currentValue counter = liftIO $ readTVarIO counter
|
||||||
-- * Our API type
|
-- * Our API type
|
||||||
type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
|
type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
|
||||||
:<|> "counter" :> Get Counter -- endpoint to get the current value
|
:<|> "counter" :> Get Counter -- endpoint to get the current value
|
||||||
:<|> "doc" :> Raw -- serve the documentation
|
|
||||||
:<|> Raw -- used for serving static files
|
:<|> Raw -- used for serving static files
|
||||||
|
|
||||||
testApi :: Proxy TestApi
|
testApi :: Proxy TestApi
|
||||||
|
@ -58,7 +54,6 @@ www = "examples/www"
|
||||||
server :: TVar Counter -> Server TestApi
|
server :: TVar Counter -> Server TestApi
|
||||||
server counter = counterPlusOne counter -- (+1) on the TVar
|
server counter = counterPlusOne counter -- (+1) on the TVar
|
||||||
:<|> currentValue counter -- read the TVar
|
:<|> currentValue counter -- read the TVar
|
||||||
:<|> serveDocumentation testApi -- serve the API docs
|
|
||||||
:<|> serveDirectory www -- serve static files
|
:<|> serveDirectory www -- serve static files
|
||||||
|
|
||||||
runServer :: TVar Counter -- ^ shared variable for the counter
|
runServer :: TVar Counter -- ^ shared variable for the counter
|
||||||
|
@ -70,7 +65,7 @@ runServer var port = run port (serve testApi $ server var)
|
||||||
incCounterNamed :: FunctionName -> AjaxReq
|
incCounterNamed :: FunctionName -> AjaxReq
|
||||||
currentValueNamed :: FunctionName -> AjaxReq
|
currentValueNamed :: FunctionName -> AjaxReq
|
||||||
|
|
||||||
incCounterNamed :<|> currentValueNamed :<|> _ :<|> _ = jquery testApi
|
incCounterNamed :<|> currentValueNamed :<|> _ = jquery testApi
|
||||||
|
|
||||||
writeJS :: FilePath -> [AjaxReq] -> IO ()
|
writeJS :: FilePath -> [AjaxReq] -> IO ()
|
||||||
writeJS fp functions = writeFile fp $
|
writeJS fp functions = writeFile fp $
|
||||||
|
|
|
@ -16,7 +16,7 @@ library
|
||||||
exposed-modules: Servant.JQuery
|
exposed-modules: Servant.JQuery
|
||||||
other-modules: Servant.JQuery.Internal
|
other-modules: Servant.JQuery.Internal
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
build-depends: base >=4.5 && <5, servant >= 0.2, lens >= 4, interpolate
|
build-depends: base >=4.5 && <5, servant >= 0.2, lens >= 4
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
ghc-options: -O2 -Wall
|
ghc-options: -O2 -Wall
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
{-# LANGUAGE DataKinds #-}
|
{-# LANGUAGE DataKinds #-}
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
|
||||||
{-# LANGUAGE TypeOperators #-}
|
{-# LANGUAGE TypeOperators #-}
|
||||||
{-# LANGUAGE FlexibleInstances #-}
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
@ -10,8 +9,6 @@
|
||||||
-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
|
-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
|
||||||
-- Stability : experimental
|
-- Stability : experimental
|
||||||
-- Portability : non-portable
|
-- Portability : non-portable
|
||||||
--
|
|
||||||
-- Usage:
|
|
||||||
module Servant.JQuery
|
module Servant.JQuery
|
||||||
( jquery
|
( jquery
|
||||||
, generateJS
|
, generateJS
|
||||||
|
@ -21,8 +18,8 @@ module Servant.JQuery
|
||||||
|
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
import Data.List
|
import Data.List
|
||||||
|
import Data.Monoid
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
import Data.String.Interpolate
|
|
||||||
import Servant.JQuery.Internal
|
import Servant.JQuery.Internal
|
||||||
|
|
||||||
jquery :: HasJQ layout => Proxy layout -> JQ layout
|
jquery :: HasJQ layout => Proxy layout -> JQ layout
|
||||||
|
@ -30,18 +27,17 @@ jquery p = jqueryFor p defReq
|
||||||
|
|
||||||
-- js codegen
|
-- js codegen
|
||||||
generateJS :: AjaxReq -> String
|
generateJS :: AjaxReq -> String
|
||||||
generateJS req =
|
generateJS req = "\n" <>
|
||||||
[i|
|
"function " <> fname <> "(" <> argsStr <> ")\n"
|
||||||
function #{fname}(#{argsStr})
|
<> "{\n"
|
||||||
{
|
<> " $.ajax(\n"
|
||||||
$.ajax(
|
<> " { url: " <> url <> "\n"
|
||||||
{ url: #{url}
|
<> " , success: onSuccess\n"
|
||||||
, success: onSuccess #{dataBody}
|
<> dataBody
|
||||||
, error: onError
|
<> " , error: onError\n"
|
||||||
, type: '#{method}'
|
<> " , type: '" <> method <> "'\n"
|
||||||
});
|
<> " });\n"
|
||||||
}
|
<> "}\n"
|
||||||
|]
|
|
||||||
|
|
||||||
where argsStr = intercalate ", " args
|
where argsStr = intercalate ", " args
|
||||||
args = captures
|
args = captures
|
||||||
|
@ -61,7 +57,7 @@ function #{fname}(#{argsStr})
|
||||||
|
|
||||||
dataBody =
|
dataBody =
|
||||||
if req ^. reqBody
|
if req ^. reqBody
|
||||||
then "\n , data: JSON.stringify(body)"
|
then "\n , data: JSON.stringify(body)\n"
|
||||||
else ""
|
else ""
|
||||||
|
|
||||||
fname = req ^. funcName
|
fname = req ^. funcName
|
||||||
|
|
Loading…
Add table
Reference in a new issue