purescript-routing
A clean, type-safe routing library for PureScript.
client側のroutingの定義をする
以下の2つを提供する
applicative parsingするrouting定義用のmodule
各routingにアクセスしたときのevent発火させる用のmodule
2つ方法がある
hash base
push state
かなり親切でわかりやすい
web frameworkに依存しない小さなexample
with Halogen
どれも古いので、そのまま参考にしても動かない

Applicative parsingするrouting定義
/user/:userId
のURLを良い感じにparseする
そのためにApplicative parsingする
Routing.Match
にある
URLに使われる単語しかないのでparsingよりもかなりシンプル
lit
とか num
とか bool
とか、という関数が用意されてる
例を見ればすぐ理解できる
purs(hs)data MyRoute
= PostIndex
| Post PostId
| PostEdit PostId
| PostBrowse Int String
myRoute :: Match MyRoute
myRoute = oneOf
[ PostIndex <$ lit "posts" -- /posts
, Post <$> (lit "posts" *> int) -- /posts/:id
, PostEdit <$> (lit "posts" *> int) <* lit "edit" -- /posts/:postId/edit
, PostBrowse <$> (lit "posts" *> lit "browse" *> int) <*> str -- /posts/browse/:year/:month
]
tutorialにあるように、1つずつ関数定義して postIndex <|> post <|> ..
とやっても良いが、上のように書いたほうが簡潔
さらに /posts/
が被っている場合、こう書ける
purs(hs)myRoute :: Match MyRoute
myRoute =
root *> lit "posts" *> oneOf
[ PostEdit <$> int <* lit "edit"
, Post <$> int
, PostBrowse <$> (lit "browse" *> int) <*> str
, pure PostIndex
] <* end
上からパターンマッチされていく
最後に <* end
を付けているのは、 /post/piyo
みたいに存在しないpathにmatchさせないため
root
は、pathの先頭のslashにmatchする
/post
の
/
のこと

今は
myRoute
という名前だが、
postRoute
とかにすれば、他のやつと
<|>
で組み合わせて良い感じに出来そう

各routingにアクセスしたときのevent発火
Routing effects and events (Routing.Hash or Routing.PushState)
上で作ったRouterに対して、matchしたらevent発火するやつを作る
2種類ある
hash base
仕組みがいまいちわからんが、 #posts/
みたいなリンクを踏むと、定義したeventが実行される
push state
単にHalogen知識がないのと、参考になりそうなHalogen&purescript-routingが軒並み古くて型エラーが解決できないのでいったん保留
