kruschid / typesafe-routes

Spices up your favorite routing library by adding type safety to plain string-based route definitions.
https://kruschid.github.io/typesafe-routes/
MIT License
102 stars 8 forks source link

Proper way to compose routes #51

Open Xample opened 4 months ago

Xample commented 4 months ago

Hello, in our project we do organise feature folders with all its dependencies so that we can share a feature folder between projects. Something like that:

import { createRoutes, int, RouteNodeMap } from 'typesafe-routes';

// feature/user.route.ts folder
export const Users = {
    list: {
        path: ['list'],
    },
    detail: {
        path: ['detail', int('uid')],
  },
} satisfies RouteNodeMap;

// feature/cart.route.ts folder
export const Cart = {
  detail: {
    path: ['detail'],
  },
} satisfies RouteNodeMap;

// app.route.ts folder
export const routes = createRoutes({
    user: {
        path: ['user'],
        children: Users,
    },
    cart: {
      path: ['cart'],
      children: Cart,
  },
});

I expected to be able to put a routeContext into another routeContext but it seems no to be possible (and there is no toRouteNodeMap converter (?)) so is there another way to compose routes or we are doing it the right way ?

Xample commented 4 months ago

ok, to what I've seen, I should export the RouteNodeMap from my module which could be imported and included within the RouteNodeMap of another module and then always use the createRoutes to create / parse urls

kruschid commented 2 weeks ago

ok, to what I've seen, I should export the RouteNodeMap from my module which could be imported and included within the RouteNodeMap of another module and then always use the createRoutes to create / parse urls

Right, this is one possible method.

I've written about composing routes on this documentation page. It could be an interesting alternative. I hope you don't mind me using your scenario as inspiration for my example there.