@@ -30,13 +30,14 @@ module Javascript where
30import Control.Monad.IO.Class
30import Control.Monad.IO.Class
31import Data.Aeson
31import Data.Aeson
32import Data.Proxy
32import Data.Proxy
33−import Data.Text (Text)
33+import Data.Text as T (Text)
34+import Data.Text.IO as T (writeFile, readFile)
34import qualified Data.Text as T
35import qualified Data.Text as T
35import GHC.Generics
36import GHC.Generics
36import Language.Javascript.JQuery
37import Language.Javascript.JQuery
37import Network.Wai
38import Network.Wai
38import Servant
39import Servant
39−import Servant.JQuery
40+import Servant.JS
40import System.Random
41import System.Random
@@ -133,7 +134,7 @@ server' = server
133 :<|> serveDirectory "tutorial/t9"
134 :<|> serveDirectory "tutorial/t9"
135app :: Application
136app :: Application
136−app = serve api' server'
137+app = serve api' EmptyConfig server'
139Why two different API types, proxies and servers though? Simply because we don't want to generate javascript functions for the `Raw` part of our API type, so we need a `Proxy` for our API type `API'` without its `Raw` endpoint.
140Why two different API types, proxies and servers though? Simply because we don't want to generate javascript functions for the `Raw` part of our API type, so we need a `Proxy` for our API type `API'` without its `Raw` endpoint.
@@ -141,8 +142,8 @@ Why two different API types, proxies and servers though? Simply because we don't
141Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
142Very similarly to how one can derive haskell functions, we can derive the javascript with just a simple function call to `jsForAPI` from `Servant.JQuery`.
143``` haskell
144``` haskell
144−apiJS :: String
145+apiJS :: Text
145−apiJS = jsForAPI api
146+apiJS = jsForAPI api vanillaJS
148This `String` contains 2 Javascript functions:
149This `String` contains 2 Javascript functions:
@@ -175,9 +176,9 @@ Right before starting up our server, we will need to write this `String` to a fi
175``` haskell
176``` haskell
176writeJSFiles :: IO ()
177writeJSFiles :: IO ()
177writeJSFiles = do
178writeJSFiles = do
178− writeFile "getting-started/gs9/api.js" apiJS
179+ T.writeFile "getting-started/gs9/api.js" apiJS
179− jq <- readFile =<< Language.Javascript.JQuery.file
180+ jq <- T.readFile =<< Language.Javascript.JQuery.file
180− writeFile "getting-started/gs9/jq.js" jq
181+ T.writeFile "getting-started/gs9/jq.js" jq
183And we're good to go. Start the server with `dist/build/tutorial/tutorial 9` and go to `http://localhost:8081/`. Start typing in the name of one of the authors in our database or part of a book title, and check out how long it takes to approximate π using the method mentioned above.
184And we're good to go. Start the server with `dist/build/tutorial/tutorial 9` and go to `http://localhost:8081/`. Start typing in the name of one of the authors in our database or part of a book title, and check out how long it takes to approximate π using the method mentioned above.