diff --git a/CHANGELOG.md b/CHANGELOG.md index 880c5a6..506c17e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Update readme to show how to use newtypes (#57 by @brodeuralexis and @JordanMartinez) ## [v10.0.1](https://github.com/purescript-contrib/purescript-routing/releases/tag/v10.0.1) - 2021-05-06 diff --git a/GUIDE.md b/GUIDE.md index e12e57f..fd93668 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -82,7 +82,7 @@ no interesting values we want to consume, we can use `<$` from `Prelude`. ```purescript postIndex :: Match MyRoute postIndex = - PostIndex <$ lit "posts + PostIndex <$ lit "posts" ``` Our next routes require extracting an integer `PostId`. @@ -345,3 +345,36 @@ main = do Right { foo, bar } -> ... Left errors -> ... ``` + +## Using `newtype` in routes + +Using the following routes with a `newtype`: +```purescript +newtype PostId = PostId Int +derive instance newtypePostId :: Newtype PostId _ + +data MyRoute + = PostIndex + | Post PostId + | PostEdit PostId + | PostBrowse String String +``` + +It is possible to wrap an `int` route parameter into a `PostId` using the +following function: +```purescript +postId :: forall f. MatchClass f => f PostId +postId = PostId <*> int +``` + +The created `postId` function can then be used like the `parser` function. +```purescript +myRoute :: Match MyRoute +myRoute = + root *> lit "posts" *> oneOf + [ PostEdit <$> postId <* lit "edit" + , Post <$> postId + , PostBrowse <$> (lit "browse" *> int) <*> str + , pure PostIndex + ] <* end +```