servant-elm: Automatically derive Elm functions to query servant webservices.

[ bsd3, library, web ] [ Propose Tags ]

Please see README.md


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
examples

Build the example programs.

Disabled
integration

Build the integration tests (requires an Elm installation).

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.2.0.0, 0.3.0.0, 0.3.0.1, 0.4.0.0, 0.4.0.1, 0.5.0.0, 0.6.0.0, 0.6.0.1, 0.6.0.2, 0.6.1, 0.7.0, 0.7.1, 0.7.2, 0.7.3
Change log CHANGELOG.md
Dependencies base (>=4.7 && <5), elm-export (>=0.5), lens, servant (>=0.8), servant-elm, servant-foreign (>=0.8), text, wl-pprint-text [details]
License BSD-3-Clause
Copyright 2015-2016 Matt Bray
Author Matt Bray
Maintainer mattjbray@gmail.com
Category Web
Home page http://github.com/mattjbray/servant-elm#readme
Source repo head: git clone https://github.com/mattjbray/servant-elm
Uploaded by mattjbray at 2017-02-04T16:13:54Z
Distributions LTSHaskell:0.7.3, NixOS:0.7.3
Reverse Dependencies 1 direct, 0 indirect [details]
Executables readme-example, giphy-example, e2e-tests-example, books-example
Downloads 8886 total (46 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-02-04 [all 1 reports]

Readme for servant-elm-0.4.0.0

[back to package description]

Servant Elm

Build Status

Generate Elm functions to query your Servant API!

Elm type generation coutesy of krisajenkins/elm-export.

Installation

Servant Elm is available on Hackage.

Example

First, some language pragmas and imports.

{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators     #-}

import           Elm          (Spec (Spec), specsToDir, toElmDecoderSource,
                               toElmTypeSource)
import           GHC.Generics (Generic)
import           Servant.API  ((:>), Capture, Get, JSON)
import           Servant.Elm  (ElmType, Proxy (Proxy), defElmImports,
                               generateElmForAPI)

We have some Haskell-defined types and our Servant API.

data Book = Book
    { name :: String
    } deriving (Generic)

instance ElmType Book

type BooksApi = "books" :> Capture "bookId" Int :> Get '[JSON] Book

Now we can generate Elm functions to query the API:

spec :: Spec
spec = Spec ["Generated", "MyApi"]
            (defElmImports
             : toElmTypeSource    (Proxy :: Proxy Book)
             : toElmDecoderSource (Proxy :: Proxy Book)
             : generateElmForAPI  (Proxy :: Proxy BooksApi))

main :: IO ()
main = specsToDir [spec] "my-elm-dir"

Let's save this as example.hs and run it:

$ stack runghc example.hs
Writing: my-elm-dir/Generated/MyApi.elm
$

Here's what was generated:

module Generated.MyApi exposing (..)

import Json.Decode exposing (..)
import Json.Decode.Pipeline exposing (..)
import Json.Encode
import Http
import String


type alias Book =
    { name : String
    }

decodeBook : Decoder Book
decodeBook =
    decode Book
        |> required "name" string

getBooksByBookId : Int -> Http.Request (Book)
getBooksByBookId capture_bookId =
    Http.request
        { method =
            "GET"
        , headers =
            []
        , url =
            String.join "/"
                [ ""
                , "books"
                , capture_bookId |> toString |> Http.encodeUri
                ]
        , body =
            Http.emptyBody
        , expect =
            Http.expectJson decodeBook
        , timeout =
            Nothing
        , withCredentials =
            False
        }

See examples for a complete usage example, or take a look at mattjbray/servant-elm-example-app for an example project using this library.

Development

$ git clone https://github.com/mattjbray/servant-elm.git
$ cd servant-elm
$ stack test
$ stack test --flag servant-elm:integration

To build all examples:

$ make examples

To run an example:

$ cd examples/e2e-tests
$ elm-reactor
# Open http://localhost:8000/elm/Main.elm