{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
module Docs.Rendered
( PDFBytes (..)
, ZipBytes (..)
, HTMLBytes (..)
, PDF
, Zip
, HTML
)
where
import Data.ByteString.Lazy (ByteString)
import Data.OpenApi (NamedSchema (NamedSchema), ToSchema (declareNamedSchema))
import Data.OpenApi.ParamSchema (binarySchema)
import Data.Text (Text)
import Network.HTTP.Media ((//))
import Servant (Accept (contentType), MimeRender (mimeRender))
newtype PDFBytes
= PDFBytes
{ PDFBytes -> ByteString
unPDFBytes :: ByteString
}
newtype ZipBytes
= ZipBytes
{ ZipBytes -> ByteString
unZipBytes :: ByteString
}
newtype HTMLBytes
= HTMLBytes
{ HTMLBytes -> ByteString
unHTMLBytes :: ByteString
}
instance ToSchema PDFBytes where
declareNamedSchema :: Proxy PDFBytes -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy PDFBytes
_ = Text -> Declare (Definitions Schema) NamedSchema
forall (f :: * -> *). Applicative f => Text -> f NamedSchema
declareBinarySchema Text
"PDF Bytes"
instance ToSchema ZipBytes where
declareNamedSchema :: Proxy ZipBytes -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy ZipBytes
_ = Text -> Declare (Definitions Schema) NamedSchema
forall (f :: * -> *). Applicative f => Text -> f NamedSchema
declareBinarySchema Text
"Zip Bytes"
instance ToSchema HTMLBytes where
declareNamedSchema :: Proxy HTMLBytes -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy HTMLBytes
_ = Text -> Declare (Definitions Schema) NamedSchema
forall (f :: * -> *). Applicative f => Text -> f NamedSchema
declareBinarySchema Text
"HTML Bytes"
declareBinarySchema :: (Applicative f) => Text -> f NamedSchema
declareBinarySchema :: forall (f :: * -> *). Applicative f => Text -> f NamedSchema
declareBinarySchema Text
name = NamedSchema -> f NamedSchema
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NamedSchema -> f NamedSchema) -> NamedSchema -> f NamedSchema
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Schema -> NamedSchema
NamedSchema (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
name) Schema
binarySchema
data PDF
data Zip
data HTML
instance Accept PDF where
contentType :: Proxy PDF -> MediaType
contentType Proxy PDF
_ = ByteString
"application" ByteString -> ByteString -> MediaType
// ByteString
"pdf"
instance Accept Zip where
contentType :: Proxy Zip -> MediaType
contentType Proxy Zip
_ = ByteString
"application" ByteString -> ByteString -> MediaType
// ByteString
"zip"
instance Accept HTML where
contentType :: Proxy HTML -> MediaType
contentType Proxy HTML
_ = ByteString
"text" ByteString -> ByteString -> MediaType
// ByteString
"plain"
instance MimeRender PDF PDFBytes where
mimeRender :: Proxy PDF -> PDFBytes -> ByteString
mimeRender Proxy PDF
_ = PDFBytes -> ByteString
unPDFBytes
instance MimeRender Zip ZipBytes where
mimeRender :: Proxy Zip -> ZipBytes -> ByteString
mimeRender Proxy Zip
_ = ZipBytes -> ByteString
unZipBytes
instance MimeRender HTML HTMLBytes where
mimeRender :: Proxy HTML -> HTMLBytes -> ByteString
mimeRender Proxy HTML
_ = HTMLBytes -> ByteString
unHTMLBytes