@@ -18,7 +18,7 @@ Let's get some boring language pragmas and imports out of the way.
1818import Data.Proxy (Proxy(Proxy))
1919import Elm (Spec(Spec), ToElmType, specsToDir)
2020import GHC.Generics (Generic)
21−import Servant ((:>), Capture, Get, JSON)
21+import Servant.API ((:>), Capture, Get, JSON)
2222import Servant.Elm (defElmImports, generateElmForAPI)
2323```
2424
@@ -43,49 +43,57 @@ spec = Spec ["Generated", "MyApi"]
4343 : generateElmForAPI (Proxy :: Proxy BooksApi))
4444
4545main :: IO ()
46−main = specsToDir "my-elm-dir" [spec]
46+main = specsToDir [spec] "my-elm-dir"
4747```
4848
4949Let's save this as `example.hs` and run it:
5050
5151```
5252$ stack runghc example.hs
53−Writing: my-elm-dir/Generated/BooksApi.elm
53+Writing: my-elm-dir/Generated/MyApi.elm
5454$
5555```
5656
5757Here's what was generated:
5858
5959```elm
60−module Generated.BooksApi where
60+module Generated.MyApi where
6161
62−import Json.Decode exposing (..)
63−import Json.Decode.Extra exposing (apply)
64−import Json.Encode as JS
62+import Json.Decode exposing ((:=))
63+import Json.Decode.Extra exposing ((|:))
64+import Json.Encode
6565import Http
6666import String
6767import Task
6868
6969
7070type alias Book =
71− {name : String}
71+ { name : String
72+ }
7273
73−decodeBook : Decoder Book
74−decodeBook = Book
75− `map` ("name" := string)
74+decodeBook : Json.Decode.Decoder Book
75+decodeBook =
76+ Json.Decode.succeed Book
77+ |: ("name" := Json.Decode.string)
7678
7779getBooksBy : Int -> Task.Task Http.Error (Book)
7880getBooksBy bookId =
79− let request =
80− { verb = "GET"
81− , headers = [("Content-Type", "application/json")]
82− , url = "/" ++ "books"
83− ++ "/" ++ (bookId |> toString |> Http.uriEncode)
84− , body = Http.empty
85− }
86− in Http.fromJson
87− decodeBook
88− (Http.send Http.defaultSettings request)
81+ let
82+ request =
83+ { verb =
84+ "GET"
85+ , headers =
86+ [("Content-Type", "application/json")]
87+ , url =
88+ "/" ++ "books"
89+ ++ "/" ++ (bookId |> toString |> Http.uriEncode)
90+ , body =
91+ Http.empty
92+ }
93+ in
94+ Http.fromJson
95+ decodeBook
96+ (Http.send Http.defaultSettings request)
8997```
9098
9199See [`examples`](examples) for a complete usage example, or take a look at