Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Type/Trout.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Type.Trout
, Named
, QueryParam
, QueryParams
, ReqBody
, type (:>)
, type (:/)
, type (:<|>)
Expand Down Expand Up @@ -76,6 +77,11 @@ data QueryParam (k :: Symbol) t
-- | type of the value. `k` is the name of the key as a `Symbol`.
data QueryParams (k :: Symbol) t

-- | Captures the request body. The `r` parameter is the type represented by
-- | the request body (usually some type specific to the application domain),
-- | and `cts` represents the content types supported.
data ReqBody r cts

infixr 9 type Sub as :>
infixr 9 type LitSub as :/
infixr 8 type Named as :=
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Trout/ContentType.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Type.Trout.ContentType where

import Prelude
import Data.Either (Either)
import Data.List.NonEmpty (NonEmptyList)
import Data.MediaType (MediaType)
import Data.Tuple (Tuple(..))
Expand All @@ -16,6 +17,9 @@ class HasMediaType ct where
class MimeRender a ct b | a -> b, ct -> b where
mimeRender :: Proxy ct -> a -> b

class MimeParse b ct a | b -> a, ct -> b where
mimeParse :: Proxy ct -> b -> Either String a

-- | Renders a value of type `a`, as appropriate for each content type in `cts`,
-- | as a non-empty list of values of type `b`, corresponding to the content
-- | types. Content types can be a single type, e.g. `HTML`, or multiple types
Expand Down
15 changes: 13 additions & 2 deletions src/Type/Trout/ContentType/JSON.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module Type.Trout.ContentType.JSON where

import Prelude
import Data.Argonaut (class EncodeJson, encodeJson)
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson)
import Data.Argonaut.Core (stringify)
import Data.Argonaut.Parser (jsonParser)
import Data.MediaType.Common (applicationJSON)
import Data.Tuple (Tuple(..))
import Type.Trout.ContentType (class AllMimeRender, class HasMediaType, class MimeRender, getMediaType, mimeRender)
import Type.Trout.ContentType
( class AllMimeRender
, class HasMediaType
, class MimeParse
, class MimeRender
, getMediaType
, mimeRender
)

-- | A content type, corresponding to the `application/json` media type.
data JSON
Expand All @@ -16,5 +24,8 @@ instance hasMediaTypeJson :: HasMediaType JSON where
instance mimeRenderJson :: EncodeJson a => MimeRender a JSON String where
mimeRender _ = stringify <<< encodeJson

instance mimeParseJson :: DecodeJson a => MimeParse String JSON a where
mimeParse _ = jsonParser >=> decodeJson

instance allMimeRenderJson :: EncodeJson a => AllMimeRender a JSON String where
allMimeRender p x = pure (Tuple (getMediaType p) (mimeRender p x))