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

[ bsd3, library, web ] [ Propose Tags ]

Please see README.md


[Skip to Readme]

Modules

[Last Documentation]

  • Servant
    • Servant.Elm

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, lens, servant (>=0.5 && <0.6), servant-foreign (>=0.5 && <0.6), 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 2016-02-14T00:04:35Z
Distributions LTSHaskell:0.7.3, NixOS:0.7.3
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 8889 total (46 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2016-11-28 [all 3 reports]

Readme for servant-elm-0.1.0.1

[back to package description]

Servant Elm

Build Status

Generate Elm functions to query your Servant API!

Elm type generation coutesy of krisajenkins/elm-export.

Example

Let's get some boring language pragmas and imports out of the way.

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

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

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

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

instance ToElmType 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
             : 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 where

import Json.Decode exposing ((:=))
import Json.Decode.Extra exposing ((|:))
import Json.Encode
import Http
import String
import Task


type alias Book =
  { name : String
  }

decodeBook : Json.Decode.Decoder Book
decodeBook =
  Json.Decode.succeed Book
    |: ("name" := Json.Decode.string)

getBooksBy : Int -> Task.Task Http.Error (Book)
getBooksBy bookId =
  let
    request =
      { verb =
          "GET"
      , headers =
          [("Content-Type", "application/json")]
      , url =
          "/" ++ "books"
          ++ "/" ++ (bookId |> toString |> Http.uriEncode)
      , body =
          Http.empty
      }
  in
    Http.fromJson
      decodeBook
      (Http.send Http.defaultSettings request)

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 build
$ stack test

TODO

Servant API coverage:

  • MatrixFlag / MatrixParam / MatrixParams
  • Header (request)
  • Headers (response)
  • Delete / Patch / Put / Raw
  • Vault / RemoteHost / IsSecure

Other:

  • Option to not use elm-export: generate functions that take a decoder and String arguments.