Skip to content

Commit 929be9b

Browse files
authored
Merge pull request #16 from nsaunders/reqbody
Add ReqBody
2 parents 63c3af6 + b18b213 commit 929be9b

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Type/Trout.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Type.Trout
1212
, Named
1313
, QueryParam
1414
, QueryParams
15+
, ReqBody
1516
, type (:>)
1617
, type (:/)
1718
, type (:<|>)
@@ -76,6 +77,11 @@ data QueryParam (k :: Symbol) t
7677
-- | type of the value. `k` is the name of the key as a `Symbol`.
7778
data QueryParams (k :: Symbol) t
7879

80+
-- | Captures the request body. The `r` parameter is the type represented by
81+
-- | the request body (usually some type specific to the application domain),
82+
-- | and `cts` represents the content types supported.
83+
data ReqBody r cts
84+
7985
infixr 9 type Sub as :>
8086
infixr 9 type LitSub as :/
8187
infixr 8 type Named as :=

src/Type/Trout/ContentType.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Type.Trout.ContentType where
22

33
import Prelude
4+
import Data.Either (Either)
45
import Data.List.NonEmpty (NonEmptyList)
56
import Data.MediaType (MediaType)
67
import Data.Tuple (Tuple(..))
@@ -16,6 +17,9 @@ class HasMediaType ct where
1617
class MimeRender a ct b | a -> b, ct -> b where
1718
mimeRender :: Proxy ct -> a -> b
1819

20+
class MimeParse b ct a | b -> a, ct -> b where
21+
mimeParse :: Proxy ct -> b -> Either String a
22+
1923
-- | Renders a value of type `a`, as appropriate for each content type in `cts`,
2024
-- | as a non-empty list of values of type `b`, corresponding to the content
2125
-- | types. Content types can be a single type, e.g. `HTML`, or multiple types

src/Type/Trout/ContentType/JSON.purs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
module Type.Trout.ContentType.JSON where
22

33
import Prelude
4-
import Data.Argonaut (class EncodeJson, encodeJson)
4+
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson)
55
import Data.Argonaut.Core (stringify)
6+
import Data.Argonaut.Parser (jsonParser)
67
import Data.MediaType.Common (applicationJSON)
78
import Data.Tuple (Tuple(..))
8-
import Type.Trout.ContentType (class AllMimeRender, class HasMediaType, class MimeRender, getMediaType, mimeRender)
9+
import Type.Trout.ContentType
10+
( class AllMimeRender
11+
, class HasMediaType
12+
, class MimeParse
13+
, class MimeRender
14+
, getMediaType
15+
, mimeRender
16+
)
917

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

27+
instance mimeParseJson :: DecodeJson a => MimeParse String JSON a where
28+
mimeParse _ = jsonParser >=> decodeJson
29+
1930
instance allMimeRenderJson :: EncodeJson a => AllMimeRender a JSON String where
2031
allMimeRender p x = pure (Tuple (getMediaType p) (mimeRender p x))

0 commit comments

Comments
 (0)