-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.d.ts
More file actions
606 lines (570 loc) · 16.1 KB
/
Copy pathrouter.d.ts
File metadata and controls
606 lines (570 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/**
* VanillaJS Router - TypeScript Definitions
* Enterprise-grade hash-based routing in pure vanilla JavaScript
*/
/**
* Raw route parameters where each key maps to an array of string values.
*
* **Why arrays?** URL parameters can appear multiple times:
* `?category=books&category=electronics` becomes `{ category: ["books", "electronics"] }`
*
* @example
* ```typescript
* // URL: #!/products?category=electronics&sort=price
* const params: RouteParams = {
* category: ["electronics"],
* sort: ["price"]
* };
* ```
*/
export interface RouteParams {
[key: string]: string[];
}
/**
* Type-coerced route parameters with automatic string → primitive type conversion.
*
* **Automatic conversions:**
* - `"123"` → `123` (number)
* - `"true"/"false"` → `true/false` (boolean)
* - `"null"` → `null`
* - `"undefined"` → `undefined`
* - `"45.67"` → `45.67` (float)
* - Other strings remain as strings
*
* @example
* ```typescript
* // URL: #!/analytics?users=1500&active=true&revenue=45000.50
* const typed: TypedRouteParams = {
* users: [1500], // number
* active: [true], // boolean
* revenue: [45000.5] // float
* };
*
* // Extract first values (common pattern)
* const [userCount] = typed.users || [0]; // 1500
* const [isActive] = typed.active || [false]; // true
* ```
*/
export interface TypedRouteParams {
[key: string]: (string | number | boolean | null | undefined)[];
}
/**
* Complete route information object containing path and all parameter variations.
*
* This is the main object you'll work with in navigation guards and route handlers.
*
* @example
* ```typescript
* // URL: #!/user?id=123&tab=profile
* const route: Route = {
* path: "user",
* params: { id: ["123"], tab: ["profile"] },
* paramsTyped: { id: [123], tab: ["profile"] },
* query: {},
* queryTyped: {}
* };
* ```
*/
export interface Route {
/**
* The route path extracted from hash (after #!/).
*
* @example "user", "dashboard", "products"
*/
path: string;
/**
* Raw query parameters from URL search string (?key=value).
* All values are strings in arrays for consistency.
*
* @example
* URL: `https://example.com?filter=active&sort=date`
* Result: `{ filter: ["active"], sort: ["date"] }`
*/
query: RouteParams;
/**
* Raw parameters from hash fragment (#!/path?key=value).
* All values are strings in arrays for consistency.
*
* @example
* URL: `#!/products?category=books&category=electronics&sort=price`
* Result: `{ category: ["books", "electronics"], sort: ["price"] }`
*/
params: RouteParams;
/**
* Type-coerced query parameters with automatic type conversion.
* Numbers, booleans, null, undefined are converted from strings.
*
* @example
* URL: `?page=2&active=true&limit=10`
* Result: `{ page: [2], active: [true], limit: [10] }`
*/
queryTyped: TypedRouteParams;
/**
* Type-coerced hash parameters with automatic type conversion.
* Numbers, booleans, null, undefined are converted from strings.
*
* @example
* URL: `#!/analytics?users=1500&revenue=45000.50&active=true`
* Result: `{ users: [1500], revenue: [45000.5], active: [true] }`
*/
paramsTyped: TypedRouteParams;
}
/**
* Navigation state containing both destination and source routes.
*
* Useful for comparing where you're going vs where you came from.
*
* @example
* ```typescript
* const state: RouteState = {
* to: { path: "dashboard", ... }, // Where we're going
* from: { path: "home", ... } // Where we came from
* };
* ```
*/
export interface RouteState {
/** The route being navigated to */
to: Route;
/** The route being navigated from */
from: Route;
}
/**
* Navigation guard function that can prevent or modify navigation.
*
* **Guards can:**
* - Inspect the destination route before navigation
* - Throw errors to cancel navigation
* - Perform async operations (API calls, auth checks)
* - Update page content based on the new route
*
* **To cancel navigation:** Throw an error or reject a promise
*
* @param to - The route being navigated to
* @param from - The route being navigated from
*
* @example
* ```typescript
* const authGuard: NavigationGuard = async (to, from) => {
* // Check authentication before accessing protected routes
* if (to.path === 'admin' && !user.isAuthenticated) {
* throw new Error('Authentication required');
* }
*
* // Load data for the new route
* await loadUserData(to.params.id);
* };
*
* router.beforeEach(authGuard);
* ```
*/
export type NavigationGuard = (to: Route, from: Route) => void | Promise<void>;
/**
* Navigation hook that runs after successful navigation.
*
* **Hooks are useful for:**
* - Analytics tracking
* - Updating page titles
* - Cleaning up resources
* - Logging navigation events
*
* **Note:** Hooks cannot cancel navigation (it's already complete)
*
* @param to - The route that was navigated to
* @param from - The route that was navigated from
*
* @example
* ```typescript
* const analyticsHook: NavigationHook = (to, from) => {
* // Track page views
* analytics.track('page_view', {
* page: to.path,
* referrer: from.path
* });
*
* // Update page title
* document.title = `My App - ${to.path}`;
* };
*
* router.afterEach(analyticsHook);
* ```
*/
export type NavigationHook = (to: Route, from: Route) => void;
/**
* Function to unsubscribe from navigation guards or hooks.
*
* Call this function to remove a previously registered guard/hook.
*
* @example
* ```typescript
* // Register a guard and get unsubscribe function
* const unsubscribe = router.beforeEach((to, from) => {
* console.log('Navigating to:', to.path);
* });
*
* // Later, remove the guard
* unsubscribe();
* ```
*/
export type UnsubscribeFunction = () => void;
/**
* Scroll position data stored for each route.
*
* The router automatically captures and restores scroll positions
* when navigating between routes.
*
* @internal This is primarily used internally by the router
*/
export interface ScrollPosition {
/** Window horizontal scroll position in pixels */
winX: number;
/** Window vertical scroll position in pixels */
winY: number;
/** Container element scroll position in pixels (if applicable) */
containerY: number;
/** Timestamp when this position was captured */
timestamp: number;
}
/**
* Main router interface providing all navigation and route management functionality.
*
* This is the primary interface you'll interact with for all routing operations.
*
* @example
* ```typescript
* // Create router instance
* const router: Router = createRouter();
*
* // Set up navigation guard
* router.beforeEach((to, from) => {
* console.log(`Navigating from ${from.path} to ${to.path}`);
* });
*
* // Navigate programmatically
* await router.push('/dashboard?tab=analytics');
*
* // Get current route info
* const current = router.currentRoute();
* console.log(current.path); // "dashboard"
* ```
*/
export interface Router {
/**
* Register a function to run before each navigation.
*
* **Use cases:**
* - Authentication checks
* - Data loading
* - Route validation
* - Loading states
*
* **To cancel navigation:** Throw an error or reject a promise
*
* @param guard - Function that receives (to, from) route objects
* @returns Unsubscribe function to remove this guard
*
* @example
* ```typescript
* // Authentication guard
* const unsubscribe = router.beforeEach(async (to, from) => {
* if (to.path === 'admin' && !await checkAuth()) {
* throw new Error('Access denied');
* }
* });
*
* // Remove guard when no longer needed
* unsubscribe();
* ```
*/
beforeEach(guard: NavigationGuard): UnsubscribeFunction;
/**
* Register a function to run after each successful navigation.
*
* **Use cases:**
* - Analytics tracking
* - Page title updates
* - Cleanup tasks
* - Success notifications
*
* @param hook - Function that receives (to, from) route objects
* @returns Unsubscribe function to remove this hook
*
* @example
* ```typescript
* // Analytics tracking
* router.afterEach((to, from) => {
* gtag('config', 'GA_MEASUREMENT_ID', {
* page_path: to.path
* });
* });
* ```
*/
afterEach(hook: NavigationHook): UnsubscribeFunction;
/**
* Navigate to a new route programmatically (adds to browser history).
*
* **Path formats supported:**
* - Simple: `"dashboard"`
* - With params: `"user?id=123&tab=profile"`
* - With leading slash: `"/products?category=books"`
*
* @param path - Route path with optional parameters
* @returns Promise that resolves to true if navigation succeeded, false if cancelled
*
* @example
* ```typescript
* // Simple navigation
* await router.push('/dashboard');
*
* // With parameters
* await router.push('/user?id=123&name=John&admin=true');
*
* // Check if navigation succeeded
* const success = await router.push('/protected-area');
* if (!success) {
* console.log('Navigation was cancelled by a guard');
* }
* ```
*/
push(path: string): Promise<boolean>;
/**
* Navigate to a new route, replacing current history entry.
*
* Unlike `push()`, this doesn't add a new entry to browser history.
* Useful for redirects or replacing invalid routes.
*
* @param path - Route path with optional parameters
* @returns Promise that resolves to true if navigation succeeded, false if cancelled
*
* @example
* ```typescript
* // Redirect without adding to history
* await router.replace('/login');
*
* // Replace invalid route
* if (isInvalidRoute(currentPath)) {
* await router.replace('/404');
* }
* ```
*/
replace(path: string): Promise<boolean>;
/**
* Get the current route information.
*
* Returns a copy of the current route object with all parameter variations.
*
* @returns Current route object with path and parameters
*
* @example
* ```typescript
* const route = router.currentRoute();
* console.log(route.path); // "user"
* console.log(route.params.id); // ["123"] (raw string)
* console.log(route.paramsTyped.id); // [123] (converted number)
*
* // Common pattern: extract first parameter value
* const [userId] = route.paramsTyped.id || [0];
* ```
*/
currentRoute(): Route;
/**
* Get the previous route information.
*
* Useful for implementing "back" functionality or breadcrumbs.
*
* @returns Previous route object, or empty route if none
*
* @example
* ```typescript
* const previous = router.previousRoute();
* if (previous.path) {
* console.log(`Came from: ${previous.path}`);
* }
* ```
*/
previousRoute(): Route;
/**
* Get both current and previous route information.
*
* Convenient method that returns both routes in a single object.
*
* @returns Object with 'to' (current) and 'from' (previous) routes
*
* @example
* ```typescript
* const { to, from } = router.getRouteState();
* console.log(`Navigation: ${from.path} → ${to.path}`);
* ```
*/
getRouteState(): RouteState;
/**
* Get current route's hash parameters with type coercion applied.
*
* Convenience method equivalent to `currentRoute().paramsTyped`.
*
* @returns Type-coerced hash parameters
*
* @example
* ```typescript
* // URL: #!/user?id=123&admin=true&score=98.5
* const params = router.getTypedParams();
* const [id] = params.id || [0]; // 123 (number)
* const [isAdmin] = params.admin || [false]; // true (boolean)
* const [score] = params.score || [0]; // 98.5 (number)
* ```
*/
getTypedParams(): TypedRouteParams;
/**
* Get current route's query parameters with type coercion applied.
*
* Convenience method equivalent to `currentRoute().queryTyped`.
*
* @returns Type-coerced query parameters
*
* @example
* ```typescript
* // URL: https://example.com?page=2&limit=50&active=true#!/dashboard
* const query = router.getTypedQuery();
* const [page] = query.page || [1]; // 2 (number)
* const [limit] = query.limit || [10]; // 50 (number)
* const [active] = query.active || [false]; // true (boolean)
* ```
*/
getTypedQuery(): TypedRouteParams;
/**
* Manually save the current scroll position for a route.
*
* The router normally does this automatically, but you can call this
* method to capture scroll positions at specific moments.
*
* @param path - Route path to save position for (defaults to current route)
*
* @example
* ```typescript
* // Save scroll position before showing a modal
* router.saveScrollPosition();
* showModal();
* ```
*/
saveScrollPosition(path?: string): void;
/**
* Manually restore the scroll position for a route.
*
* The router normally does this automatically during navigation,
* but you can call this method to restore positions manually.
*
* @param path - Route path to restore position for (defaults to current route)
*
* @example
* ```typescript
* // Restore scroll position after closing a modal
* closeModal();
* router.restoreScrollPosition();
* ```
*/
restoreScrollPosition(path?: string): void;
/**
* Clear all saved scroll positions from memory.
*
* Useful for memory cleanup in long-running applications.
*
* @example
* ```typescript
* // Clear scroll history when user logs out
* router.clearScrollHistory();
* ```
*/
clearScrollHistory(): void;
/**
* Navigate through browser history by a specific number of steps.
*
* Positive numbers go forward, negative numbers go backward.
*
* @param delta - Number of steps to move in history (+/-)
*
* @example
* ```typescript
* router.go(-1); // Go back one page
* router.go(-2); // Go back two pages
* router.go(1); // Go forward one page
* ```
*/
go(delta: number): void;
/**
* Navigate back one step in browser history.
*
* Equivalent to clicking the browser's back button or calling `go(-1)`.
*
* @example
* ```typescript
* // Add a back button to your UI
* backButton.onclick = () => router.back();
* ```
*/
back(): void;
/**
* Navigate forward one step in browser history.
*
* Equivalent to clicking the browser's forward button or calling `go(1)`.
*
* @example
* ```typescript
* // Add a forward button to your UI
* forwardButton.onclick = () => router.forward();
* ```
*/
forward(): void;
/**
* Clean up the router and remove all event listeners.
*
* **Important:** Call this when destroying your application to prevent memory leaks.
* After calling destroy(), the router instance should not be used.
*
* **What gets cleaned up:**
* - Event listeners (hashchange, scroll)
* - Navigation guards and hooks
* - Scroll position history
* - Internal state
*
* @example
* ```typescript
* // Clean up when app is destroyed
* window.addEventListener('beforeunload', () => {
* router.destroy();
* });
*
* // Or in a SPA framework lifecycle
* onBeforeUnmount(() => {
* router.destroy();
* });
* ```
*/
destroy(): void;
}
// Utility functions
export function paramsToObj(params: URLSearchParams): RouteParams;
export function coerceValue(value: string): string | number | boolean | null | undefined;
export function coerceParams(params: RouteParams): TypedRouteParams;
// Router factory function
export function createRouter(win?: Window): Router;
// Default export
declare const createRouter: (win?: Window) => Router;
export default createRouter;
// Pre-configured router instance
export declare const router: Router;
// Global declaration for UMD build
declare global {
interface Window {
MyRouter: Router;
}
// For environments where MyRouter might be global
const MyRouter: Router;
}
// Module augmentation for environments that might extend the router
declare module 'vanillajs-router' {
interface Route {
// Allow users to extend Route interface
}
interface Router {
// Allow users to extend Router interface
}
}