• Stars
    star
    162
  • Rank 231,772 (Top 5 %)
  • Language
    Haskell
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically derive Elm functions to query servant webservices

Servant Elm

Build Status

Generate Elm functions to query your Servant API!

Elm type generation coutesy of elm-bridge.

Installation

Servant Elm is available on Hackage.

Example

First, some language pragmas and imports.

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

import           Elm.Derive   (defaultOptions, deriveBoth)

import           Servant.API  ((:>), Capture, Get, JSON)
import           Servant.Elm  (DefineElm (DefineElm), Proxy (Proxy), defElmImports, defElmOptions,
                               generateElmModuleWith)

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

data Book = Book
    { name :: String
    }

deriveBoth defaultOptions ''Book

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

Now we can generate Elm functions to query the API:

main :: IO ()
main =
  generateElmModuleWith
    defElmOptions
    [ "Generated"
    , "MyApi"
    ]
    defElmImports
    "my-elm-dir"
    [ DefineElm (Proxy :: Proxy Book)
    ]
    (Proxy :: Proxy BooksApi)

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
import Json.Encode exposing (Value)
-- The following module comes from bartavelle/json-helpers
import Json.Helpers exposing (..)
import Dict exposing (Dict)
import Set
import Http
import String
import Url.Builder

type alias Book  =
   { name: String
   }

jsonDecBook : Json.Decode.Decoder ( Book )
jsonDecBook =
   Json.Decode.succeed (\pname -> {name = pname})
   |> required "name" (Json.Decode.string)

jsonEncBook : Book -> Value
jsonEncBook  val =
   Json.Encode.object
   [ ("name", Json.Encode.string val.name)
   ]


getBooksByBookId : Int -> Http.Request Book
getBooksByBookId capture_bookId =
    let
        params =
            List.filterMap identity
            (List.concat
                [])
    in
        Http.request
            { method =
                "GET"
            , headers =
                []
            , url =
                Url.Builder.absolute
                    [ "books"
                    , capture_bookId |> String.fromInt
                    ]
                    params
            , body =
                Http.emptyBody
            , expect =
                Http.expectJson <| jsonDecBook
            , timeout =
                Nothing
            , withCredentials =
                False
            }

See examples for a complete usage example, or take a look at mattjbray/servant-elm-example-app (elm 0.18) or haskell-servant/example-servant-elm (elm 0.19) 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

More Repositories

1

servant

Main repository for the servant libraries — DSL for describing, serving, querying, mocking, documenting web applications and more!
Haskell
1,738
star
2

servant-auth

Haskell
160
star
3

example-servant-elm

Example for a web app with a servant backend and an elm frontend
Haskell
140
star
4

servant-swagger

Swagger for Servant
Haskell
124
star
5

example-servant-persistent

Simple example to illustrate how to use persistent and servant in combination.
Haskell
81
star
6

example-servant-minimal

A minimal example for a REST-API-server written with servant and a test-suite using servant-client
Haskell
58
star
7

servant-swagger-ui

Provide embedded swagger UI for servant and swagger
JavaScript
46
star
8

servant-quickcheck

Haskell
40
star
9

servant-multipart

Support for file uploads in multipart/form-data for servant
Haskell
40
star
10

servant-snap

Snap port of servant-server (WIP)
Haskell
28
star
11

servant-js

Automatically derive javascript functions to query servant webservices.
Haskell
22
star
12

servant-mock

Derive a mock server for free from your servant API types
Haskell
19
star
13

haskell-servant.github.io

servant website
Haskell
18
star
14

servant-dhall

Servant dhall bindings
Haskell
18
star
15

servant-blaze

Haskell
10
star
16

servant-lucid

Haskell
8
star
17

servant-nix

Support for Nix expressions as HTTP request/response bodies in servant, using hnix
Haskell
7
star
18

servant-cassava

Haskell
6
star
19

servant-uverb

deprecated: https://github.com/haskell-servant/servant/pull/1314
Haskell
5
star
20

HaskellSGMeetup2015

Slides for a talk about servant
Haskell
5
star
21

servant-contrib

A collection of user contributions for servant, a Type-Level Web DSL.
Haskell
4
star
22

servant-client-haxl

Haxl-based client functions for servant APIs
Haskell
4
star
23

servant-yaml

Servant support for yaml
Haskell
3
star
24

servant-jsaddle

servant jsaddle client
Haskell
3
star
25

slides-functional-thursday

HTML
2
star
26

servant-bazaar

An example project to illustrate how to package servant combinators
Haskell
2
star
27

cretheus

Haskell
2
star
28

servant-benchmarks

Benchmarks for servant
Haskell
1
star
29

hugsMay2015Berlin

Slides of the talk for the Berlin Haskell User Group
TeX
1
star
30

cufp-2015

Haskell
1
star