Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for newtype parameter in routes #57

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 34 additions & 1 deletion GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
```