git.iko.soy/typeable/servant
on master help sign in
Move accept handling into servant-server.
commit98e02ea7cf1710a2e1594ceb746e96e593e631ae authorJulian K. Arni <jkarni@gmail.com> dateMon 12 Jan 2015, 15:09 (12 years ago) parent8930a45
2 files changed+6−49
src/Servant/API.hs+1−1 src/Servant/API/ContentTypes.hs+5−48
src/Servant/API.hs+1−1
@@ -46,7 +46,7 @@ module Servant.API (
4646
4747import Servant.API.Alternative ( (:<|>)(..) )
4848import Servant.API.Capture ( Capture )
49−import Servant.API.ContentTypes ( Accept(..), MimeRender(..), HTML, XML, JSON )
49+import Servant.API.ContentTypes ( HTML, XML, JSON, JavaScript, CSS, PlainText )
5050import Servant.API.Delete ( Delete )
5151import Servant.API.Get ( Get )
5252import Servant.API.Header ( Header )
src/Servant/API/ContentTypes.hs+5−48
@@ -1,55 +1,12 @@
1−{-# LANGUAGE DataKinds #-}
21{-# LANGUAGE DeriveDataTypeable #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE PolyKinds #-}
7−{-# LANGUAGE ScopedTypeVariables #-}
8−{-# LANGUAGE TypeOperators #-}
92module Servant.API.ContentTypes where
103
11−import Data.ByteString (ByteString)
12−import Data.Proxy (Proxy(..))
13−import Data.Typeable (Typeable)
4+import Data.Typeable
5+
146
157data XML deriving Typeable
168data HTML deriving Typeable
179data JSON deriving Typeable
18−
19−type ContentTypeBS = ByteString
20−
21−class Accept ctype where
22− isContentType :: Proxy ctype -> ByteString -> Bool
23− contentType :: Proxy ctype -> ContentTypeBS
24− isContentType p bs = bs == contentType p
25−
26−instance Accept HTML where
27− contentType _ = "text/html"
28−
29−instance Accept JSON where
30− contentType _ = "application/json"
31−
32−instance Accept XML where
33− contentType _ = "application/xml"
34−
35−-- | Instantiate this class to register a way of serializing a type based
36−-- on the @Accept@ header.
37−class Accept ctype => MimeRender ctype a where
38− toByteString :: Proxy ctype -> a -> ByteString
39−
40−
41−class AllCTRender list a where
42− handleAcceptH :: Proxy list -> ContentTypeBS -> a -> (ByteString, ContentTypeBS)
43−
44−instance MimeRender ctyp a => AllCTRender '[ctyp] a where
45− handleAcceptH _ accept val = (toByteString pctyp val, accept)
46− where pctyp = Proxy :: Proxy ctyp
47−
48−instance ( MimeRender ctyp a
49− , AllCTRender ctyps a
50− ) => AllCTRender (ctyp ': ctyps) a where
51− handleAcceptH _ accept val
52− | isContentType pctyp accept = (toByteString pctyp val, accept)
53− | otherwise = handleAcceptH pctyps accept val
54− where pctyp = Proxy :: Proxy ctyp
55− pctyps = Proxy :: Proxy ctyps
10+data JavaScript deriving Typeable
11+data CSS deriving Typeable
12+data PlainText deriving Typeable