3Type safe querying of servant APIs from the browser.
5`servant-client-ghcjs` is much like `servant-client`, as both packages allow you to generate functions that query the endpoints of your servant API. Both packages should feel the same in usage. The big difference lies in how they perform the actual requests. `servant-client` (indirectly) uses your operating system's socket mechanisms, whereas `servant-client-ghcjs` uses your browser's [XHR](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) mechanisms to send requests.
7This guide assumes knowledge of servant. Reading its [documentation](http://docs.servant.dev) is recommended if you're new to the subject.
9## Using servant-client-ghcjs
10`servant-client-ghcjs` should feel familiar if you've worked with `servant-client`.
12Take the following API (taken from the [Querying an API](http://docs.servant.dev/en/stable/tutorial/Client.html) section in the servant documentation)
15{-# LANGUAGE DataKinds #-}
16{-# LANGUAGE DeriveGeneric #-}
17{-# LANGUAGE TypeOperators #-}
24import Servant.API -- From the 'servant' package, to define the API itself
25import Servant.Client.Ghcjs -- To generate client functions
28 "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
29 :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
30 :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
33-- Data types used in the API
35data Position = Position
38 } deriving (Show, Generic)
40instance FromJSON Position
42newtype HelloMessage = HelloMessage { msg :: String }
43 deriving (Show, Generic)
45instance FromJSON HelloMessage
47data ClientInfo = ClientInfo
48 { clientName :: String
49 , clientEmail :: String
51 , clientInterestedIn :: [String]
54instance ToJSON ClientInfo
61 } deriving (Show, Generic)
63instance FromJSON Email
66Client functions are generated with the `client` function, like with `servant-client`:
74 -> ClientM HelloMessage
76marketing :: ClientInfo
82position :<|> hello :<|> marketing = client api
85To run these requests, they only need to be given to `runClientM`. The type of which is as follows:
88runClientM :: ClientM a -> IO (Either ServantError a)
91The requests can then be run as follows:
96 ePos <- runClientM $ position 10 20
99 eHelloMessage <- runClientM $ hello (Just "Servant")
102 eEmail <- runClientM $ marketing ClientInfo
103 { clientName = "Servant"
104 , clientEmail = "servant@example.com"
106 , clientInterestedIn = ["servant", "haskell", "type safety", "web apps"]
111`runClientM` requires no URL, as it assumes that all requests are meant for the server that served the web page on which the code is being run. It is however possible to call REST APIs from other locations with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) using `runClientMOrigin`:
114runClientMOrigin :: ClientM a -> ClientEnv -> IO (Either ServantError a)
117Where `ClientEnv` holds a `BaseURL` that tells servant where to send the request to.
119# Common client functions
120Specifically in big applications it can be desirable to have client functions that work for both `servant-client` *and* `servant-client-ghcjs`. Luckily, the common bits of those two packages live in a parent package, called `servant-client-core`. This package holds the tools to create generic client functions. Generating clients this way is a bit different, though, as the client functions need to be generic in the monad `m` that runs the actual requests.
122In the example below, the client functions are put in a data type called `APIClient`, which has `m` as a type parameter. The lowercase `apiClient` constructs this data type, demanding that `m` is indeed a monad that can run requests.
125{-# LANGUAGE FlexibleContexts #-}
126{-# LANGUAGE RecordWildCards #-}
127{-# LANGUAGE ScopedTypeVariables #-}
128{-# LANGUAGE TypeApplications #-}
131import Servant.Client.Core -- From the 'servant-client-core' package
135data APIClient m = APIClient
136 { position :: Int -> Int -> m Position
137 , hello :: Maybe String -> m HelloMessage
138 , marketing :: ClientInfo -> m Email
145apiClient = APIClient { .. }
149 :<|> marketing = Proxy @API `clientIn` Proxy @m
152The call site changes slightly too, as the functions now need to be taken from `apiClient`:
155import Servant.Client.Ghcjs
159 ePos <- runClientM $ position apiClient 10 20
163Here's how the requests would be performed using the regular `servant-client` package:
167import Network.HTTP.Client ( newManager, defaultManagerSettings )
171 mgr <- newManager defaultManagerSettings
172 let clientBaseUrl = BaseUrl Http "www.example.com" 80 ""
173 ePos <- runClientM (position apiClient 10 20) $ mkClientEnv mgr clientBaseUrl
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list