route-configurator
v1.0.0
Published
Configuration-based React Router provider with privilege filtering and feature toggles
Maintainers
Readme
Router Provider Library
Configuration-based React Router provider with privilege filtering, feature toggles, and state-based routing.
Installation
npm install route-configurator react react-dom react-router react-router-dom mobx mobx-react lodashUsage
Basic Setup
import { RouterProvider } from "route-configurator";
import type { IRoute, TFeatureEnabledChecker } from "route-configurator";
// Define your routes
const routes: IRoute[] = [
{
key: "dashboard",
path: "/dashboard",
component: Dashboard,
privileges: ["DASHBOARD_VIEW"],
},
{
key: "settings",
path: "/settings",
component: Settings,
somePrivileges: ["SETTINGS_READ", "SETTINGS_WRITE"],
},
];
// Create feature checker function
const isFeatureEnabled: TFeatureEnabledChecker = (key, options) => {
// Your logic to check privileges
return userPrivileges.includes(key);
};
// Render provider
<RouterProvider
routesConfig={routes}
unAuthorizedRoutes={loginRoutes}
isAuthorizedUser={isAuthorized}
layout={MainLayout}
isFeatureEnabled={isFeatureEnabled}
systemLoader={YourSystemLoader}
/>;API
RouterProvider Props
routesConfig: IRoute[]- Main routes configurationunAuthorizedRoutes: IRoute[]- Routes for unauthorized users (login, signup, etc.)isAuthorizedUser: boolean- User authorization statelayout: React.ElementType- Layout component wrapperisFeatureEnabled: TFeatureEnabledChecker- Function to check privileges/featuressystemLoader?: React.ElementType- Optional system loader component (default: no-op)
IRoute Interface
Extends react-router's NonIndexRouteObject with:
key?: string- Unique route identifierpath?: string- Route pathoriginalPath?: string- Original path before transformationexact?: boolean- Exact path matchingloc?: TLocalizationDescription- Menu localizationredirect?: string- Redirect URLpriority?: number- Display order (higher = first)routes?: IRoute[]- Nested routesprivileges?: (string | Parameters)[]- Required privileges (all must match)somePrivileges?: (string | Parameters)[]- Optional privileges (one must match)isRedirectRoute?: boolean- Is redirect-only routechildRoutesFilter?: Function- Custom filter functionisLayoutRoute?: boolean- Is layout wrappericon?: React.ElementType- Icon componentcomponent?: React.ComponentType- Route componentisBeta?: boolean- Beta feature flag
Utilities
getRoutes(items)- Prepare routes for renderingsortPriority(routes)- Sort by priority (recursive)resolveConstraintsInRoutes(routes, isFeatureEnabled, location)- Filter by privilegesroutesMap(routes)- Flatten hierarchical routesgetBreadcrumbs(path)- Generate breadcrumb trailgetActiveRouteKeys(routes, path)- Get active route keysremoveModulesLayer(routes)- Remove module grouping layer
Features
✅ Configuration-based routing
✅ Privilege-based filtering
✅ Feature toggle support
✅ State-based access (auth, initialization)
✅ Hierarchical route support
✅ Breadcrumb generation
✅ Custom route filters
✅ Zero external dependencies (except peer deps)
