@spa-tools
Core Router

The @spa-tools/core-router
package simplifies modern-day SPA routing, shedding all excess baggage without compromising functionality.
Feature highlights include:
- Dev-Defined Route Shape
- Dead-Simple Navigation
- Absolute Flow Control
- Succinct Options
- React (or not)
- TypeScript First
- Zero Dependencies
- Tree-shakable
Quickstart
It's highly advised to first checkout the @spa-tools documentation site for a complete list of features, usage scenarios, guidance, and reference.
Installation
npm install @spa-tools/core-router
Usage
Looking for React usage? See the docsite for an awesome React quickstart!
The first thing to do is create/define your routes.
import { CoreRoute, routesFactory } from '@spa-tools/core-router';
interface MyCustomRoute extends CoreRoute {
requiresAuth: boolean;
}
const createMyRoutes = routesFactory<MyCustomRoute>();
export const myRoutes = createMyRoutes({
dashboardRoute: {
path: '/',
requiresAuth: true,
},
financialsRoute: {
path: '/financials',
requiresAuth: true,
},
loginRoute: {
path: '/login',
requiresAuth: false,
},
signupRoute: {
path: '/signup',
requiresAuth: false,
},
});
Once you've created your routes, next you create your router.
import { CoreRouter } from '@spa-tools/core-router';
import { myRoutes } from '..';
import { checkUserAuthentication } from '..';
export const myRouter = CoreRouter.initialize(myRoutes, {
onRouteRequest: async ({ newRoute, newState }) => {
if (newRoute.requiresAuth) {
const isUserAuthenticated = await checkUserAuthentication();
if (!isUserAuthenticated) {
return [
myRoutes.loginRoute,
{ fromRoute: newRoute, fromState: newState }
]
}
}
return true;
}
});
Now you can use your router from anywhere in your app to navigate to any route.
import { myRouter, myRoutes } from '..';
function navigateToFinanicals() {
myRouter.navigate(myRoutes.financialsRoute);
}
Docsite
View the @spa-tools documentation site for complete reference.
Contributing
If you're interested in contributing to @spa-tools, please first create an issue on the @spa-tools monorepo in GitHub
or comment on an already open issue. From there we can discuss the feature or bugfix you're interested in and how best to approach it.
Unit Test Coverage
All packages in @spa-tools require 100% unit test coverage. This is a condition for all PRs to be merged whether you're adding a new feature or fixing a bug.
License
All packages in @spa-tools are licensed under the MIT license. Copyright © 2024, Ryan Howard (rollercodester). All rights reserved.