-- |
-- Copyright        : (c) Raghu Kaippully, 2020
-- License          : MPL-2.0
-- Maintainer       : rkaippully@gmail.com
--
-- Middlewares provided by WebGear.
--
module WebGear.Middlewares
  ( ok
  , noContent
  , badRequest
  , notFound

  , module WebGear.Middlewares.Method
  , module WebGear.Middlewares.Path
  , module WebGear.Middlewares.Header
  , module WebGear.Middlewares.Body
  ) where

import Data.String (IsString)

import WebGear.Middlewares.Body
import WebGear.Middlewares.Header
import WebGear.Middlewares.Method
import WebGear.Middlewares.Path
import WebGear.Trait (Linked, linkzero)
import WebGear.Types (Response (..))

import qualified Network.HTTP.Types as HTTP


-- | Respond with a 200 OK
ok :: Monad m => a -> m (Linked '[] (Response a))
ok :: a -> m (Linked '[] (Response a))
ok = Linked '[] (Response a) -> m (Linked '[] (Response a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Linked '[] (Response a) -> m (Linked '[] (Response a)))
-> (a -> Linked '[] (Response a))
-> a
-> m (Linked '[] (Response a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response a -> Linked '[] (Response a)
forall a. a -> Linked '[] a
linkzero (Response a -> Linked '[] (Response a))
-> (a -> Response a) -> a -> Linked '[] (Response a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
forall a.
Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
Response Status
HTTP.ok200 HashMap HeaderName ByteString
forall a. Monoid a => a
mempty (Maybe a -> Response a) -> (a -> Maybe a) -> a -> Response a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just

-- | Respond with a 400 Bad Request
badRequest :: Monad m => m (Linked '[] (Response a))
badRequest :: m (Linked '[] (Response a))
badRequest = Linked '[] (Response a) -> m (Linked '[] (Response a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Linked '[] (Response a) -> m (Linked '[] (Response a)))
-> Linked '[] (Response a) -> m (Linked '[] (Response a))
forall a b. (a -> b) -> a -> b
$ Response a -> Linked '[] (Response a)
forall a. a -> Linked '[] a
linkzero (Response a -> Linked '[] (Response a))
-> Response a -> Linked '[] (Response a)
forall a b. (a -> b) -> a -> b
$ Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
forall a.
Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
Response Status
HTTP.badRequest400 HashMap HeaderName ByteString
forall a. Monoid a => a
mempty Maybe a
forall a. Maybe a
Nothing

-- | Respond with a 404 NotFound
notFound :: Monad m => m (Linked '[] (Response a))
notFound :: m (Linked '[] (Response a))
notFound = Linked '[] (Response a) -> m (Linked '[] (Response a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Linked '[] (Response a) -> m (Linked '[] (Response a)))
-> Linked '[] (Response a) -> m (Linked '[] (Response a))
forall a b. (a -> b) -> a -> b
$ Response a -> Linked '[] (Response a)
forall a. a -> Linked '[] a
linkzero (Response a -> Linked '[] (Response a))
-> Response a -> Linked '[] (Response a)
forall a b. (a -> b) -> a -> b
$ Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
forall a.
Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
Response Status
HTTP.notFound404 HashMap HeaderName ByteString
forall a. Monoid a => a
mempty Maybe a
forall a. Maybe a
Nothing

-- | Respond with a 204 NoContent
noContent :: (Monad m, IsString s) => m (Linked '[] (Response s))
noContent :: m (Linked '[] (Response s))
noContent = Linked '[] (Response s) -> m (Linked '[] (Response s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Linked '[] (Response s) -> m (Linked '[] (Response s)))
-> Linked '[] (Response s) -> m (Linked '[] (Response s))
forall a b. (a -> b) -> a -> b
$ Response s -> Linked '[] (Response s)
forall a. a -> Linked '[] a
linkzero (Response s -> Linked '[] (Response s))
-> Response s -> Linked '[] (Response s)
forall a b. (a -> b) -> a -> b
$ Status -> HashMap HeaderName ByteString -> Maybe s -> Response s
forall a.
Status -> HashMap HeaderName ByteString -> Maybe a -> Response a
Response Status
HTTP.noContent204 HashMap HeaderName ByteString
forall a. Monoid a => a
mempty (Maybe s -> Response s) -> Maybe s -> Response s
forall a b. (a -> b) -> a -> b
$ s -> Maybe s
forall a. a -> Maybe a
Just ""