-- | Module for HTML-based servers
module Mig.Json.IO
  (
  -- * 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 a = Get (IO a)

instance (ToJsonResp a) => ToServer (Get a) where
  type ServerMonad (Get a) = IO
  toServer :: Get a -> Server (ServerMonad (Get a))
toServer (Get IO 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
<$> IO a
act)

-- Post

newtype Post a = Post (IO a)

instance (ToJsonResp a) => ToServer (Post a) where
  type ServerMonad (Post a) = IO
  toServer :: Post a -> Server (ServerMonad (Post a))
toServer (Post IO 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
<$> IO a
act)

-- Put

newtype Put a = Put (IO a)

instance (ToJsonResp a) => ToServer (Put a) where
  type ServerMonad (Put a) = IO
  toServer :: Put a -> Server (ServerMonad (Put a))
toServer (Put IO 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
<$> IO a
act)

-- Delete

newtype Delete a = Delete (IO a)

instance (ToJsonResp a) => ToServer (Delete a) where
  type ServerMonad (Delete a) = IO
  toServer :: Delete a -> Server (ServerMonad (Delete a))
toServer (Delete IO 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
<$> IO a
act)

-- Patch

newtype Patch a = Patch (IO a)

instance (ToJsonResp a) => ToServer (Patch a) where
  type ServerMonad (Patch a) = IO
  toServer :: Patch a -> Server (ServerMonad (Patch a))
toServer (Patch IO 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
<$> IO a
act)

-- Options

newtype Options a = Options (IO a)

instance (ToJsonResp a) => ToServer (Options a) where
  type ServerMonad (Options a) = IO
  toServer :: Options a -> Server (ServerMonad (Options a))
toServer (Options IO 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
<$> IO a
act)