MyNixOS website logo
Description

Specify Servant APIs with records.

Specify Servant APIs with records instead of :<|> trees. See the README for more information. This package has been merged into servant 0.14.1, please use that instead if available.

Build Status Hackage

tl;dr

Specify Servant APIs with simple records instead of :<|> trees.

data Site route = Site
  { about :: route :-
      "about" :> Get '[PlainText] Text
  , faq :: route :-
      "faq" :> Get '[PlainText] Text
  } deriving Generic

siteServer :: Site AsServer
siteServer = Site
  { about = return "about"
  , faq = return "faq"
  }

type Api = ToServant (Site AsApi)

main :: IO ()
main = run 31337 $ serve (Proxy :: Proxy Api) (toServant siteServer)

So long and thanks for all the :<|>

Servant is great for building typesafe APIs. However, its biggest weakness is that you need to specify APIs as a tree of :<|> operators:

type SiteOld =
       "about" :> Get '[PlainText] Text
  :<|> "faq" :> Get '[PlainText] Text
  :<|> "subsite" :> SubSiteOld

type SubSiteOld =
       "echo" :> Capture "x" Text :> Get '[PlainText] Text
  :<|> "timestwo" :> Capture "x" Int :> Get '[PlainText] Text

siteOldServer :: Server SiteOld
siteOldServer =
       return "about"
  :<|> subSiteOldServer
  :<|> return "faq"

subSiteOldServer :: Server SubSiteOld
subSiteOldServer =
       return
  :<|> (\x -> return $ T.pack $ show (x * 2))

This is cumbersome because you need to remember the exact order of routes when you are implementing the API. Not only that, but if you get anything wrong, you are treated with an incomprehensible error message. Let's see what happens if we mix up the implementations of /faq and /subsite:

    • Couldn't match type ‘(Text
                            -> transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                                 ServantErr IO Text)
                           :<|> (Int
                                 -> transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                                      ServantErr IO Text)’
                     with ‘transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                             ServantErr IO Text’
      Expected type: Server SiteOld
        Actual type: transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                       ServantErr IO Text
                     :<|> (Server SubSiteOld
                           :<|> ((Text
                                  -> transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                                       ServantErr IO Text)
                                 :<|> (Int
                                       -> transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                                            ServantErr IO Text)))
    • In the expression:
        return "about" :<|> subSiteOldServer :<|> return "faq"
      In an equation for ‘siteOldServer’:
          siteOldServer
            = return "about" :<|> subSiteOldServer :<|> return "faq"

Can you figure out what the problem is from that?

What this package lets you do is to specify routes as regular Haskell records:

data Site route = Site
  { about :: route :-
      "about" :> Get '[PlainText] Text
  , faq :: route :-
      "faq" :> Get '[PlainText] Text
  , subSite :: route :-
      "subsite" :> ToServant (SubSite AsApi) -- record APIs can be nested easily
  } deriving Generic

data SubSite route = SubSite
  { echo :: route :-
      "echo" :> Capture "x" Text :> Get '[PlainText] Text
  , timesTwo :: route :-
      "timestwo" :> Capture "x" Int :> Get '[PlainText] Text
  } deriving Generic

type Api = ToServant (Site AsApi)

subSiteServer :: SubSite AsServer
subSiteServer = SubSite
  { echo = return
  , timesTwo = \x -> return $ T.pack $ show (x * 2)
  }

siteServer :: Site AsServer
siteServer = Site
  { about = return "about"
  , faq = return "faq"
  , subSite = toServant subSiteServer
  }

Now everything is named so we don't need to remember which route is which. These records can be converted to Servant-compatible types like this:

ToServant (MyApiRecord AsApi)

and to Servant-compatible implementations like this:

toServant myApiRecordServer

These functions work with any library based on Servant, not just servant-server.

What happens if we make the same mistake as above?

    • Couldn't match type ‘(Text
                            -> transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                                 ServantErr IO Text)
                           :<|> (Int
                                 -> transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                                      ServantErr IO Text)’
                     with ‘transformers-0.5.2.0:Control.Monad.Trans.Except.ExceptT
                             ServantErr IO Text’
      Expected type: AsServer :- ("faq" :> Get '[PlainText] Text)
        Actual type: ToServant (SubSite AsServer)
    • In the ‘faq’ field of a record
      In the expression:
        Site
          {about = return "about", faq = toServant subSiteServer,
           subSite = return "faq",
           home = return "So long and thanks for all the :<|>"}
      In an equation for ‘siteServer’:
          siteServer
            = Site
                {about = return "about", faq = toServant subSiteServer,
                 subSite = return "faq",
                 home = return "So long and thanks for all the :<|>"}

Now, GHC tells us that the problem is in the faq field, and that we passed a SubSite route rather than an "faq" route.

This approach is based on my solga library, which also has these benefits and is a lot simpler than Servant.

Metadata

Version

0.1.0.3

License

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows