{-# OPTIONS_GHC -Wno-orphans #-}

-- | OpenApi implementation of 'WG.Cookie' and 'WG.SetCookie' traits.
module WebGear.OpenApi.Trait.Cookie () where

import Data.OpenApi hiding (Response)
import Data.Proxy (Proxy (Proxy))
import Data.String (fromString)
import Data.Text (Text)
import GHC.TypeLits (KnownSymbol, symbolVal)
import WebGear.Core.Trait (Get (..), Set (..))
import qualified WebGear.Core.Trait.Cookie as WG
import WebGear.OpenApi.Handler (OpenApiHandler (..))
import WebGear.OpenApi.Trait.Auth (addSecurityScheme)

instance (KnownSymbol name) => Get (OpenApiHandler m) (WG.Cookie e name val) where
  {-# INLINE getTrait #-}
  getTrait :: forall (ts :: [*]).
Prerequisite (Cookie e name val) ts =>
Cookie e name val
-> OpenApiHandler
     m
     (With Request ts)
     (Either
        (Absence (Cookie e name val))
        (Attribute (Cookie e name val) Request))
getTrait Cookie e name val
WG.Cookie =
    (OpenApi -> State Documentation OpenApi)
-> OpenApiHandler
     m
     (With Request ts)
     (Either
        (Absence (Cookie e name val))
        (Attribute (Cookie e name val) Request))
forall {k} {k1} {k2} (m :: k) (a :: k1) (b :: k2).
(OpenApi -> State Documentation OpenApi) -> OpenApiHandler m a b
OpenApiHandler ((OpenApi -> State Documentation OpenApi)
 -> OpenApiHandler
      m
      (With Request ts)
      (Either
         (Absence (Cookie e name val))
         (Attribute (Cookie e name val) Request)))
-> (OpenApi -> State Documentation OpenApi)
-> OpenApiHandler
     m
     (With Request ts)
     (Either
        (Absence (Cookie e name val))
        (Attribute (Cookie e name val) Request))
forall a b. (a -> b) -> a -> b
$ Text -> SecurityScheme -> OpenApi -> State Documentation OpenApi
forall (m :: * -> *).
MonadState Documentation m =>
Text -> SecurityScheme -> OpenApi -> m OpenApi
addSecurityScheme Text
cookieName SecurityScheme
securityScheme
    where
      cookieName :: Text
cookieName = forall a. IsString a => String -> a
fromString @Text (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy name -> String) -> Proxy name -> String
forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name

      securityScheme :: SecurityScheme
      securityScheme :: SecurityScheme
securityScheme =
        SecurityScheme
          { _securitySchemeType :: SecuritySchemeType
_securitySchemeType =
              ApiKeyParams -> SecuritySchemeType
SecuritySchemeApiKey
                ApiKeyParams
                  { _apiKeyName :: Text
_apiKeyName = Text
cookieName
                  , _apiKeyIn :: ApiKeyLocation
_apiKeyIn = ApiKeyLocation
ApiKeyCookie
                  }
          , _securitySchemeDescription :: Maybe Text
_securitySchemeDescription = Maybe Text
forall a. Maybe a
Nothing
          }

-- Response cookie information is not captured by OpenAPI

instance Set (OpenApiHandler m) (WG.SetCookie e name) where
  {-# INLINE setTrait #-}
  setTrait :: forall (ts :: [*]).
SetCookie e name
-> (With Response ts
    -> Response
    -> Attribute (SetCookie e name) Response
    -> With Response (SetCookie e name : ts))
-> OpenApiHandler
     m
     (With Response ts, Attribute (SetCookie e name) Response)
     (With Response (SetCookie e name : ts))
setTrait SetCookie e name
WG.SetCookie With Response ts
-> Response
-> Attribute (SetCookie e name) Response
-> With Response (SetCookie e name : ts)
_ = (OpenApi -> State Documentation OpenApi)
-> OpenApiHandler
     m
     (With Response ts, Attribute (SetCookie e name) Response)
     (With Response (SetCookie e name : ts))
forall {k} {k1} {k2} (m :: k) (a :: k1) (b :: k2).
(OpenApi -> State Documentation OpenApi) -> OpenApiHandler m a b
OpenApiHandler OpenApi -> State Documentation OpenApi
forall a. a -> StateT Documentation Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure