git.iko.soy/haskell-servant/servant-elm
on master help sign in
fix examples, update README code
commitb5901c432db9b4cedff46628c87768e6b1d3f3b1 authorMatt Bray <mattjbray@gmail.com> dateSat 13 Feb 2016, 22:55 (11 years ago) parentda3fbfb
5 files changed+99−29
README.md+29−21 examples/books/generate.hs+4−4 examples/giphy/generate.hs+4−4 examples/readme-example/generate.hs+25 examples/readme-example/my-elm-dir/Generated/MyApi.elm+37
README.md+29−21
@@ -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
examples/books/generate.hs+4−4
@@ -1,11 +1,11 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE TypeOperators #-}
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE TypeOperators #-}
44
55import Data.Proxy
66import Elm (Spec (Spec), ToElmType, specsToDir)
77import GHC.Generics (Generic)
8−import Servant ((:<|>), (:>), Capture, Get, JSON, Post, ReqBody)
8+import Servant.API ((:<|>), (:>), Capture, Get, JSON, Post, ReqBody)
99import Servant.Elm (ElmOptions (..), defElmImports, defElmOptions,
1010 generateElmForAPIWith)
1111
examples/giphy/generate.hs+4−4
@@ -1,11 +1,11 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE TypeOperators #-}
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE TypeOperators #-}
44
55import Data.Proxy
66import Elm (Spec (Spec), ToElmType, specsToDir)
77import GHC.Generics (Generic)
8−import Servant ((:>), Get, JSON, QueryParam)
8+import Servant.API ((:>), Get, JSON, QueryParam)
99import Servant.Elm (ElmOptions (..), defElmImports, defElmOptions,
1010 generateElmForAPIWith)
1111
examples/readme-example/generate.hsnew file+25
@@ -0,0 +1,25 @@
1+ {-# LANGUAGE DeriveGeneric #-}
2+ {-# LANGUAGE DataKinds #-}
3+ {-# LANGUAGE TypeOperators #-}
4+
5+ import Data.Proxy (Proxy(Proxy))
6+ import Elm (Spec(Spec), ToElmType, specsToDir)
7+ import GHC.Generics (Generic)
8+ import Servant.API ((:>), Capture, Get, JSON)
9+ import Servant.Elm (defElmImports, generateElmForAPI)
10+
11+ data Book = Book
12+ { name :: String
13+ } deriving (Generic)
14+
15+ instance ToElmType Book
16+
17+ type BooksApi = "books" :> Capture "bookId" Int :> Get '[JSON] Book
18+
19+ spec :: Spec
20+ spec = Spec ["Generated", "MyApi"]
21+ (defElmImports
22+ : generateElmForAPI (Proxy :: Proxy BooksApi))
23+
24+ main :: IO ()
25+ main = specsToDir [spec] "my-elm-dir"
examples/readme-example/my-elm-dir/Generated/MyApi.elmnew file+37
@@ -0,0 +1,37 @@
1+module Generated.MyApi where
2+
3+import Json.Decode exposing ((:=))
4+import Json.Decode.Extra exposing ((|:))
5+import Json.Encode
6+import Http
7+import String
8+import Task
9+
10+
11+type alias Book =
12+ { name : String
13+ }
14+
15+decodeBook : Json.Decode.Decoder Book
16+decodeBook =
17+ Json.Decode.succeed Book
18+ |: ("name" := Json.Decode.string)
19+
20+getBooksBy : Int -> Task.Task Http.Error (Book)
21+getBooksBy bookId =
22+ let
23+ request =
24+ { verb =
25+ "GET"
26+ , headers =
27+ [("Content-Type", "application/json")]
28+ , url =
29+ "/" ++ "books"
30+ ++ "/" ++ (bookId |> toString |> Http.uriEncode)
31+ , body =
32+ Http.empty
33+ }
34+ in
35+ Http.fromJson
36+ decodeBook
37+ (Http.send Http.defaultSettings request)