{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_HADDOCK not-home #-}
module Servant.Links (
module Servant.API.TypeLevel,
safeLink
, safeLink'
, allLinks
, allLinks'
, URI(..)
, AsLink
, fieldLink
, fieldLink'
, allFieldLinks
, allFieldLinks'
, HasLink(..)
, Link
, linkURI
, linkURI'
, LinkArrayElementStyle (..)
, Param (..)
, linkSegments
, linkQueryParams
, linkFragment
) where
import Data.List
import Data.Constraint
import Data.Proxy
(Proxy (..))
import Data.Singletons.Bool
(SBool (..), SBoolI (..))
import qualified Data.Text as Text
import qualified Data.Text.Encoding as TE
import Data.Type.Bool
(If)
import GHC.TypeLits
(KnownSymbol, TypeError, symbolVal)
import Network.URI
(URI (..), escapeURIString, isUnreserved)
import Prelude ()
import Prelude.Compat
import Servant.API.Alternative
((:<|>) ((:<|>)))
import Servant.API.BasicAuth
(BasicAuth)
import Servant.API.Capture
(Capture', CaptureAll)
import Servant.API.Description
(Description, Summary)
import Servant.API.Empty
(EmptyAPI (..))
import Servant.API.Experimental.Auth
(AuthProtect)
import Servant.API.Fragment
(Fragment)
import Servant.API.Generic
import Servant.API.Header
(Header')
import Servant.API.HttpVersion
(HttpVersion)
import Servant.API.IsSecure
(IsSecure)
import Servant.API.Modifiers
(FoldRequired)
import Servant.API.NamedRoutes
(NamedRoutes)
import Servant.API.QueryParam
(QueryFlag, QueryParam', QueryParams)
import Servant.API.Raw
(Raw)
import Servant.API.RemoteHost
(RemoteHost)
import Servant.API.ReqBody
(ReqBody')
import Servant.API.Stream
(Stream, StreamBody')
import Servant.API.Sub
(type (:>))
import Servant.API.TypeErrors
import Servant.API.TypeLevel
import Servant.API.UVerb
import Servant.API.Vault
(Vault)
import Servant.API.Verbs
(Verb, NoContentVerb)
import Servant.API.WithNamedContext
(WithNamedContext)
import Web.HttpApiData
import Data.Kind
(Type)
data Link = Link
{ Link -> [Escaped]
_segments :: [Escaped]
, Link -> [Param]
_queryParams :: [Param]
, Link -> Fragment'
_fragment :: Fragment'
} deriving Int -> Link -> ShowS
[Link] -> ShowS
Link -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Link] -> ShowS
$cshowList :: [Link] -> ShowS
show :: Link -> String
$cshow :: Link -> String
showsPrec :: Int -> Link -> ShowS
$cshowsPrec :: Int -> Link -> ShowS
Show
newtype Escaped = Escaped String
type Fragment' = Maybe String
escaped :: String -> Escaped
escaped :: String -> Escaped
escaped = String -> Escaped
Escaped forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ShowS
escapeURIString Char -> Bool
isUnreserved
getEscaped :: Escaped -> String
getEscaped :: Escaped -> String
getEscaped (Escaped String
s) = String
s
instance Show Escaped where
showsPrec :: Int -> Escaped -> ShowS
showsPrec Int
d (Escaped String
s) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
d String
s
show :: Escaped -> String
show (Escaped String
s) = forall a. Show a => a -> String
show String
s
linkSegments :: Link -> [String]
linkSegments :: Link -> [String]
linkSegments = forall a b. (a -> b) -> [a] -> [b]
map Escaped -> String
getEscaped forall b c a. (b -> c) -> (a -> b) -> a -> c
. Link -> [Escaped]
_segments
linkQueryParams :: Link -> [Param]
linkQueryParams :: Link -> [Param]
linkQueryParams = Link -> [Param]
_queryParams
linkFragment :: Link -> Fragment'
linkFragment :: Link -> Fragment'
linkFragment = Link -> Fragment'
_fragment
instance ToHttpApiData Link where
toHeader :: Link -> ByteString
toHeader = Text -> ByteString
TE.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToHttpApiData a => a -> Text
toUrlPiece
toUrlPiece :: Link -> Text
toUrlPiece Link
l =
let uri :: URI
uri = Link -> URI
linkURI Link
l
in String -> Text
Text.pack forall a b. (a -> b) -> a -> b
$ URI -> String
uriPath URI
uri forall a. [a] -> [a] -> [a]
++ URI -> String
uriQuery URI
uri forall a. [a] -> [a] -> [a]
++ URI -> String
uriFragment URI
uri
data Param
= SingleParam String Text.Text
| ArrayElemParam String Text.Text
| FlagParam String
deriving Int -> Param -> ShowS
[Param] -> ShowS
Param -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Param] -> ShowS
$cshowList :: [Param] -> ShowS
show :: Param -> String
$cshow :: Param -> String
showsPrec :: Int -> Param -> ShowS
$cshowsPrec :: Int -> Param -> ShowS
Show
addSegment :: Escaped -> Link -> Link
addSegment :: Escaped -> Link -> Link
addSegment Escaped
seg Link
l = Link
l { _segments :: [Escaped]
_segments = Link -> [Escaped]
_segments Link
l forall a. Semigroup a => a -> a -> a
<> [Escaped
seg] }
addQueryParam :: Param -> Link -> Link
addQueryParam :: Param -> Link -> Link
addQueryParam Param
qp Link
l =
Link
l { _queryParams :: [Param]
_queryParams = Link -> [Param]
_queryParams Link
l forall a. Semigroup a => a -> a -> a
<> [Param
qp] }
addFragment :: Fragment' -> Link -> Link
addFragment :: Fragment' -> Link -> Link
addFragment Fragment'
fr Link
l = Link
l { _fragment :: Fragment'
_fragment = Fragment'
fr }
linkURI :: Link -> URI
linkURI :: Link -> URI
linkURI = LinkArrayElementStyle -> Link -> URI
linkURI' LinkArrayElementStyle
LinkArrayElementBracket
data LinkArrayElementStyle
= LinkArrayElementBracket
| LinkArrayElementPlain
deriving (LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
$c/= :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
== :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
$c== :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
Eq, Eq LinkArrayElementStyle
LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
LinkArrayElementStyle -> LinkArrayElementStyle -> Ordering
LinkArrayElementStyle
-> LinkArrayElementStyle -> LinkArrayElementStyle
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: LinkArrayElementStyle
-> LinkArrayElementStyle -> LinkArrayElementStyle
$cmin :: LinkArrayElementStyle
-> LinkArrayElementStyle -> LinkArrayElementStyle
max :: LinkArrayElementStyle
-> LinkArrayElementStyle -> LinkArrayElementStyle
$cmax :: LinkArrayElementStyle
-> LinkArrayElementStyle -> LinkArrayElementStyle
>= :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
$c>= :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
> :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
$c> :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
<= :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
$c<= :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
< :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
$c< :: LinkArrayElementStyle -> LinkArrayElementStyle -> Bool
compare :: LinkArrayElementStyle -> LinkArrayElementStyle -> Ordering
$ccompare :: LinkArrayElementStyle -> LinkArrayElementStyle -> Ordering
Ord, Int -> LinkArrayElementStyle -> ShowS
[LinkArrayElementStyle] -> ShowS
LinkArrayElementStyle -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LinkArrayElementStyle] -> ShowS
$cshowList :: [LinkArrayElementStyle] -> ShowS
show :: LinkArrayElementStyle -> String
$cshow :: LinkArrayElementStyle -> String
showsPrec :: Int -> LinkArrayElementStyle -> ShowS
$cshowsPrec :: Int -> LinkArrayElementStyle -> ShowS
Show, Int -> LinkArrayElementStyle
LinkArrayElementStyle -> Int
LinkArrayElementStyle -> [LinkArrayElementStyle]
LinkArrayElementStyle -> LinkArrayElementStyle
LinkArrayElementStyle
-> LinkArrayElementStyle -> [LinkArrayElementStyle]
LinkArrayElementStyle
-> LinkArrayElementStyle
-> LinkArrayElementStyle
-> [LinkArrayElementStyle]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: LinkArrayElementStyle
-> LinkArrayElementStyle
-> LinkArrayElementStyle
-> [LinkArrayElementStyle]
$cenumFromThenTo :: LinkArrayElementStyle
-> LinkArrayElementStyle
-> LinkArrayElementStyle
-> [LinkArrayElementStyle]
enumFromTo :: LinkArrayElementStyle
-> LinkArrayElementStyle -> [LinkArrayElementStyle]
$cenumFromTo :: LinkArrayElementStyle
-> LinkArrayElementStyle -> [LinkArrayElementStyle]
enumFromThen :: LinkArrayElementStyle
-> LinkArrayElementStyle -> [LinkArrayElementStyle]
$cenumFromThen :: LinkArrayElementStyle
-> LinkArrayElementStyle -> [LinkArrayElementStyle]
enumFrom :: LinkArrayElementStyle -> [LinkArrayElementStyle]
$cenumFrom :: LinkArrayElementStyle -> [LinkArrayElementStyle]
fromEnum :: LinkArrayElementStyle -> Int
$cfromEnum :: LinkArrayElementStyle -> Int
toEnum :: Int -> LinkArrayElementStyle
$ctoEnum :: Int -> LinkArrayElementStyle
pred :: LinkArrayElementStyle -> LinkArrayElementStyle
$cpred :: LinkArrayElementStyle -> LinkArrayElementStyle
succ :: LinkArrayElementStyle -> LinkArrayElementStyle
$csucc :: LinkArrayElementStyle -> LinkArrayElementStyle
Enum, LinkArrayElementStyle
forall a. a -> a -> Bounded a
maxBound :: LinkArrayElementStyle
$cmaxBound :: LinkArrayElementStyle
minBound :: LinkArrayElementStyle
$cminBound :: LinkArrayElementStyle
Bounded)
linkURI' :: LinkArrayElementStyle -> Link -> URI
linkURI' :: LinkArrayElementStyle -> Link -> URI
linkURI' LinkArrayElementStyle
addBrackets (Link [Escaped]
segments [Param]
q_params Fragment'
mfragment) =
String -> Maybe URIAuth -> String -> String -> String -> URI
URI forall a. Monoid a => a
mempty
forall a. Maybe a
Nothing
(forall a. [a] -> [[a]] -> [a]
intercalate String
"/" forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Escaped -> String
getEscaped [Escaped]
segments)
([Param] -> String
makeQueries [Param]
q_params)
(Fragment' -> String
makeFragment Fragment'
mfragment)
where
makeQueries :: [Param] -> String
makeQueries :: [Param] -> String
makeQueries [] = String
""
makeQueries [Param]
xs =
String
"?" forall a. Semigroup a => a -> a -> a
<> forall a. [a] -> [[a]] -> [a]
intercalate String
"&" (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Param -> String
makeQuery [Param]
xs)
makeQuery :: Param -> String
makeQuery :: Param -> String
makeQuery (ArrayElemParam String
k Text
v) = ShowS
escape String
k forall a. Semigroup a => a -> a -> a
<> String
style forall a. Semigroup a => a -> a -> a
<> ShowS
escape (Text -> String
Text.unpack Text
v)
makeQuery (SingleParam String
k Text
v) = ShowS
escape String
k forall a. Semigroup a => a -> a -> a
<> String
"=" forall a. Semigroup a => a -> a -> a
<> ShowS
escape (Text -> String
Text.unpack Text
v)
makeQuery (FlagParam String
k) = ShowS
escape String
k
makeFragment :: Fragment' -> String
makeFragment :: Fragment' -> String
makeFragment Fragment'
Nothing = String
""
makeFragment (Just String
fr) = String
"#" forall a. Semigroup a => a -> a -> a
<> ShowS
escape String
fr
style :: String
style = case LinkArrayElementStyle
addBrackets of
LinkArrayElementStyle
LinkArrayElementBracket -> String
"[]="
LinkArrayElementStyle
LinkArrayElementPlain -> String
"="
escape :: String -> String
escape :: ShowS
escape = (Char -> Bool) -> ShowS
escapeURIString Char -> Bool
isUnreserved
safeLink
:: forall endpoint api. (IsElem endpoint api, HasLink endpoint)
=> Proxy api
-> Proxy endpoint
-> MkLink endpoint Link
safeLink :: forall endpoint api.
(IsElem endpoint api, HasLink endpoint) =>
Proxy api -> Proxy endpoint -> MkLink endpoint Link
safeLink = forall endpoint api a.
(IsElem endpoint api, HasLink endpoint) =>
(Link -> a) -> Proxy api -> Proxy endpoint -> MkLink endpoint a
safeLink' forall a. a -> a
id
safeLink'
:: forall endpoint api a. (IsElem endpoint api, HasLink endpoint)
=> (Link -> a)
-> Proxy api
-> Proxy endpoint
-> MkLink endpoint a
safeLink' :: forall endpoint api a.
(IsElem endpoint api, HasLink endpoint) =>
(Link -> a) -> Proxy api -> Proxy endpoint -> MkLink endpoint a
safeLink' Link -> a
toA Proxy api
_ Proxy endpoint
endpoint = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA Proxy endpoint
endpoint ([Escaped] -> [Param] -> Fragment' -> Link
Link forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty)
allLinks
:: forall api. HasLink api
=> Proxy api
-> MkLink api Link
allLinks :: forall {k} (api :: k). HasLink api => Proxy api -> MkLink api Link
allLinks = forall {k} (api :: k) a.
HasLink api =>
(Link -> a) -> Proxy api -> MkLink api a
allLinks' forall a. a -> a
id
allLinks'
:: forall api a. HasLink api
=> (Link -> a)
-> Proxy api
-> MkLink api a
allLinks' :: forall {k} (api :: k) a.
HasLink api =>
(Link -> a) -> Proxy api -> MkLink api a
allLinks' Link -> a
toA Proxy api
api = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA Proxy api
api ([Escaped] -> [Param] -> Fragment' -> Link
Link forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty)
fieldLink
:: ( IsElem endpoint (ToServantApi routes), HasLink endpoint
, GenericServant routes AsApi
)
=> (routes AsApi -> endpoint)
-> MkLink endpoint Link
fieldLink :: forall endpoint (routes :: * -> *).
(IsElem endpoint (ToServantApi routes), HasLink endpoint,
GenericServant routes AsApi) =>
(routes AsApi -> endpoint) -> MkLink endpoint Link
fieldLink = forall (routes :: * -> *) endpoint a.
(IsElem endpoint (ToServantApi routes), HasLink endpoint,
GenericServant routes AsApi) =>
(Link -> a) -> (routes AsApi -> endpoint) -> MkLink endpoint a
fieldLink' forall a. a -> a
id
fieldLink'
:: forall routes endpoint a.
( IsElem endpoint (ToServantApi routes), HasLink endpoint
, GenericServant routes AsApi
)
=> (Link -> a)
-> (routes AsApi -> endpoint)
-> MkLink endpoint a
fieldLink' :: forall (routes :: * -> *) endpoint a.
(IsElem endpoint (ToServantApi routes), HasLink endpoint,
GenericServant routes AsApi) =>
(Link -> a) -> (routes AsApi -> endpoint) -> MkLink endpoint a
fieldLink' Link -> a
toA routes AsApi -> endpoint
_ = forall endpoint api a.
(IsElem endpoint api, HasLink endpoint) =>
(Link -> a) -> Proxy api -> Proxy endpoint -> MkLink endpoint a
safeLink' Link -> a
toA (forall (routes :: * -> *).
GenericServant routes AsApi =>
Proxy routes -> Proxy (ToServantApi routes)
genericApi (forall {k} (t :: k). Proxy t
Proxy :: Proxy routes)) (forall {k} (t :: k). Proxy t
Proxy :: Proxy endpoint)
data AsLink (a :: *)
instance GenericMode (AsLink a) where
type (AsLink a) :- api = MkLink api a
allFieldLinks
:: ( HasLink (ToServantApi routes)
, GenericServant routes (AsLink Link)
, ToServant routes (AsLink Link) ~ MkLink (ToServantApi routes) Link
)
=> routes (AsLink Link)
allFieldLinks :: forall (routes :: * -> *).
(HasLink (ToServantApi routes),
GenericServant routes (AsLink Link),
ToServant routes (AsLink Link)
~ MkLink (ToServantApi routes) Link) =>
routes (AsLink Link)
allFieldLinks = forall (routes :: * -> *) a.
(HasLink (ToServantApi routes), GenericServant routes (AsLink a),
ToServant routes (AsLink a) ~ MkLink (ToServantApi routes) a) =>
(Link -> a) -> routes (AsLink a)
allFieldLinks' forall a. a -> a
id
allFieldLinks'
:: forall routes a.
( HasLink (ToServantApi routes)
, GenericServant routes (AsLink a)
, ToServant routes (AsLink a) ~ MkLink (ToServantApi routes) a
)
=> (Link -> a)
-> routes (AsLink a)
allFieldLinks' :: forall (routes :: * -> *) a.
(HasLink (ToServantApi routes), GenericServant routes (AsLink a),
ToServant routes (AsLink a) ~ MkLink (ToServantApi routes) a) =>
(Link -> a) -> routes (AsLink a)
allFieldLinks' Link -> a
toA
= forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant
forall a b. (a -> b) -> a -> b
$ forall {k} (api :: k) a.
HasLink api =>
(Link -> a) -> Proxy api -> MkLink api a
allLinks' Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy (ToServantApi routes))
class HasLink endpoint where
type MkLink endpoint (a :: *)
toLink
:: (Link -> a)
-> Proxy endpoint
-> Link
-> MkLink endpoint a
instance (KnownSymbol sym, HasLink sub) => HasLink (sym :> sub) where
type MkLink (sym :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a) -> Proxy (sym :> sub) -> Link -> MkLink (sym :> sub) a
toLink Link -> a
toA Proxy (sym :> sub)
_ =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Escaped -> Link -> Link
addSegment (String -> Escaped
escaped String
seg)
where
seg :: String
seg = forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy sym)
instance (KnownSymbol sym, ToHttpApiData v, HasLink sub, SBoolI (FoldRequired mods))
=> HasLink (QueryParam' mods sym v :> sub)
where
type MkLink (QueryParam' mods sym v :> sub) a = If (FoldRequired mods) v (Maybe v) -> MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (QueryParam' mods sym v :> sub)
-> Link
-> MkLink (QueryParam' mods sym v :> sub) a
toLink Link -> a
toA Proxy (QueryParam' mods sym v :> sub)
_ Link
l If (FoldRequired mods) v (Maybe v)
mv =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall a b. (a -> b) -> a -> b
$
case forall (b :: Bool). SBoolI b => SBool b
sbool :: SBool (FoldRequired mods) of
SBool (FoldRequired mods)
STrue -> (Param -> Link -> Link
addQueryParam forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text -> Param
SingleParam String
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToHttpApiData a => a -> Text
toQueryParam) If (FoldRequired mods) v (Maybe v)
mv Link
l
SBool (FoldRequired mods)
SFalse -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (Param -> Link -> Link
addQueryParam forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text -> Param
SingleParam String
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToHttpApiData a => a -> Text
toQueryParam) If (FoldRequired mods) v (Maybe v)
mv Link
l
where
k :: String
k :: String
k = forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy sym)
instance (KnownSymbol sym, ToHttpApiData v, HasLink sub)
=> HasLink (QueryParams sym v :> sub)
where
type MkLink (QueryParams sym v :> sub) a = [v] -> MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (QueryParams sym v :> sub)
-> Link
-> MkLink (QueryParams sym v :> sub) a
toLink Link -> a
toA Proxy (QueryParams sym v :> sub)
_ Link
l =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Link
l' v
v -> Param -> Link -> Link
addQueryParam (String -> Text -> Param
ArrayElemParam String
k (forall a. ToHttpApiData a => a -> Text
toQueryParam v
v)) Link
l') Link
l
where
k :: String
k = forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy sym)
instance (KnownSymbol sym, HasLink sub)
=> HasLink (QueryFlag sym :> sub)
where
type MkLink (QueryFlag sym :> sub) a = Bool -> MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (QueryFlag sym :> sub)
-> Link
-> MkLink (QueryFlag sym :> sub) a
toLink Link -> a
toA Proxy (QueryFlag sym :> sub)
_ Link
l Bool
False =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) Link
l
toLink Link -> a
toA Proxy (QueryFlag sym :> sub)
_ Link
l Bool
True =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall a b. (a -> b) -> a -> b
$ Param -> Link -> Link
addQueryParam (String -> Param
FlagParam String
k) Link
l
where
k :: String
k = forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
Proxy :: Proxy sym)
instance (HasLink a, HasLink b) => HasLink (a :<|> b) where
type MkLink (a :<|> b) r = MkLink a r :<|> MkLink b r
toLink :: forall a.
(Link -> a) -> Proxy (a :<|> b) -> Link -> MkLink (a :<|> b) a
toLink Link -> a
toA Proxy (a :<|> b)
_ Link
l = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy a) Link
l forall a b. a -> b -> a :<|> b
:<|> forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy b) Link
l
instance HasLink sub => HasLink (ReqBody' mods ct a :> sub) where
type MkLink (ReqBody' mods ct a :> sub) r = MkLink sub r
toLink :: forall a.
(Link -> a)
-> Proxy (ReqBody' mods ct a :> sub)
-> Link
-> MkLink (ReqBody' mods ct a :> sub) a
toLink Link -> a
toA Proxy (ReqBody' mods ct a :> sub)
_ = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (StreamBody' mods framing ct a :> sub) where
type MkLink (StreamBody' mods framing ct a :> sub) r = MkLink sub r
toLink :: forall a.
(Link -> a)
-> Proxy (StreamBody' mods framing ct a :> sub)
-> Link
-> MkLink (StreamBody' mods framing ct a :> sub) a
toLink Link -> a
toA Proxy (StreamBody' mods framing ct a :> sub)
_ = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance (ToHttpApiData v, HasLink sub)
=> HasLink (Capture' mods sym v :> sub)
where
type MkLink (Capture' mods sym v :> sub) a = v -> MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (Capture' mods sym v :> sub)
-> Link
-> MkLink (Capture' mods sym v :> sub) a
toLink Link -> a
toA Proxy (Capture' mods sym v :> sub)
_ Link
l v
v =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall a b. (a -> b) -> a -> b
$
Escaped -> Link -> Link
addSegment (String -> Escaped
escaped forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack forall a b. (a -> b) -> a -> b
$ forall a. ToHttpApiData a => a -> Text
toUrlPiece v
v) Link
l
instance (ToHttpApiData v, HasLink sub)
=> HasLink (CaptureAll sym v :> sub)
where
type MkLink (CaptureAll sym v :> sub) a = [v] -> MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (CaptureAll sym v :> sub)
-> Link
-> MkLink (CaptureAll sym v :> sub) a
toLink Link -> a
toA Proxy (CaptureAll sym v :> sub)
_ Link
l [v]
vs = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> a -> b
$ Escaped -> Link -> Link
addSegment forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Escaped
escaped forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToHttpApiData a => a -> Text
toUrlPiece) Link
l [v]
vs
instance HasLink sub => HasLink (Header' mods sym (a :: *) :> sub) where
type MkLink (Header' mods sym a :> sub) r = MkLink sub r
toLink :: forall a.
(Link -> a)
-> Proxy (Header' mods sym a :> sub)
-> Link
-> MkLink (Header' mods sym a :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (Vault :> sub) where
type MkLink (Vault :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (Vault :> sub) -> Link -> MkLink (Vault :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (Description s :> sub) where
type MkLink (Description s :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (Description s :> sub)
-> Link
-> MkLink (Description s :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (Summary s :> sub) where
type MkLink (Summary s :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (Summary s :> sub) -> Link -> MkLink (Summary s :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (HttpVersion :> sub) where
type MkLink (HttpVersion:> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (HttpVersion :> sub)
-> Link
-> MkLink (HttpVersion :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (IsSecure :> sub) where
type MkLink (IsSecure :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (IsSecure :> sub) -> Link -> MkLink (IsSecure :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (WithNamedContext name context sub) where
type MkLink (WithNamedContext name context sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (WithNamedContext name context sub)
-> Link
-> MkLink (WithNamedContext name context sub) a
toLink Link -> a
toA Proxy (WithNamedContext name context sub)
_ = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (RemoteHost :> sub) where
type MkLink (RemoteHost :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (RemoteHost :> sub)
-> Link
-> MkLink (RemoteHost :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink sub => HasLink (BasicAuth realm a :> sub) where
type MkLink (BasicAuth realm a :> sub) r = MkLink sub r
toLink :: forall a.
(Link -> a)
-> Proxy (BasicAuth realm a :> sub)
-> Link
-> MkLink (BasicAuth realm a :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance HasLink EmptyAPI where
type MkLink EmptyAPI a = EmptyAPI
toLink :: forall a.
(Link -> a) -> Proxy EmptyAPI -> Link -> MkLink EmptyAPI a
toLink Link -> a
_ Proxy EmptyAPI
_ Link
_ = EmptyAPI
EmptyAPI
instance HasLink (Verb m s ct a) where
type MkLink (Verb m s ct a) r = r
toLink :: forall a.
(Link -> a)
-> Proxy (Verb m s ct a) -> Link -> MkLink (Verb m s ct a) a
toLink Link -> a
toA Proxy (Verb m s ct a)
_ = Link -> a
toA
instance HasLink (NoContentVerb m) where
type MkLink (NoContentVerb m) r = r
toLink :: forall a.
(Link -> a)
-> Proxy (NoContentVerb m) -> Link -> MkLink (NoContentVerb m) a
toLink Link -> a
toA Proxy (NoContentVerb m)
_ = Link -> a
toA
instance HasLink Raw where
type MkLink Raw a = a
toLink :: forall a. (Link -> a) -> Proxy Raw -> Link -> MkLink Raw a
toLink Link -> a
toA Proxy Raw
_ = Link -> a
toA
instance HasLink (Stream m status fr ct a) where
type MkLink (Stream m status fr ct a) r = r
toLink :: forall a.
(Link -> a)
-> Proxy (Stream m status fr ct a)
-> Link
-> MkLink (Stream m status fr ct a) a
toLink Link -> a
toA Proxy (Stream m status fr ct a)
_ = Link -> a
toA
instance HasLink (UVerb m ct a) where
type MkLink (UVerb m ct a) r = r
toLink :: forall a.
(Link -> a)
-> Proxy (UVerb m ct a) -> Link -> MkLink (UVerb m ct a) a
toLink Link -> a
toA Proxy (UVerb m ct a)
_ = Link -> a
toA
type GLinkConstraints routes a =
( MkLink (ToServant routes AsApi) a ~ ToServant routes (AsLink a)
, GenericServant routes (AsLink a)
)
class GLink (routes :: * -> *) (a :: *) where
gLinkProof :: Dict (GLinkConstraints routes a)
instance GLinkConstraints routes a => GLink routes a where
gLinkProof :: Dict (GLinkConstraints routes a)
gLinkProof = forall (a :: Constraint). a => Dict a
Dict
instance
( HasLink (ToServantApi routes)
, forall a. GLink routes a
) => HasLink (NamedRoutes routes) where
type MkLink (NamedRoutes routes) a = routes (AsLink a)
toLink
:: forall a. (Link -> a)
-> Proxy (NamedRoutes routes)
-> Link
-> routes (AsLink a)
toLink :: forall a.
(Link -> a)
-> Proxy (NamedRoutes routes) -> Link -> routes (AsLink a)
toLink Link -> a
toA Proxy (NamedRoutes routes)
_ Link
l = case forall (routes :: * -> *) a.
GLink routes a =>
Dict (GLinkConstraints routes a)
gLinkProof @routes @a of
Dict (GLinkConstraints routes a)
Dict -> forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant forall a b. (a -> b) -> a -> b
$ forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy @(ToServantApi routes)) Link
l
instance HasLink sub => HasLink (AuthProtect tag :> sub) where
type MkLink (AuthProtect tag :> sub) a = MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (AuthProtect tag :> sub)
-> Link
-> MkLink (AuthProtect tag :> sub) a
toLink = forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance (HasLink sub, ToHttpApiData v)
=> HasLink (Fragment v :> sub) where
type MkLink (Fragment v :> sub) a = v -> MkLink sub a
toLink :: forall a.
(Link -> a)
-> Proxy (Fragment v :> sub)
-> Link
-> MkLink (Fragment v :> sub) a
toLink Link -> a
toA Proxy (Fragment v :> sub)
_ Link
l v
mv =
forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub) forall a b. (a -> b) -> a -> b
$
Fragment' -> Link -> Link
addFragment ((forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToHttpApiData a => a -> Text
toQueryParam) v
mv) Link
l
simpleToLink
:: forall sub a combinator.
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a)
=> Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink :: forall {k} sub a (combinator :: k).
(HasLink sub, MkLink sub a ~ MkLink (combinator :> sub) a) =>
Proxy sub
-> (Link -> a)
-> Proxy (combinator :> sub)
-> Link
-> MkLink (combinator :> sub) a
simpleToLink Proxy sub
_ Link -> a
toA Proxy (combinator :> sub)
_ = forall {k} (endpoint :: k) a.
HasLink endpoint =>
(Link -> a) -> Proxy endpoint -> Link -> MkLink endpoint a
toLink Link -> a
toA (forall {k} (t :: k). Proxy t
Proxy :: Proxy sub)
instance TypeError (PartialApplication
#if __GLASGOW_HASKELL__ >= 904
@(Type -> Constraint)
#endif
HasLink arr) => HasLink ((arr :: a -> b) :> sub)
where
type MkLink (arr :> sub) _ = TypeError (PartialApplication (HasLink :: * -> Constraint) arr)
toLink :: forall a.
(Link -> a) -> Proxy (arr :> sub) -> Link -> MkLink (arr :> sub) a
toLink = forall a. HasCallStack => String -> a
error String
"unreachable"
instance {-# OVERLAPPABLE #-} TypeError (NoInstanceForSub
#if __GLASGOW_HASKELL__ >= 904
@(Type -> Constraint)
#endif
HasLink ty) => HasLink (ty :> sub)
instance {-# OVERLAPPABLE #-} TypeError (NoInstanceFor (HasLink api)) => HasLink api