lapwinglabs / enroute

tiny functional router
99 stars 6 forks source link

Routes as middleware: fall-through logic #4

Open timoxley opened 7 years ago

timoxley commented 7 years ago

It'd be neat to be able to trigger fall-through logic, middleware style.

e.g. attach a handler for /users/:userID that will validate :userID and or hydrate the user info which could be consumed in nested /users/:userID/* routes.

Express's params handler stuff is also kinda neat for centralising this kind of logic: http://expressjs.com/en/4x/api.html#app.param

Basically afaict just some way to toggle whether this is a return or a continue: https://github.com/lapwinglabs/enroute/blob/36300b8dfe5ce0545daf055bb8adefde667b9891/index.js#L32-L33

Just a thought.

matthewmueller commented 7 years ago

Hey @timoxley, thanks for stopping by :-)

Would the hydration step be async or sync? I think the way that I normally approach this with the sync version is with nested routers.

Something like this:

function App (props) {
  return Route({
    '/users/:userID': (params) => UserView(Object.assign(params, props))
  }, props.url)
}

function UserView (props) {
  var url = userRoutingCondition(props)
  return Route({
    '/users/:userID/settings': () => Settings(props),
    '/users/:userID/profile': () => Profile(props)
  }, url)
}

Does that solve your use case?

timoxley commented 7 years ago

Would the hydration step be async or sync?

Sync does makes things easier but it's not always possible.

I don't mind that nested router pattern, nice, it's similar to express's ability to mount sub Router objects. How do you think that could that work for async?