1.0.1 โ€ข Published 5 months ago

@riogz/router v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

npm version CI Deploy License: MIT gzip

@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-logger

Basic 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

๐Ÿ“ฆ Package Ecosystem

Core Packages

PackageDescriptionVersionBundle
@riogz/routerCore routernpmgzip
@riogz/react-routerReact integration with hooks and componentsnpmgzip

Plugins

PackageDescriptionVersionBundle
@riogz/router-plugin-browserBrowser integration (History API, hash)npmgzip
@riogz/router-plugin-loggerTransition logging for debuggingnpmgzip
@riogz/router-plugin-persistent-paramsParameter persistence between transitionsnpmgzip

Utilities

PackageDescriptionVersionBundle
@riogz/router-helpersRoute manipulation utilitiesnpmgzip
@riogz/router-transition-pathTransition path computationnpmgzip

๐ŸŽฏ 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/edit

Route 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.

๐Ÿ”— Links

1.0.1

5 months ago

1.0.0

5 months ago

0.0.16

5 months ago

0.0.15

5 months ago

0.0.13

5 months ago

0.0.12

5 months ago

0.0.4

5 months ago

0.0.3

6 months ago

0.0.2

6 months ago