@@ -0,0 +1,33 @@
1+module Servant.Extra.API.Cookie where
2+
3+import Control.Lens
4+import Data.OpenApi as OA
5+import Data.Proxy
6+import Data.Text (Text)
7+import qualified Data.Text as T
8+import GHC.TypeLits
9+import Servant.API as S
10+import Servant.OpenApi
11+import Servant.OpenApi.Internal
12+
13+newtype Cookie (name :: Symbol) = Cookie (Maybe Text)
14+ deriving (Eq, FromHttpApiData, ToHttpApiData)
15+
16+instance OA.ToParamSchema (Cookie name) where
17+ toParamSchema _ = toParamSchema (Proxy @Text)
18+
19+instance HasLink sub => HasLink (Cookie name :> sub) where
20+ type MkLink (Cookie name :> sub) endpoint = MkLink sub endpoint
21+ toLink f _p l = toLink f (Proxy @sub) l
22+
23+instance (HasOpenApi api, KnownSymbol name) =>
24+ HasOpenApi (Cookie name :> api) where
25+ toOpenApi _ = toOpenApi (Proxy @api)
26+ & addParam param
27+ & addDefaultResponse400 cookieNameText
28+ where
29+ cookieNameText = T.pack (symbolVal (Proxy @name))
30+ param = mempty
31+ & name .~ cookieNameText
32+ & in_ .~ OA.ParamCookie
33+ & required ?~ True