Skip to content

Commit c1f89fa

Browse files
committed
Add routeParam Function
1 parent 634c266 commit c1f89fa

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Guide/routing.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ instance HasPath PostsController where
255255
pathTo ShowPostAction { postId = Nothing, slug = Just slug } = "/posts/" <> slug
256256
```
257257

258+
### Helper Functions
259+
260+
The [`IHP.RouterSupport`](https://ihp.digitallyinduced.com/api-docs/IHP-RouterSupport.html) module includes helpers functions such as:
261+
262+
- [`parseUUID`](https://ihp.digitallyinduced.com/api-docs/IHP-RouterSupport.html#v:parseUUID) to parse and return an UUID
263+
264+
- [`parseId`](https://ihp.digitallyinduced.com/api-docs/IHP-RouterSupport.html#v:parseId) to parse an UUID, afterwards wraps it in an Id
265+
266+
- [`parseText`](https://ihp.digitallyinduced.com/api-docs/IHP-RouterSupport.html#v:parseText) to parse until the next `/` character
267+
268+
- [`routeParam`](https://ihp.digitallyinduced.com/api-docs/IHP-RouterSupport.html#v:routeParam) to parse route query parameters
269+
258270
### Real-World Example
259271

260272
Here is a real world example of a custom routing implementation for a custom Apple Web Service interface implemented at digitally induced:

IHP/RouterSupport.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CanRoute (..)
2525
, webSocketAppWithCustomPath
2626
, onlyAllowMethods
2727
, getMethod
28+
, routeParam
2829
) where
2930

3031
import qualified Prelude
@@ -63,6 +64,8 @@ import Data.Dynamic
6364
import IHP.Router.Types
6465
import IHP.WebSocket (WSApp)
6566
import GHC.TypeLits as T
67+
import IHP.Controller.Context
68+
import IHP.Controller.Param
6669

6770
class FrontController application where
6871
controllers :: (?applicationContext :: ApplicationContext, ?application :: application, ?context :: RequestContext) => [Parser (IO ResponseReceived)]
@@ -807,6 +810,26 @@ parseIntegerId queryVal = let
807810
in
808811
rawValue >>= Just . unsafeCoerce
809812

813+
-- | Parses and returns an integer
814+
-- parseRational :: (Integral a) => Parser a
815+
-- parseRational = Attoparsec.decimal
816+
817+
-- | Parses a route query parameter
818+
--
819+
-- __Example:__
820+
--
821+
-- > let showPost = do
822+
-- > string "/post"
823+
-- > let postId = routeParam "postId"
824+
-- > pure ShowPostAction { .. }
825+
-- Will parse the `postId` query in `/post?postId=09b545dd-9744-4ef8-87b8-8d227f4faa1e`
826+
--
827+
routeParam :: (?context::RequestContext, ParamReader paramType) => ByteString -> paramType
828+
routeParam paramName =
829+
let requestContext = ?context
830+
in
831+
let ?context = FrozenControllerContext { requestContext = requestContext, customFields = mempty }
832+
in param paramName
810833

811834
-- | Display a better error when the user missed to pass an argument to an action.
812835
--

0 commit comments

Comments
 (0)