Skip to content

Commit 14cc938

Browse files
Add example for newtype parameter in routes (#57)
Co-authored-by: JordanMartinez <jordanalex.martinez@protonmail.com>
1 parent 6236927 commit 14cc938

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ New features:
1111
Bugfixes:
1212

1313
Other improvements:
14+
- Update readme to show how to use newtypes (#57 by @brodeuralexis and @JordanMartinez)
1415

1516
## [v10.0.1](https://github.com/purescript-contrib/purescript-routing/releases/tag/v10.0.1) - 2021-05-06
1617

GUIDE.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ no interesting values we want to consume, we can use `<$` from `Prelude`.
8282
```purescript
8383
postIndex :: Match MyRoute
8484
postIndex =
85-
PostIndex <$ lit "posts
85+
PostIndex <$ lit "posts"
8686
```
8787

8888
Our next routes require extracting an integer `PostId`.
@@ -345,3 +345,36 @@ main = do
345345
Right { foo, bar } -> ...
346346
Left errors -> ...
347347
```
348+
349+
## Using `newtype` in routes
350+
351+
Using the following routes with a `newtype`:
352+
```purescript
353+
newtype PostId = PostId Int
354+
derive instance newtypePostId :: Newtype PostId _
355+
356+
data MyRoute
357+
= PostIndex
358+
| Post PostId
359+
| PostEdit PostId
360+
| PostBrowse String String
361+
```
362+
363+
It is possible to wrap an `int` route parameter into a `PostId` using the
364+
following function:
365+
```purescript
366+
postId :: forall f. MatchClass f => f PostId
367+
postId = PostId <*> int
368+
```
369+
370+
The created `postId` function can then be used like the `parser` function.
371+
```purescript
372+
myRoute :: Match MyRoute
373+
myRoute =
374+
root *> lit "posts" *> oneOf
375+
[ PostEdit <$> postId <* lit "edit"
376+
, Post <$> postId
377+
, PostBrowse <$> (lit "browse" *> int) <*> str
378+
, pure PostIndex
379+
] <* end
380+
```

0 commit comments

Comments
 (0)