3[](https://travis-ci.org/haskell-servant/servant-elm)
5Generate Elm functions to query your Servant API!
7Elm type generation coutesy of [elm-bridge](http://hackage.haskell.org/package/elm-bridge).
11Servant Elm is [available on Hackage](http://hackage.haskell.org/package/servant-elm).
15First, some language pragmas and imports.
18{-# LANGUAGE DataKinds #-}
19{-# LANGUAGE OverloadedStrings #-}
20{-# LANGUAGE TemplateHaskell #-}
21{-# LANGUAGE TypeOperators #-}
23import Elm.Derive (defaultOptions, deriveBoth)
25import Servant.API ((:>), Capture, Get, JSON)
26import Servant.Elm (DefineElm (DefineElm), Proxy (Proxy), defElmImports, defElmOptions,
27 generateElmModuleWith)
30We have some Haskell-defined types and our Servant API.
37deriveBoth defaultOptions ''Book
39type BooksApi = "books" :> Capture "bookId" Int :> Get '[JSON] Book
42Now we can generate Elm functions to query the API:
54 [ DefineElm (Proxy :: Proxy Book)
56 (Proxy :: Proxy BooksApi)
59Let's save this as `example.hs` and run it:
62$ stack runghc example.hs
63Writing: my-elm-dir/Generated/MyApi.elm
66Here's what was generated:
69module Generated.MyApi exposing(..)
72import Json.Encode exposing (Value)
73-- The following module comes from bartavelle/json-helpers
74import Json.Helpers exposing (..)
75import Dict exposing (Dict)
85jsonDecBook : Json.Decode.Decoder ( Book )
87 Json.Decode.succeed (\pname -> {name = pname})
88 |> required "name" (Json.Decode.string)
90jsonEncBook : Book -> Value
93 [ ("name", Json.Encode.string val.name)
97getBooksByBookId : Int -> Http.Request Book
98getBooksByBookId capture_bookId =
101 List.filterMap identity
113 , capture_bookId |> String.fromInt
119 Http.expectJson <| jsonDecBook
127See [`examples`](examples) for a complete usage example, or take a look at
128[`mattjbray/servant-elm-example-app`](https://github.com/mattjbray/servant-elm-example-app) (elm 0.18) or [`haskell-servant/example-servant-elm`](https://github.com/haskell-servant/example-servant-elm) (elm 0.19) for an example project using this library.
133$ git clone https://github.com/mattjbray/servant-elm.git
136$ stack test --flag servant-elm:integration
139To build all examples:
148$ cd examples/e2e-tests
150# Open http://localhost:8000/elm/Main.elm
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