@liteforge/router 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +296 -0
- package/dist/components.d.ts +75 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +475 -0
- package/dist/components.js.map +1 -0
- package/dist/guards.d.ts +103 -0
- package/dist/guards.d.ts.map +1 -0
- package/dist/guards.js +268 -0
- package/dist/guards.js.map +1 -0
- package/dist/history.d.ts +33 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +315 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/lazy.d.ts +197 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +458 -0
- package/dist/lazy.js.map +1 -0
- package/dist/middleware.d.ts +69 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +215 -0
- package/dist/middleware.js.map +1 -0
- package/dist/route-matcher.d.ts +80 -0
- package/dist/route-matcher.d.ts.map +1 -0
- package/dist/route-matcher.js +482 -0
- package/dist/route-matcher.js.map +1 -0
- package/dist/router.d.ts +26 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +393 -0
- package/dist/router.js.map +1 -0
- package/dist/types.d.ts +459 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SchildW3rk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
# @liteforge/router
|
|
2
|
+
|
|
3
|
+
Full-featured router for LiteForge with guards, middleware, and lazy loading.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @liteforge/router @liteforge/core @liteforge/runtime
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependencies: `@liteforge/core >= 0.1.0`, `@liteforge/runtime >= 0.1.0`
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
`@liteforge/router` provides client-side routing with route guards, middleware, nested routes, lazy loading, and full TypeScript support.
|
|
16
|
+
|
|
17
|
+
## Basic Usage
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { createRouter, RouterOutlet, Link } from '@liteforge/router'
|
|
21
|
+
import { createApp } from '@liteforge/runtime'
|
|
22
|
+
|
|
23
|
+
const router = createRouter({
|
|
24
|
+
routes: [
|
|
25
|
+
{ path: '/', component: Home },
|
|
26
|
+
{ path: '/about', component: About },
|
|
27
|
+
{ path: '/users/:id', component: UserDetail }
|
|
28
|
+
]
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const App = () => (
|
|
32
|
+
<div>
|
|
33
|
+
<nav>
|
|
34
|
+
<Link href="/">Home</Link>
|
|
35
|
+
<Link href="/about">About</Link>
|
|
36
|
+
</nav>
|
|
37
|
+
<RouterOutlet />
|
|
38
|
+
</div>
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
createApp({ router }).mount(App)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### createRouter
|
|
47
|
+
|
|
48
|
+
Creates a router instance.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createRouter } from '@liteforge/router'
|
|
52
|
+
|
|
53
|
+
const router = createRouter({
|
|
54
|
+
routes: [...],
|
|
55
|
+
|
|
56
|
+
// Optional settings
|
|
57
|
+
base: '/app', // Base path for all routes
|
|
58
|
+
hashMode: false, // Use hash-based routing (#/path)
|
|
59
|
+
scrollBehavior: 'auto', // 'auto' | 'smooth' | 'none'
|
|
60
|
+
|
|
61
|
+
// Global guards
|
|
62
|
+
guards: {
|
|
63
|
+
auth: async (ctx) => {
|
|
64
|
+
if (!isLoggedIn()) return '/login'
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
// Global middleware
|
|
70
|
+
middleware: [loggerMiddleware]
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Route Definition
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const routes = [
|
|
78
|
+
// Basic route
|
|
79
|
+
{ path: '/', component: Home },
|
|
80
|
+
|
|
81
|
+
// With name (for programmatic navigation)
|
|
82
|
+
{ path: '/about', name: 'about', component: About },
|
|
83
|
+
|
|
84
|
+
// Dynamic parameters
|
|
85
|
+
{ path: '/users/:id', component: UserDetail },
|
|
86
|
+
|
|
87
|
+
// Optional parameters
|
|
88
|
+
{ path: '/posts/:id?', component: Posts },
|
|
89
|
+
|
|
90
|
+
// Wildcard (catch-all)
|
|
91
|
+
{ path: '/docs/*', component: DocsPage },
|
|
92
|
+
|
|
93
|
+
// With guard
|
|
94
|
+
{ path: '/admin', component: Admin, guard: 'auth' },
|
|
95
|
+
|
|
96
|
+
// Multiple guards
|
|
97
|
+
{ path: '/admin', component: Admin, guard: ['auth', 'role:admin'] },
|
|
98
|
+
|
|
99
|
+
// Nested routes
|
|
100
|
+
{
|
|
101
|
+
path: '/dashboard',
|
|
102
|
+
component: DashboardLayout,
|
|
103
|
+
children: [
|
|
104
|
+
{ path: '/', component: DashboardHome },
|
|
105
|
+
{ path: '/settings', component: Settings }
|
|
106
|
+
]
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
// Route metadata
|
|
110
|
+
{
|
|
111
|
+
path: '/private',
|
|
112
|
+
component: Private,
|
|
113
|
+
meta: { requiresAuth: true, title: 'Private Page' }
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### lazy
|
|
119
|
+
|
|
120
|
+
Lazy-load components for code splitting.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { lazy } from '@liteforge/router'
|
|
124
|
+
|
|
125
|
+
const routes = [
|
|
126
|
+
{
|
|
127
|
+
path: '/admin',
|
|
128
|
+
component: lazy(() => import('./AdminPanel'), {
|
|
129
|
+
loading: () => <Spinner />,
|
|
130
|
+
error: ({ error, retry }) => <RetryButton onclick={retry} />,
|
|
131
|
+
delay: 200 // Show loading after 200ms
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Navigation
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
import { use } from '@liteforge/runtime'
|
|
141
|
+
|
|
142
|
+
const MyComponent = () => {
|
|
143
|
+
const router = use('router')
|
|
144
|
+
|
|
145
|
+
// Navigate programmatically
|
|
146
|
+
router.navigate('/users/123')
|
|
147
|
+
router.navigate('/users/123', { replace: true })
|
|
148
|
+
router.navigate({ name: 'user', params: { id: '123' } })
|
|
149
|
+
|
|
150
|
+
// Go back/forward
|
|
151
|
+
router.back()
|
|
152
|
+
router.forward()
|
|
153
|
+
|
|
154
|
+
// Reactive route data (signals)
|
|
155
|
+
router.path() // '/users/123'
|
|
156
|
+
router.params() // { id: '123' }
|
|
157
|
+
router.query() // { tab: 'posts' }
|
|
158
|
+
router.hash() // '#section'
|
|
159
|
+
router.route() // Full matched route object
|
|
160
|
+
|
|
161
|
+
return <div>Current path: {() => router.path()}</div>
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Link and NavLink
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
import { Link, NavLink } from '@liteforge/router'
|
|
169
|
+
|
|
170
|
+
// Basic link
|
|
171
|
+
<Link href="/about">About</Link>
|
|
172
|
+
|
|
173
|
+
// With query params
|
|
174
|
+
<Link href="/search" query={{ q: 'term' }}>Search</Link>
|
|
175
|
+
|
|
176
|
+
// Replace history instead of push
|
|
177
|
+
<Link href="/login" replace>Login</Link>
|
|
178
|
+
|
|
179
|
+
// NavLink adds 'active' class when matched
|
|
180
|
+
<NavLink href="/users" activeClass="nav-active">
|
|
181
|
+
Users
|
|
182
|
+
</NavLink>
|
|
183
|
+
|
|
184
|
+
// Exact matching (default matches partial paths)
|
|
185
|
+
<NavLink href="/users" exact>Users</NavLink>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### RouterOutlet
|
|
189
|
+
|
|
190
|
+
Renders the current route's component.
|
|
191
|
+
|
|
192
|
+
```tsx
|
|
193
|
+
import { RouterOutlet } from '@liteforge/router'
|
|
194
|
+
|
|
195
|
+
const Layout = () => (
|
|
196
|
+
<div>
|
|
197
|
+
<Header />
|
|
198
|
+
<main>
|
|
199
|
+
<RouterOutlet /> {/* Current route renders here */}
|
|
200
|
+
</main>
|
|
201
|
+
<Footer />
|
|
202
|
+
</div>
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
// For nested routes, use additional outlets
|
|
206
|
+
const Dashboard = () => (
|
|
207
|
+
<div>
|
|
208
|
+
<Sidebar />
|
|
209
|
+
<RouterOutlet /> {/* Nested route renders here */}
|
|
210
|
+
</div>
|
|
211
|
+
)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Guards
|
|
215
|
+
|
|
216
|
+
Guards control access to routes.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { defineGuard, createAuthGuard, createRoleGuard } from '@liteforge/router'
|
|
220
|
+
|
|
221
|
+
// Custom guard
|
|
222
|
+
const premiumGuard = defineGuard('premium', async (ctx) => {
|
|
223
|
+
const user = await getUser()
|
|
224
|
+
if (!user.isPremium) {
|
|
225
|
+
return '/upgrade' // Redirect
|
|
226
|
+
}
|
|
227
|
+
return true // Allow
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
// Built-in guards
|
|
231
|
+
const authGuard = createAuthGuard({
|
|
232
|
+
isAuthenticated: () => !!token(),
|
|
233
|
+
redirectTo: '/login'
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
const adminGuard = createRoleGuard({
|
|
237
|
+
getCurrentRole: () => user()?.role,
|
|
238
|
+
allowedRoles: ['admin', 'superadmin'],
|
|
239
|
+
redirectTo: '/unauthorized'
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
const router = createRouter({
|
|
243
|
+
routes: [...],
|
|
244
|
+
guards: {
|
|
245
|
+
auth: authGuard,
|
|
246
|
+
premium: premiumGuard,
|
|
247
|
+
admin: adminGuard
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Middleware
|
|
253
|
+
|
|
254
|
+
Middleware runs on every navigation.
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
import { defineMiddleware, createLoggerMiddleware } from '@liteforge/router'
|
|
258
|
+
|
|
259
|
+
// Custom middleware
|
|
260
|
+
const analyticsMiddleware = defineMiddleware('analytics', (ctx, next) => {
|
|
261
|
+
trackPageView(ctx.to.path)
|
|
262
|
+
return next()
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
// Built-in middleware
|
|
266
|
+
const logger = createLoggerMiddleware({ collapsed: true })
|
|
267
|
+
const title = createTitleMiddleware({
|
|
268
|
+
default: 'My App',
|
|
269
|
+
template: (title) => `${title} | My App`
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
const router = createRouter({
|
|
273
|
+
routes: [...],
|
|
274
|
+
middleware: [logger, title, analyticsMiddleware]
|
|
275
|
+
})
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Types
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
import type {
|
|
282
|
+
Router,
|
|
283
|
+
RouterOptions,
|
|
284
|
+
RouteDefinition,
|
|
285
|
+
MatchedRoute,
|
|
286
|
+
GuardFunction,
|
|
287
|
+
GuardContext,
|
|
288
|
+
MiddlewareFunction,
|
|
289
|
+
NavigateOptions,
|
|
290
|
+
LinkProps
|
|
291
|
+
} from '@liteforge/router'
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## License
|
|
295
|
+
|
|
296
|
+
MIT
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { LazyComponentWithMethods } from './lazy.js';
|
|
2
|
+
/**
|
|
3
|
+
* RouterOutlet configuration
|
|
4
|
+
*/
|
|
5
|
+
export interface RouterOutletConfig {
|
|
6
|
+
/** Fallback component when no route matches */
|
|
7
|
+
fallback?: () => Node;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* RouterOutlet renders the component for the current matched route.
|
|
11
|
+
*
|
|
12
|
+
* For nested routes, each RouterOutlet tracks its depth and renders
|
|
13
|
+
* the appropriate level of the route match chain.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // Basic usage
|
|
18
|
+
* const outlet = RouterOutlet();
|
|
19
|
+
*
|
|
20
|
+
* // With fallback for no match
|
|
21
|
+
* const outlet = RouterOutlet({ fallback: () => document.createTextNode('Not Found') });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function RouterOutlet(config?: RouterOutletConfig): Node;
|
|
25
|
+
/**
|
|
26
|
+
* Link component configuration
|
|
27
|
+
*/
|
|
28
|
+
export interface LinkConfig {
|
|
29
|
+
/** Target path or URL */
|
|
30
|
+
href: string;
|
|
31
|
+
/** Link content - string or Node */
|
|
32
|
+
children: string | Node;
|
|
33
|
+
/** Class added when path matches (prefix match by default, exact match if `exact` is true) */
|
|
34
|
+
activeClass?: string;
|
|
35
|
+
/** Class added when path matches exactly */
|
|
36
|
+
exactActiveClass?: string;
|
|
37
|
+
/** When true, activeClass requires exact path match instead of prefix match */
|
|
38
|
+
exact?: boolean;
|
|
39
|
+
/** Static CSS class */
|
|
40
|
+
class?: string;
|
|
41
|
+
/** Use replaceState instead of pushState */
|
|
42
|
+
replace?: boolean;
|
|
43
|
+
/** Target attribute for the link */
|
|
44
|
+
target?: string;
|
|
45
|
+
/** Additional attributes */
|
|
46
|
+
attrs?: Record<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Preload the route's lazy component on hover.
|
|
49
|
+
* Can be true (preload on mouseenter) or a LazyComponent to preload.
|
|
50
|
+
*/
|
|
51
|
+
preload?: boolean | LazyComponentWithMethods;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Link creates an anchor element with client-side navigation.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // Basic link
|
|
59
|
+
* const link = Link({ href: '/about', children: 'About Us' });
|
|
60
|
+
*
|
|
61
|
+
* // With active class
|
|
62
|
+
* const navLink = Link({
|
|
63
|
+
* href: '/users',
|
|
64
|
+
* children: 'Users',
|
|
65
|
+
* activeClass: 'nav-active',
|
|
66
|
+
* exactActiveClass: 'nav-exact',
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function Link(config: LinkConfig): HTMLAnchorElement;
|
|
71
|
+
/**
|
|
72
|
+
* NavLink is an alias for Link with navigation-focused defaults.
|
|
73
|
+
*/
|
|
74
|
+
export declare const NavLink: typeof Link;
|
|
75
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAM1D;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;CACvB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,MAAM,GAAE,kBAAuB,GAAG,IAAI,CA2XlE;AAMD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,yBAAyB;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,8FAA8F;IAC9F,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,4CAA4C;IAC5C,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,+EAA+E;IAC/E,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,uBAAuB;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,4CAA4C;IAC5C,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,oCAAoC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4BAA4B;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,wBAAwB,CAAC;CAC9C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,iBAAiB,CAoI1D;AAMD;;GAEG;AACH,eAAO,MAAM,OAAO,aAAO,CAAC"}
|