-- | 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

-- | Get method. Note that we can not use body input with Get-method, use Post for that.
-- So with Get we can use only URI inputs (Query, Optional, Capture)
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 method
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 method
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 method
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 method
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 method
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)