honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
16.99k stars 472 forks source link

Look into adding type safe routes instead of stringly types params. #2561

Open seivan opened 2 months ago

seivan commented 2 months ago

What is the feature you are proposing?

Routes could look like this:

const user = defineRoute(
  {
    userId: param.path.string,
  },
  (p) => `/users/${p.userId}`
);

const { routes } = createRouter({
  home: defineRoute("/"),
  post: defineRoute({ postId: param.path.string }, (p) => `/post/${p.postId}`),
  about: defineRoute("/about"),
  user,
  userSettings: user.extend("/settings"),
  userActivity: user.extend("/activity"),
});

This have two benefits, inferring data from the route means you can straight up pass it to a component directly. The other benefit is when you're creating a link and need to pass it params, you get

<a
  href={routes.user({userId: "2").href}link</a>

And programatically redirecting would also be supported with types

  routes.post({ postId: "abc" }).push();
yusukebe commented 2 months ago

Hi @seivan

Currently, Hono has the RPC feature that can enable the same purpose. So, I don't want to implement it now.