{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
module Servant.Hateoas.Resource
(
ResourceLink(..),
MkResource,
Resource(..),
addSelfRel,
EmbeddingResource(..),
CollectingResource(..),
ToResource(..)
) where
import Servant
import Servant.Hateoas.RelationLink
import Data.Kind
import Data.Aeson
type family MkResource ct :: (Type -> Type)
data ResourceLink =
CompleteLink Link
| TemplateLink RelationLink
deriving (Int -> ResourceLink -> ShowS
[ResourceLink] -> ShowS
ResourceLink -> String
(Int -> ResourceLink -> ShowS)
-> (ResourceLink -> String)
-> ([ResourceLink] -> ShowS)
-> Show ResourceLink
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResourceLink -> ShowS
showsPrec :: Int -> ResourceLink -> ShowS
$cshow :: ResourceLink -> String
show :: ResourceLink -> String
$cshowList :: [ResourceLink] -> ShowS
showList :: [ResourceLink] -> ShowS
Show)
instance ToJSON ResourceLink where
toJSON :: ResourceLink -> Value
toJSON (CompleteLink Link
l) = let uri :: URI
uri = Link -> URI
linkURI Link
l in URI -> Value
forall a. ToJSON a => a -> Value
toJSON (URI -> Value) -> URI -> Value
forall a b. (a -> b) -> a -> b
$ URI
uri { uriPath = "/" <> uriPath uri }
toJSON (TemplateLink RelationLink
l) = RelationLink -> Value
forall a. ToJSON a => a -> Value
toJSON (RelationLink -> Value) -> RelationLink -> Value
forall a b. (a -> b) -> a -> b
$ RelationLink
l { _path = "/" <> _path l }
class Resource res where
wrap :: a -> res a
addRel :: (String, ResourceLink) -> res a -> res a
addSelfRel :: Resource res => ResourceLink -> res a -> res a
addSelfRel :: forall (res :: * -> *) a.
Resource res =>
ResourceLink -> res a -> res a
addSelfRel ResourceLink
l = (String, ResourceLink) -> res a -> res a
forall a. (String, ResourceLink) -> res a -> res a
forall (res :: * -> *) a.
Resource res =>
(String, ResourceLink) -> res a -> res a
addRel (String
"self", ResourceLink
l)
class Resource res => EmbeddingResource res where
embed :: ToJSON e => (String, res e) -> res a -> res a
class Resource res => CollectingResource res where
collect :: a -> res a -> res a
class ToResource res a where
toResource :: Proxy res -> a -> res a
default toResource :: Resource res => Proxy res -> a -> res a
toResource Proxy res
_ = a -> res a
forall a. a -> res a
forall (res :: * -> *) a. Resource res => a -> res a
wrap
instance Resource res => ToResource res [a]