1.0.1 โข Published 5 months ago
@riogz/router v1.0.1
@riogz/router
Modern, flexible and framework-agnostic router โ a continuation of router5 with improved architecture and TypeScript support.
โจ Key Features
- ๐ฏ Framework Agnostic โ works with any frameworks and libraries
- ๐ View/State Separation โ router processes instructions and outputs state updates
- ๐ Universal โ works on client and server
- ๐งฉ Modular Architecture โ use only what you need
- โก High Performance โ optimized algorithms and caching
- ๐ Type-Safe โ full TypeScript support
- ๐ก๏ธ Route Guards โ access control and transition validation
- ๐ Hierarchical Routes โ nested routes and dynamic segments
- ๐คนโโ๏ธ Compatable with Router5 โ use your existing router5 routes, plugins and middlewares
๐ Quick Start
Installation
# Core router
npm install @riogz/router
# For React applications
npm install @riogz/router @riogz/react-router
# For browser integration
npm install @riogz/router-plugin-browser
# For debugging
npm install @riogz/router-plugin-loggerBasic Setup
import createRouter from '@riogz/router'
import browserPlugin from '@riogz/router-plugin-browser'
const routes = [
{ name: 'home', path: '/' },
{ name: 'users', path: '/users' },
{ name: 'users.detail', path: '/:id' }
]
const router = createRouter(routes)
router.start()
// Navigation
router.navigate('users.detail', { id: '123' })With React
import React from 'react'
import { createRouter, RouterProvider, useRouter } from '@riogz/router'
import browserPlugin from '@riogz/router-plugin-browser'
const routes = [
{ name: 'home', path: '/' },
{ name: 'users', path: '/users' },
{ name: 'users.detail', path: '/:id' }
]
const router = createRouter(routes)
router.usePlugin(browserPlugin())
router.start()
function App() {
return (
<RouterProvider router={router}>
<Navigation />
<RouteNode nodeName="home">
<Home />
</RouteNode>
<RouteNode nodeName="users" children={Users} />
</RouterProvider>
)
}
function Navigation() {
const router = useRouter()
return (
<nav>
<button onClick={() => router.navigate('home')}>
Home
</button>
{ /* -or- */}
<Link routeName="users">Users</Link>
</nav>
)
}๐ Documentation
- Introduction
- Integration
- Advanced
- Route Guards TBD
- Plugins TBD
- Middleware TBD
- Transition Path TBD
- Route Helpers TBD
- Listeners plugin TBD
Examples
- Base examples are placed in the examples folder
- Advanced examples are placed in the Github: riogod/frontend-modules-mvvm
๐ฆ Package Ecosystem
Core Packages
| Package | Description | Version | Bundle |
|---|---|---|---|
| @riogz/router | Core router | ||
| @riogz/react-router | React integration with hooks and components |
Plugins
| Package | Description | Version | Bundle |
|---|---|---|---|
| @riogz/router-plugin-browser | Browser integration (History API, hash) | ||
| @riogz/router-plugin-logger | Transition logging for debugging | ||
| @riogz/router-plugin-persistent-params | Parameter persistence between transitions |
Utilities
| Package | Description | Version | Bundle |
|---|---|---|---|
| @riogz/router-helpers | Route manipulation utilities | ||
| @riogz/router-transition-path | Transition path computation |
๐ฏ Core Concepts
Hierarchical Routes
const routes = [
{ name: 'app', path: '/app' },
{ name: 'app.users', path: '/users' },
{ name: 'app.users.detail', path: '/:id' },
{ name: 'app.users.detail.edit', path: '/edit' }
]
// Resulting paths:
// app โ /app
// app.users โ /app/users
// app.users.detail โ /app/users/:id
// app.users.detail.edit โ /app/users/:id/editRoute Guards
const router = createRouter(routes, {
defaultRoute: 'home'
})
// Guard for authorization check
router.canActivate('admin', (toState, fromState) => {
return user.isAuthenticated && user.hasRole('admin')
})
// Async guard
router.canActivate('users.detail', async (toState) => {
const user = await api.getUser(toState.params.id)
return user.exists
})Middleware
// Transition logging
router.useMiddleware((toState, fromState) => {
console.log(`Transition: ${fromState?.name} โ ${toState.name}`)
})
// Analytics
router.useMiddleware((toState) => {
analytics.track('page_view', {
route: toState.name,
params: toState.params
})
})Route Node Lifecycle
const routes = [
{ name: 'app', path: '/app' },
{
name: 'app.users',
browserTitle: 'Users',
path: '/users',
onEnterNode: (toState, fromState, deps) => {
console.log('Entering users node')
},
onExitNode: (toState, fromState, deps) => {
console.log('Leaving users node')
},
onNodeInActiveChain: (toState, fromState, deps) => {
console.log('Users node is in the active chain')
},
children: [
{
name: 'app.users.detail',
path: '/:id'
}
]
}
]๐ค Compatibility
- Node.js: 14+
- TypeScript: 4.0+
- React: 17.0+ (for @riogz/react-router)
- Browsers: modern browsers with ES2018 support
๐ License
MIT ยฉ Vyacheslav Krasnyanskiy
๐ค Contributing
We welcome contributions from the community! Read the contributor's guide for detailed information on how to contribute to the project.