Skip to content

Commit f1a8486

Browse files
committed
feat: add route helper utilities for simplified route management
1 parent ab65e6b commit f1a8486

File tree

8 files changed

+862
-99
lines changed

8 files changed

+862
-99
lines changed

public/js/README-route-helpers.md

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# Route Helper Utilities
2+
3+
This document explains how to use the route helper utilities to simplify adding and managing routes in the SPA application.
4+
5+
## Overview
6+
7+
The route helper utilities provide a more concise and maintainable way to define routes for the SPA router. They handle common patterns like authentication checks, subscription requirements, and page initialization.
8+
9+
## Key Features
10+
11+
- **Simplified Route Definition**: Define routes with minimal code
12+
- **Authentication Handling**: Built-in support for protected routes
13+
- **Subscription Checks**: Easy way to require active subscriptions for premium routes
14+
- **Modular Route Organization**: Support for organizing routes in separate files
15+
- **Consistent Route Behavior**: Standardized handling of route transitions and guards
16+
17+
## Usage
18+
19+
### Basic Route Definition
20+
21+
The simplest way to define a route is to provide just the path to the view HTML file:
22+
23+
```javascript
24+
import { createRoutes } from './route-helpers.js';
25+
26+
const routes = createRoutes({
27+
'/about': '/views/about.html'
28+
});
29+
```
30+
31+
### Route with Initialization Function
32+
33+
To run code after a route is rendered, use the `afterRender` option:
34+
35+
```javascript
36+
const routes = createRoutes({
37+
'/profile': {
38+
viewPath: '/views/profile.html',
39+
afterRender: () => initProfilePage()
40+
}
41+
});
42+
```
43+
44+
### Protected Routes
45+
46+
To require authentication for a route, use the `requireAuth` option:
47+
48+
```javascript
49+
const routes = createRoutes({
50+
'/dashboard': {
51+
viewPath: '/views/dashboard.html',
52+
requireAuth: true
53+
}
54+
});
55+
```
56+
57+
### Premium Routes
58+
59+
To require an active subscription, use the `requireSubscription` option:
60+
61+
```javascript
62+
const routes = createRoutes({
63+
'/premium-feature': {
64+
viewPath: '/views/premium-feature.html',
65+
requireSubscription: true
66+
}
67+
});
68+
```
69+
70+
### Custom Route Guards
71+
72+
For more complex access control, use the `beforeEnter` option:
73+
74+
```javascript
75+
const routes = createRoutes({
76+
'/admin': {
77+
viewPath: '/views/admin.html',
78+
beforeEnter: (to, from, next) => {
79+
const userRole = localStorage.getItem('user_role');
80+
if (userRole === 'admin') {
81+
next();
82+
} else {
83+
next('/access-denied');
84+
}
85+
}
86+
}
87+
});
88+
```
89+
90+
## Organizing Routes
91+
92+
For larger applications, you can organize routes in separate files:
93+
94+
### Feature-specific Routes
95+
96+
```javascript
97+
// feature-routes.js
98+
import { createRoutes } from './route-helpers.js';
99+
import { initFeaturePage } from './page-initializers.js';
100+
101+
export const featureRoutes = createRoutes({
102+
'/feature': {
103+
viewPath: '/views/feature.html',
104+
afterRender: initFeaturePage
105+
},
106+
'/feature/settings': {
107+
viewPath: '/views/feature-settings.html',
108+
requireAuth: true
109+
}
110+
});
111+
```
112+
113+
### Merging Routes
114+
115+
```javascript
116+
// router.js
117+
import { createRoutes } from './route-helpers.js';
118+
import { featureRoutes } from './feature-routes.js';
119+
import { adminRoutes } from './admin-routes.js';
120+
121+
export function defineRoutes(router) {
122+
// Define core routes
123+
const coreRoutes = createRoutes({
124+
'/': '/views/home.html',
125+
'/login': {
126+
viewPath: '/views/login.html',
127+
afterRender: initLoginPage
128+
}
129+
});
130+
131+
// Merge all routes
132+
const routes = {
133+
...coreRoutes,
134+
...featureRoutes,
135+
...adminRoutes
136+
};
137+
138+
// Register routes with the router
139+
router.registerRoutes(routes);
140+
router.init();
141+
142+
return router;
143+
}
144+
```
145+
146+
## Adding a New Route
147+
148+
To add a new route to the application:
149+
150+
1. Create the HTML view file in the `/views` directory
151+
2. If needed, create an initialization function in `page-initializers.js`
152+
3. Add the route to the routes object in `router.js` using the `createRoutes` helper
153+
154+
Example:
155+
156+
```javascript
157+
// In router.js
158+
const routes = createRoutes({
159+
// ... existing routes
160+
161+
// Add your new route
162+
'/new-feature': {
163+
viewPath: '/views/new-feature.html',
164+
afterRender: initNewFeaturePage,
165+
requireAuth: true // If the route requires authentication
166+
}
167+
});
168+
```
169+
170+
For more detailed examples, see the `examples/add-new-route-example.js` file.
171+
172+
## API Reference
173+
174+
### `createRoute(viewPath, options)`
175+
176+
Creates a single route configuration object.
177+
178+
Parameters:
179+
- `viewPath`: Path to the view HTML file
180+
- `options`: Route options
181+
- `afterRender`: Function to run after rendering
182+
- `beforeEnter`: Function to run before entering route
183+
- `requireAuth`: Whether the route requires authentication
184+
- `requireSubscription`: Whether the route requires an active subscription
185+
186+
### `createRoutes(routeDefinitions)`
187+
188+
Creates multiple route configuration objects.
189+
190+
Parameters:
191+
- `routeDefinitions`: Object mapping paths to route definitions
192+
- Keys are route paths
193+
- Values can be:
194+
- String: Path to view HTML file
195+
- Object: Route configuration with `viewPath` and options
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Example: Adding the Feature Example Route
3+
*
4+
* This file demonstrates how to add the feature example route to the router.
5+
* In a real implementation, you would modify the router.js file directly.
6+
*/
7+
import { createRoutes } from '../route-helpers.js';
8+
import { initFeatureExamplePage } from './feature-example-initializer.js';
9+
10+
/**
11+
* Step 1: Add the initializer function to page-initializers.js
12+
*
13+
* In a real implementation, you would add the initFeatureExamplePage function
14+
* to the page-initializers.js file and export it.
15+
*/
16+
17+
/**
18+
* Step 2: Add the route to the router.js file
19+
*
20+
* In the defineRoutes function in router.js, add the new route to the createRoutes call:
21+
*/
22+
23+
// Example of how the routes object would look with the new route added
24+
const routes = createRoutes({
25+
// Existing routes...
26+
'/': '/views/home.html',
27+
'/login': {
28+
viewPath: '/views/login.html',
29+
afterRender: () => {} // initLoginPage in the real implementation
30+
},
31+
32+
// Add the new feature example route
33+
'/feature-example': {
34+
viewPath: '/views/feature-example.html',
35+
afterRender: initFeatureExamplePage,
36+
requireAuth: true // If the route requires authentication
37+
}
38+
});
39+
40+
/**
41+
* Step 3: Test the new route
42+
*
43+
* After adding the route, you can test it by navigating to /feature-example in the browser.
44+
* You should see the feature example page with the form, and the initializer should run.
45+
*/
46+
47+
/**
48+
* Complete Example: Adding a New Route
49+
*
50+
* Here's a complete example of the process to add a new route:
51+
*
52+
* 1. Create the HTML view file in /views/your-feature.html
53+
* 2. Create an initializer function in page-initializers.js:
54+
*
55+
* export function initYourFeaturePage() {
56+
* // Initialize the page
57+
* }
58+
*
59+
* 3. Add the route to the routes object in router.js:
60+
*
61+
* const routes = createRoutes({
62+
* // Existing routes...
63+
*
64+
* '/your-feature': {
65+
* viewPath: '/views/your-feature.html',
66+
* afterRender: initYourFeaturePage,
67+
* requireAuth: true // If needed
68+
* }
69+
* });
70+
*
71+
* That's it! The route is now available at /your-feature
72+
*/
73+
74+
// This is just an example - don't export anything
75+
console.log('Example of adding a new route');

0 commit comments

Comments
 (0)