fenok / react-router-typesafe-routes

Comprehensive and extensible type-safe routes for React Router v6 with first-class support for nested routes and param validation.
MIT License
145 stars 3 forks source link

Can this be shared between a backend and frontend app? #42

Closed giopetris closed 1 year ago

giopetris commented 1 year ago

My current setup is a monorepo where the URLs are shared between frontend app (using react-router) and a backend app (using node), my concern is that since this relies on react-router it will not work on the backend app, or am I missing something?

fenok commented 1 year ago

It will work, but, depending on how routes are used on the backend, it might be inconvenient, because the API is designed for React Router. If you need something more generic, I recommend Typesafe Routes.

If the API is suitable for your use case, there aren't really any other concerns. Hooks aside, only two functions from React Router are used: createSearchParams and generatePath.

In fact, you can even use the library without any peer dependencies, if you use the root entry point. Hooks aside, the API is the same, but, instead of route, you get createRoute, and you can construct route yourself:

import { createRoute } from "react-router-typesafe-routes";
import { createSearchParams, generatePath } from "./custom-implementation";

const route = createRoute({ createSearchParams, generatePath });

It's not documented, but it's technically a part of public API.

WARNING: For demonstration purposes only, I don't think it makes practical sense to do this.

giopetris commented 1 year ago

Thanks so much @fenok !