Skip to content

Instantly share code, notes, and snippets.

@alpmestan
Last active January 2, 2019 23:08
Show Gist options
  • Save alpmestan/16b0473975b2e640364949b306cd23c3 to your computer and use it in GitHub Desktop.
Save alpmestan/16b0473975b2e640364949b306cd23c3 to your computer and use it in GitHub Desktop.
Anonymous """value-level""" servant API type definition
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
import Data.Proxy
import GHC.TypeLits
import Servant.API
infixr 9 >:>
infixr 8 <||>
(>:>) :: Proxy a -> Proxy b -> Proxy (a :> b)
Proxy >:> Proxy = Proxy
(<||>) :: Proxy a -> Proxy b -> Proxy (a :<|> b)
Proxy <||> Proxy = Proxy
path
:: KnownSymbol s
=> Proxy s
path = Proxy
capture
:: KnownSymbol s
=> Proxy (Capture s a)
capture = Proxy
get
:: Proxy (Get cts a)
get = Proxy
-- example
api = path @"hello" >:> capture @"userid" @Int >:> get @'[JSON] @String
<||> path @"bye" >:> capture @"userid" @Int >:> get @'[JSON] @String
-- and then call 'serve api ...' or 'docsFor api' or 'client api' or ...
{-
*Main λ> :t api
api
:: Proxy
(("hello" :> (Capture "userid" Int :> Get '[JSON] String))
:<|> ("bye" :> (Capture "userid" Int :> Get '[JSON] String)))
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment