module Web.Hyperbole.Effect.Query where
import Data.Aeson (FromJSON, ToJSON)
import Data.ByteString qualified as BS
import Data.Default (Default (..))
import Data.Maybe (fromMaybe)
import Data.String.Conversions (cs)
import Effectful
import Effectful.Dispatch.Dynamic (send)
import Web.Hyperbole.Data.QueryData (FromQuery (..), Param (..), QueryData (..), ToQuery (..), queryData)
import Web.Hyperbole.Data.QueryData qualified as QueryData
import Web.Hyperbole.Effect.Hyperbole (Hyperbole (..), request)
import Web.Hyperbole.Types.Client (Client (..), clientSetQuery)
import Web.Hyperbole.Types.Request hiding (Param)
import Web.Hyperbole.Types.Response
import Prelude
query :: (FromQuery a, Hyperbole :> es) => Eff es a
query :: forall a (es :: [Effect]).
(FromQuery a, Hyperbole :> es) =>
Eff es a
query = do
q <- Eff es QueryData
forall (es :: [Effect]). (Hyperbole :> es) => Eff es QueryData
queryParams
case parseQuery q of
Left String
e -> Hyperbole (Eff es) a -> Eff es a
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Hyperbole (Eff es) a -> Eff es a)
-> Hyperbole (Eff es) a -> Eff es a
forall a b. (a -> b) -> a -> b
$ Response -> Hyperbole (Eff es) a
forall (a :: * -> *) b. Response -> Hyperbole a b
RespondNow (Response -> Hyperbole (Eff es) a)
-> Response -> Hyperbole (Eff es) a
forall a b. (a -> b) -> a -> b
$ ResponseError -> Response
Err (ResponseError -> Response) -> ResponseError -> Response
forall a b. (a -> b) -> a -> b
$ String -> ResponseError
ErrQuery (String -> ResponseError) -> String -> ResponseError
forall a b. (a -> b) -> a -> b
$ String
"Query Parse " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
e String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" from " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a b. ConvertibleStrings a b => a -> b
cs (QueryData -> String
forall a. Show a => a -> String
show QueryData
q)
Right a
a -> a -> Eff es a
forall a. a -> Eff es a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
setQuery :: (ToQuery a, Hyperbole :> es) => a -> Eff es ()
setQuery :: forall a (es :: [Effect]).
(ToQuery a, Hyperbole :> es) =>
a -> Eff es ()
setQuery a
a = do
(QueryData -> QueryData) -> Eff es ()
forall (es :: [Effect]).
(Hyperbole :> es) =>
(QueryData -> QueryData) -> Eff es ()
modifyQueryData (QueryData -> QueryData -> QueryData
forall a b. a -> b -> a
const (QueryData -> QueryData -> QueryData)
-> QueryData -> QueryData -> QueryData
forall a b. (a -> b) -> a -> b
$ a -> QueryData
forall a. ToQuery a => a -> QueryData
toQuery a
a)
modifyQuery :: (ToQuery a, FromQuery a, Default a, Hyperbole :> es) => (a -> a) -> Eff es a
modifyQuery :: forall a (es :: [Effect]).
(ToQuery a, FromQuery a, Default a, Hyperbole :> es) =>
(a -> a) -> Eff es a
modifyQuery a -> a
f = do
s <- Eff es a
forall a (es :: [Effect]).
(FromQuery a, Hyperbole :> es) =>
Eff es a
query
let updated = a -> a
f a
s
setQuery updated
pure updated
clearQuery :: (Hyperbole :> es) => Eff es ()
clearQuery :: forall (es :: [Effect]). (Hyperbole :> es) => Eff es ()
clearQuery =
QueryData -> Eff es ()
forall a (es :: [Effect]).
(ToQuery a, Hyperbole :> es) =>
a -> Eff es ()
setQuery (QueryData
forall a. Monoid a => a
mempty :: QueryData)
param :: (FromJSON a, Hyperbole :> es) => Param -> Eff es a
param :: forall a (es :: [Effect]).
(FromJSON a, Hyperbole :> es) =>
Param -> Eff es a
param Param
p = do
q <- Eff es QueryData
forall (es :: [Effect]). (Hyperbole :> es) => Eff es QueryData
queryParams
case QueryData.require p q of
Left String
e -> Hyperbole (Eff es) a -> Eff es a
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (Hyperbole (Eff es) a -> Eff es a)
-> Hyperbole (Eff es) a -> Eff es a
forall a b. (a -> b) -> a -> b
$ Response -> Hyperbole (Eff es) a
forall (a :: * -> *) b. Response -> Hyperbole a b
RespondNow (Response -> Hyperbole (Eff es) a)
-> Response -> Hyperbole (Eff es) a
forall a b. (a -> b) -> a -> b
$ ResponseError -> Response
Err (ResponseError -> Response) -> ResponseError -> Response
forall a b. (a -> b) -> a -> b
$ String -> ResponseError
ErrQuery (String -> String
forall a b. ConvertibleStrings a b => a -> b
cs String
e)
Right a
a -> a -> Eff es a
forall a. a -> Eff es a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
lookupParam :: (FromJSON a, Hyperbole :> es) => Param -> Eff es (Maybe a)
lookupParam :: forall a (es :: [Effect]).
(FromJSON a, Hyperbole :> es) =>
Param -> Eff es (Maybe a)
lookupParam Param
p = do
Param -> QueryData -> Maybe a
forall a. FromJSON a => Param -> QueryData -> Maybe a
QueryData.lookup Param
p (QueryData -> Maybe a) -> Eff es QueryData -> Eff es (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff es QueryData
forall (es :: [Effect]). (Hyperbole :> es) => Eff es QueryData
queryParams
setParam :: (ToJSON a, Hyperbole :> es) => Param -> a -> Eff es ()
setParam :: forall a (es :: [Effect]).
(ToJSON a, Hyperbole :> es) =>
Param -> a -> Eff es ()
setParam Param
key a
a = do
(QueryData -> QueryData) -> Eff es ()
forall (es :: [Effect]).
(Hyperbole :> es) =>
(QueryData -> QueryData) -> Eff es ()
modifyQueryData (Param -> a -> QueryData -> QueryData
forall a. ToJSON a => Param -> a -> QueryData -> QueryData
QueryData.insert Param
key a
a)
deleteParam :: (Hyperbole :> es) => Param -> Eff es ()
deleteParam :: forall (es :: [Effect]). (Hyperbole :> es) => Param -> Eff es ()
deleteParam Param
key = do
(QueryData -> QueryData) -> Eff es ()
forall (es :: [Effect]).
(Hyperbole :> es) =>
(QueryData -> QueryData) -> Eff es ()
modifyQueryData (Param -> QueryData -> QueryData
QueryData.delete Param
key)
queryParams :: (Hyperbole :> es) => Eff es QueryData
queryParams :: forall (es :: [Effect]). (Hyperbole :> es) => Eff es QueryData
queryParams = do
cq <- Eff es (Maybe QueryData)
clientQuery
rq <- requestQuery
pure $ fromMaybe rq cq
where
clientQuery :: Eff es (Maybe QueryData)
clientQuery = (.query) (Client -> Maybe QueryData)
-> Eff es Client -> Eff es (Maybe QueryData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Hyperbole (Eff es) Client -> Eff es Client
forall (e :: Effect) (es :: [Effect]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send Hyperbole (Eff es) Client
forall (a :: * -> *). Hyperbole a Client
GetClient
requestQuery :: (Hyperbole :> es) => Eff es QueryData
requestQuery :: forall (es :: [Effect]). (Hyperbole :> es) => Eff es QueryData
requestQuery = do
r <- Eff es Request
forall (es :: [Effect]). (Hyperbole :> es) => Eff es Request
request
pure $ queryData $ filter (not . isSystemParam) r.query
isSystemParam :: (ByteString, b) -> Bool
isSystemParam (ByteString
key, b
_) =
ByteString
"hyp-" ByteString -> ByteString -> Bool
`BS.isPrefixOf` ByteString
key
modifyQueryData :: (Hyperbole :> es) => (QueryData -> QueryData) -> Eff es ()
modifyQueryData :: forall (es :: [Effect]).
(Hyperbole :> es) =>
(QueryData -> QueryData) -> Eff es ()
modifyQueryData QueryData -> QueryData
f = do
q <- Eff es QueryData
forall (es :: [Effect]). (Hyperbole :> es) => Eff es QueryData
queryParams
send $ ModClient $ clientSetQuery (f q)