![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@spa-tools/core-router
Advanced tools
Simplifies modern-day SPA routing, shedding all excess baggage without compromising functionality.
The @spa-tools/core-router
package simplifies modern-day SPA routing, shedding all excess baggage without compromising functionality.
npm install @spa-tools/core-router
Looking for React usage? See the docsite for an awesome React quickstart!
import { CoreRoute, routesFactory } from '@spa-tools/core-router';
// adding custom properties to your routes is as simple
// as creating an interface that extends CoreRoute
interface MyCustomRoute extends CoreRoute {
requiresAuth: boolean;
}
// to create your routes, first generate a route factory function
const createMyRoutes = routesFactory<MyCustomRoute>();
// next define and create all of your routes, which you'll
// typically export to use throughout your app
export const myRoutes = createMyRoutes({
dashboardRoute: {
path: '/',
requiresAuth: true,
},
financialsRoute: {
path: '/financials',
requiresAuth: true,
},
loginRoute: {
path: '/login',
requiresAuth: false,
},
signupRoute: {
path: '/signup',
requiresAuth: false,
},
});
import { CoreRouter } from '@spa-tools/core-router';
import { myRoutes } from '..';
import { checkUserAuthentication } from '..';
export const myRouter = CoreRouter.initialize(myRoutes, {
//
// onRouteRequest is the callback you use to perform all
// of your flow control logic in one centalized location
//
// the controller scenarios are only limited by your
// imagination, but ultimately this is where you determine
// if the user's route request should be allowed to proceed
// or if it should be denied or perhaps even redirected.
//
onRouteRequest: async ({ newRoute, newState }) => {
// here we use our custom requiresAuth property to check
// if the user must first be authenticated before allowing
// navigation to the requested route
if (newRoute.requiresAuth) {
// here we call a so-called async function to determine
// if the user is authenticated
const isUserAuthenticated = await checkUserAuthentication();
if (!isUserAuthenticated) {
// since the user is NOT authenticated, we request a
// redirect to the login route by returning a tuple with
// respective route along with state we want to pass along
return [
myRoutes.loginRoute,
{ fromRoute: newRoute, fromState: newState }
]
}
}
// here we ALLOW the route request by returning true; we also
// could've done nothing and by default the route request would
// be allowed to proceed
return true;
}
});
import { myRouter, myRoutes } from '..';
function navigateToFinanicals() {
myRouter.navigate(myRoutes.financialsRoute);
}
View the @spa-tools documentation site for complete reference.
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.
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.
All packages in @spa-tools are licensed under the MIT license. Copyright © 2024, Ryan Howard (rollercodester). All rights reserved.
FAQs
Simplifies modern-day SPA routing, shedding all excess baggage without compromising functionality.
The npm package @spa-tools/core-router receives a total of 1 weekly downloads. As such, @spa-tools/core-router popularity was classified as not popular.
We found that @spa-tools/core-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.