https://github.com/haskell-miso/servant-miso-client
🍜 A servant-client interpretation for miso
https://github.com/haskell-miso/servant-miso-client
haskell miso servant servant-client
Last synced: 8 months ago
JSON representation
🍜 A servant-client interpretation for miso
- Host: GitHub
- URL: https://github.com/haskell-miso/servant-miso-client
- Owner: haskell-miso
- License: bsd-3-clause
- Created: 2025-09-01T22:46:02.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2025-09-01T23:45:36.000Z (9 months ago)
- Last Synced: 2025-09-02T01:06:35.177Z (9 months ago)
- Topics: haskell, miso, servant, servant-client
- Language: Haskell
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
🍜 servant-miso-client
===================================
This is a [servant-client](https://github.com/haskell-servant/servant) binding to [miso](https://github.com/dmjio/miso).
```haskell
-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
-----------------------------------------------------------------------------
module Main where
-----------------------------------------------------------------------------
import Miso
import Miso.Html.Element as H
import Miso.Html.Event as H
-----------------------------------------------------------------------------
import Data.Aeson
import Data.Proxy
import Servant.Miso.Client
import Servant.API
-----------------------------------------------------------------------------
main :: IO ()
main = run $ startComponent myComponent
{ initialAction = Just Start
}
-----------------------------------------------------------------------------
type MyComponent = App () Action
-----------------------------------------------------------------------------
myComponent :: MyComponent
myComponent = component () update_ $ \() ->
H.div_ []
[ button_ [ onClick Download ] [ "download" ]
] where
update_ = \case
Download -> do
io_ (consoleLog "clicked")
downloadGithub Downloaded DownloadError
DownloadError Response {..} -> io_ $ do
consoleError $ ms (show errorMessage)
Downloaded Response {..} -> io_ $ do
consoleLog $ ms $ show body
Start -> io_ $ do
consoleLog "starting..."
-----------------------------------------------------------------------------
data Action
= Downloaded (Response Value)
| DownloadError (Response MisoString)
| Download
| Start
-----------------------------------------------------------------------------
type API = UploadFile :<|> DownloadFile
-----------------------------------------------------------------------------
type UploadFile
= "api" :> "upload" :> "file1" :> ReqBody '[OctetStream] File :> PostNoContent
-----------------------------------------------------------------------------
type DownloadFile
= "api" :> "download" :> "file1" :> QueryParam "foo" MisoString :> Get '[OctetStream] File
-----------------------------------------------------------------------------
uploadFile
:: File
-- ^ File to upload
-> (Response () -> Action)
-- ^ Successful callback (expecting no response)
-> (Response MisoString -> Action)
-- ^ Errorful callback, with error message as param
-> Transition () Action
-----------------------------------------------------------------------------
downloadFile
:: Maybe MisoString
-> (Response File -> Action)
-- ^ Received file
-> (Response MisoString -> Action)
-- ^ Error message
-> Transition () Action
-----------------------------------------------------------------------------
uploadFile :<|> downloadFile = toClient mempty (Proxy @MyComponent) (Proxy @API)
-----------------------------------------------------------------------------
type GitHubAPI = Get '[JSON] Value
-----------------------------------------------------------------------------
downloadGithub :: (Response Value -> Action) -> (Response MisoString -> Action) -> Effect ROOT () Action
downloadGithub = toClient "https://api.github.com" (Proxy @MyComponent) (Proxy @GitHubAPI)
-----------------------------------------------------------------------------
```
### Build
```bash
cabal build
```
### Dev
```bash
cabal build
```