-- | Module for HTML-based servers
module Mig.Json
  (
  -- * methods
    Get (..)
  , Post (..)
  , Put (..)
  , Delete (..)
  , Patch (..)
  , Options (..)

  -- * common
  -- | Common re-exports
  , module X
  ) where

import Mig.Common as X
import Mig (ToJsonResp (..))
import Mig.Internal.Types (toMethod)
import Network.HTTP.Types.Method

-- Get

newtype Get m a = Get (m a)

instance (Monad m, ToJsonResp a) => ToServer (Get m a) where
  type ServerMonad (Get m a) = m
  toServer :: Get m a -> Server (ServerMonad (Get m a))
toServer (Get m a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodGet (forall a. ToJsonResp a => a -> Resp
toJsonResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
act)

-- Post

newtype Post m a = Post (m a)

instance (Monad m, ToJsonResp a) => ToServer (Post m a) where
  type ServerMonad (Post m a) = m
  toServer :: Post m a -> Server (ServerMonad (Post m a))
toServer (Post m a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodPost (forall a. ToJsonResp a => a -> Resp
toJsonResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
act)

-- Put

newtype Put m a = Put (m a)

instance (Monad m, ToJsonResp a) => ToServer (Put m a) where
  type ServerMonad (Put m a) = m
  toServer :: Put m a -> Server (ServerMonad (Put m a))
toServer (Put m a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodPut (forall a. ToJsonResp a => a -> Resp
toJsonResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
act)

-- Delete

newtype Delete m a = Delete (m a)

instance (Monad m, ToJsonResp a) => ToServer (Delete m a) where
  type ServerMonad (Delete m a) = m
  toServer :: Delete m a -> Server (ServerMonad (Delete m a))
toServer (Delete m a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodDelete (forall a. ToJsonResp a => a -> Resp
toJsonResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
act)

-- Patch

newtype Patch m a = Patch (m a)

instance (Monad m, ToJsonResp a) => ToServer (Patch m a) where
  type ServerMonad (Patch m a) = m
  toServer :: Patch m a -> Server (ServerMonad (Patch m a))
toServer (Patch m a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodPatch (forall a. ToJsonResp a => a -> Resp
toJsonResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
act)

-- Options

newtype Options m a = Options (m a)

instance (Monad m, ToJsonResp a) => ToServer (Options m a) where
  type ServerMonad (Options m a) = m
  toServer :: Options m a -> Server (ServerMonad (Options m a))
toServer (Options m a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodOptions (forall a. ToJsonResp a => a -> Resp
toJsonResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
act)