module Web.Hyperbole.Effect.Request where

import Effectful
import Web.Hyperbole.Data.URI (Path (..))
import Web.Hyperbole.Effect.Hyperbole
import Web.Hyperbole.Effect.Response (parseError)
import Web.Hyperbole.HyperView.Input (InputValue (..))
import Web.Hyperbole.Types.Request


{- | Return the request path

>>> reqPath
["users", "100"]
-}
reqPath :: (Hyperbole :> es) => Eff es Path
reqPath :: forall (es :: [Effect]). (Hyperbole :> es) => Eff es Path
reqPath = (.path) (Request -> Path) -> Eff es Request -> Eff es Path
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff es Request
forall (es :: [Effect]). (Hyperbole :> es) => Eff es Request
request


{- | Return the request body as a 'Form'

Prefer using Type-Safe 'Form's when possible
-}
formBody :: (Hyperbole :> es) => Eff es Form
formBody :: forall (es :: [Effect]). (Hyperbole :> es) => Eff es Form
formBody = do
  (.form) (Request -> Form) -> Eff es Request -> Eff es Form
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff es Request
forall (es :: [Effect]). (Hyperbole :> es) => Eff es Request
request


inputValue :: (InputValue a, Hyperbole :> es) => Eff es a
inputValue :: forall a (es :: [Effect]).
(InputValue a, Hyperbole :> es) =>
Eff es a
inputValue = do
  t <- (.input) (Request -> Text) -> Eff es Request -> Eff es Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff es Request
forall (es :: [Effect]). (Hyperbole :> es) => Eff es Request
request
  case parseInputValue t of
    Left String
err -> String -> Eff es a
forall (es :: [Effect]) a. (Hyperbole :> es) => String -> Eff es a
parseError (String -> Eff es a) -> String -> Eff es a
forall a b. (a -> b) -> a -> b
$ String
"Input: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
err
    Right a
a -> a -> Eff es a
forall a. a -> Eff es a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a