@flight-framework/router 0.0.2 → 0.0.4
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/README.md +488 -137
- package/dist/index.d.ts +267 -16
- package/dist/index.js +399 -55
- package/package.json +48 -48
- package/LICENSE +0 -21
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,30 @@ interface RouterProviderProps {
|
|
|
27
27
|
/** Base path for the router (e.g., '/app') */
|
|
28
28
|
basePath?: string;
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Prefetch strategy for Link component
|
|
32
|
+
*
|
|
33
|
+
* - 'none': No prefetching (default)
|
|
34
|
+
* - 'intent': Prefetch on hover or focus (recommended for most cases)
|
|
35
|
+
* - 'render': Prefetch immediately when link renders
|
|
36
|
+
* - 'viewport': Prefetch when link enters the viewport (good for mobile)
|
|
37
|
+
*/
|
|
38
|
+
type PrefetchStrategy = 'none' | 'intent' | 'render' | 'viewport';
|
|
39
|
+
/**
|
|
40
|
+
* Priority level for prefetch requests
|
|
41
|
+
*/
|
|
42
|
+
type PrefetchPriority = 'high' | 'low' | 'auto';
|
|
43
|
+
/**
|
|
44
|
+
* Options for programmatic prefetching
|
|
45
|
+
*/
|
|
46
|
+
interface PrefetchOptions {
|
|
47
|
+
/** Priority of the prefetch request */
|
|
48
|
+
priority?: PrefetchPriority;
|
|
49
|
+
/** Include data prefetch (loaders) */
|
|
50
|
+
includeData?: boolean;
|
|
51
|
+
/** Include module prefetch (JS chunks) */
|
|
52
|
+
includeModules?: boolean;
|
|
53
|
+
}
|
|
30
54
|
/**
|
|
31
55
|
* Props for Link component
|
|
32
56
|
*/
|
|
@@ -41,8 +65,17 @@ interface LinkProps {
|
|
|
41
65
|
target?: string;
|
|
42
66
|
/** Link relationship */
|
|
43
67
|
rel?: string;
|
|
44
|
-
/**
|
|
45
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Prefetch strategy for the target page.
|
|
70
|
+
*
|
|
71
|
+
* - `true` or `'intent'`: Prefetch on hover/focus
|
|
72
|
+
* - `'render'`: Prefetch when link renders
|
|
73
|
+
* - `'viewport'`: Prefetch when link enters viewport
|
|
74
|
+
* - `false` or `'none'`: No prefetching (default)
|
|
75
|
+
*
|
|
76
|
+
* @default 'none'
|
|
77
|
+
*/
|
|
78
|
+
prefetch?: boolean | PrefetchStrategy;
|
|
46
79
|
/** Replace current history entry instead of pushing */
|
|
47
80
|
replace?: boolean;
|
|
48
81
|
/** Scroll to top after navigation */
|
|
@@ -71,6 +104,51 @@ type RouteParams<T extends string = string> = Record<T, string>;
|
|
|
71
104
|
* Search params as key-value pairs
|
|
72
105
|
*/
|
|
73
106
|
type SearchParams = Record<string, string | string[]>;
|
|
107
|
+
/**
|
|
108
|
+
* Route definition for automatic route generation
|
|
109
|
+
*/
|
|
110
|
+
interface RouteDefinition {
|
|
111
|
+
/** Route path pattern (e.g., '/docs/:slug') */
|
|
112
|
+
path: string;
|
|
113
|
+
/** Dynamic import for component */
|
|
114
|
+
component: () => Promise<{
|
|
115
|
+
default: unknown;
|
|
116
|
+
}>;
|
|
117
|
+
/** Dynamic imports for layouts (in order, root first) */
|
|
118
|
+
layouts?: Array<() => Promise<{
|
|
119
|
+
default: unknown;
|
|
120
|
+
}>>;
|
|
121
|
+
/** Dynamic import for loader function */
|
|
122
|
+
loader?: () => Promise<{
|
|
123
|
+
loader: LoaderFunction;
|
|
124
|
+
}>;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Loader function signature
|
|
128
|
+
*/
|
|
129
|
+
type LoaderFunction = (context: LoaderContext) => Promise<unknown> | unknown;
|
|
130
|
+
/**
|
|
131
|
+
* Context passed to loader functions
|
|
132
|
+
*/
|
|
133
|
+
interface LoaderContext {
|
|
134
|
+
/** Route parameters */
|
|
135
|
+
params: RouteParams;
|
|
136
|
+
/** Request object (available on server) */
|
|
137
|
+
request?: Request;
|
|
138
|
+
/** URL search params */
|
|
139
|
+
searchParams: URLSearchParams;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Route match result
|
|
143
|
+
*/
|
|
144
|
+
interface RouteMatch {
|
|
145
|
+
/** Matched route definition */
|
|
146
|
+
route: RouteDefinition;
|
|
147
|
+
/** Extracted parameters */
|
|
148
|
+
params: RouteParams;
|
|
149
|
+
/** Matched path */
|
|
150
|
+
pathname: string;
|
|
151
|
+
}
|
|
74
152
|
|
|
75
153
|
/**
|
|
76
154
|
* Router Context and Provider
|
|
@@ -88,13 +166,101 @@ declare let RouterProvider: unknown;
|
|
|
88
166
|
declare let useRouter: () => RouterContextValue;
|
|
89
167
|
|
|
90
168
|
/**
|
|
91
|
-
* Link Component
|
|
169
|
+
* @flight-framework/router - Link Component
|
|
170
|
+
*
|
|
171
|
+
* Universal client-side navigation link with advanced prefetching support.
|
|
172
|
+
* Works with React, Vue, Svelte, Solid, and vanilla JavaScript.
|
|
92
173
|
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
174
|
+
* Features:
|
|
175
|
+
* - Multiple prefetch strategies (none, intent, render, viewport)
|
|
176
|
+
* - SSR-safe implementation
|
|
177
|
+
* - Accessibility compliant
|
|
178
|
+
* - Framework-agnostic core with React adapter
|
|
95
179
|
*/
|
|
96
180
|
|
|
181
|
+
/**
|
|
182
|
+
* @deprecated Use `prefetch` from '@flight-framework/router' instead.
|
|
183
|
+
*/
|
|
184
|
+
declare function prefetchRoute(href: string): void;
|
|
97
185
|
declare let Link: unknown;
|
|
186
|
+
/**
|
|
187
|
+
* Create a link element with SPA navigation support (vanilla JS).
|
|
188
|
+
*
|
|
189
|
+
* For Vue, Svelte, Solid, and other frameworks, use this function
|
|
190
|
+
* or implement framework-specific wrappers.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const link = createLink({
|
|
195
|
+
* href: '/docs',
|
|
196
|
+
* children: 'Documentation',
|
|
197
|
+
* prefetch: 'intent',
|
|
198
|
+
* });
|
|
199
|
+
* document.body.appendChild(link);
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
declare function createLink(props: LinkProps): HTMLAnchorElement;
|
|
203
|
+
/**
|
|
204
|
+
* Get link props for Vue template usage.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```vue
|
|
208
|
+
* <script setup>
|
|
209
|
+
* import { useLinkProps } from '@flight-framework/router';
|
|
210
|
+
* const linkProps = useLinkProps('/docs', { prefetch: 'intent' });
|
|
211
|
+
* </script>
|
|
212
|
+
*
|
|
213
|
+
* <template>
|
|
214
|
+
* <a v-bind="linkProps">Documentation</a>
|
|
215
|
+
* </template>
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
declare function useLinkProps(href: string, options?: Partial<Omit<LinkProps, 'href' | 'children'>>): {
|
|
219
|
+
href: string;
|
|
220
|
+
onClick: (e: MouseEvent) => void;
|
|
221
|
+
onMouseenter?: () => void;
|
|
222
|
+
onFocus?: () => void;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @flight-framework/router - PrefetchPageLinks Component
|
|
227
|
+
*
|
|
228
|
+
* Renders prefetch link tags for a page. Useful for proactively
|
|
229
|
+
* prefetching pages based on user behavior (e.g., search results).
|
|
230
|
+
*
|
|
231
|
+
* This is the Flight equivalent of React Router's PrefetchPageLinks.
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
declare let PrefetchPageLinks: unknown;
|
|
235
|
+
/**
|
|
236
|
+
* Prefetch multiple pages at once.
|
|
237
|
+
*
|
|
238
|
+
* Useful for prefetching a list of search results or related pages.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* // Prefetch top 5 search results
|
|
243
|
+
* prefetchPages([
|
|
244
|
+
* '/products/1',
|
|
245
|
+
* '/products/2',
|
|
246
|
+
* '/products/3',
|
|
247
|
+
* ]);
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
declare function prefetchPages(pages: string[], options?: PrefetchOptions): void;
|
|
251
|
+
/**
|
|
252
|
+
* Prefetch a page when idle.
|
|
253
|
+
*
|
|
254
|
+
* Uses requestIdleCallback if available, otherwise falls back
|
|
255
|
+
* to setTimeout.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* // Prefetch after initial render settles
|
|
260
|
+
* prefetchWhenIdle('/dashboard');
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
declare function prefetchWhenIdle(page: string, options?: PrefetchOptions): void;
|
|
98
264
|
|
|
99
265
|
/**
|
|
100
266
|
* Router Hooks
|
|
@@ -124,16 +290,6 @@ declare let usePathname: () => string;
|
|
|
124
290
|
* ```
|
|
125
291
|
*/
|
|
126
292
|
declare function navigate(to: string, options?: NavigateOptions): void;
|
|
127
|
-
/**
|
|
128
|
-
* Prefetch a route's assets
|
|
129
|
-
* Creates a prefetch link for the target URL
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* ```ts
|
|
133
|
-
* prefetch('/docs'); // Prefetch when user hovers
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
declare function prefetch(href: string): void;
|
|
137
293
|
/**
|
|
138
294
|
* Match a pathname against a route pattern
|
|
139
295
|
*
|
|
@@ -157,5 +313,100 @@ declare function matchRoute(pathname: string, pattern: string): {
|
|
|
157
313
|
* ```
|
|
158
314
|
*/
|
|
159
315
|
declare function parseParams(pathname: string, pattern: string): RouteParams;
|
|
316
|
+
/**
|
|
317
|
+
* Find the best matching route from a list of route definitions
|
|
318
|
+
*/
|
|
319
|
+
declare function findRoute(pathname: string, routes: RouteDefinition[]): RouteMatch | null;
|
|
320
|
+
/**
|
|
321
|
+
* Generate a URL from a route pattern and params
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* generatePath('/docs/:slug', { slug: 'routing' });
|
|
326
|
+
* // Returns: '/docs/routing'
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
declare function generatePath(pattern: string, params?: RouteParams): string;
|
|
330
|
+
/**
|
|
331
|
+
* Check if current path matches a pattern
|
|
332
|
+
*/
|
|
333
|
+
declare function isActive(pattern: string): boolean;
|
|
334
|
+
/**
|
|
335
|
+
* Redirect to a new URL (full page navigation)
|
|
336
|
+
* Use this for external redirects or when you need to break out of SPA
|
|
337
|
+
*/
|
|
338
|
+
declare function redirect(url: string): never;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* @flight-framework/router - Prefetch Utilities
|
|
342
|
+
*
|
|
343
|
+
* Universal prefetch system for Flight Framework.
|
|
344
|
+
* Works across all UI frameworks and runtimes.
|
|
345
|
+
*
|
|
346
|
+
* Features:
|
|
347
|
+
* - Multiple prefetch strategies (intent, render, viewport)
|
|
348
|
+
* - Priority-based prefetching
|
|
349
|
+
* - Module and data prefetching
|
|
350
|
+
* - IntersectionObserver for viewport detection
|
|
351
|
+
* - SSR-safe implementation
|
|
352
|
+
*/
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Prefetch a URL with the specified options.
|
|
356
|
+
*
|
|
357
|
+
* This is the main programmatic prefetch API for Flight Framework.
|
|
358
|
+
* It creates appropriate link elements for prefetching resources.
|
|
359
|
+
*
|
|
360
|
+
* @param href - The URL to prefetch
|
|
361
|
+
* @param options - Prefetch configuration options
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* // Basic prefetch
|
|
366
|
+
* prefetch('/docs');
|
|
367
|
+
*
|
|
368
|
+
* // High priority prefetch (for critical navigation paths)
|
|
369
|
+
* prefetch('/checkout', { priority: 'high' });
|
|
370
|
+
*
|
|
371
|
+
* // Prefetch with data loaders
|
|
372
|
+
* prefetch('/products', { includeData: true });
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
375
|
+
declare function prefetch(href: string, options?: PrefetchOptions): void;
|
|
376
|
+
/**
|
|
377
|
+
* Prefetch multiple URLs at once.
|
|
378
|
+
*
|
|
379
|
+
* @param hrefs - Array of URLs to prefetch
|
|
380
|
+
* @param options - Prefetch configuration options
|
|
381
|
+
*/
|
|
382
|
+
declare function prefetchAll(hrefs: string[], options?: PrefetchOptions): void;
|
|
383
|
+
/**
|
|
384
|
+
* Check if a URL has been prefetched.
|
|
385
|
+
*
|
|
386
|
+
* @param href - The URL to check
|
|
387
|
+
* @returns True if the URL has been prefetched
|
|
388
|
+
*/
|
|
389
|
+
declare function isPrefetched(href: string): boolean;
|
|
390
|
+
/**
|
|
391
|
+
* Clear all prefetch state (useful for testing or memory management).
|
|
392
|
+
*/
|
|
393
|
+
declare function clearPrefetchCache(): void;
|
|
394
|
+
/**
|
|
395
|
+
* Observe an element for viewport entry and trigger prefetch.
|
|
396
|
+
*
|
|
397
|
+
* @param element - The link element to observe
|
|
398
|
+
* @param href - The URL to prefetch when visible
|
|
399
|
+
* @returns Cleanup function to stop observing
|
|
400
|
+
*/
|
|
401
|
+
declare function observeForPrefetch(element: Element, href: string): () => void;
|
|
402
|
+
/**
|
|
403
|
+
* Setup intent-based prefetching for an element.
|
|
404
|
+
* Prefetches on mouseenter or focus.
|
|
405
|
+
*
|
|
406
|
+
* @param element - The link element
|
|
407
|
+
* @param href - The URL to prefetch
|
|
408
|
+
* @returns Cleanup function to remove listeners
|
|
409
|
+
*/
|
|
410
|
+
declare function setupIntentPrefetch(element: HTMLElement, href: string): () => void;
|
|
160
411
|
|
|
161
|
-
export { Link, type LinkProps, type NavigateOptions, type RouteParams, RouterContext, type RouterContextValue, RouterProvider, type RouterProviderProps, type SearchParams, matchRoute, navigate, parseParams, prefetch, useParams, usePathname, useRouter, useSearchParams };
|
|
412
|
+
export { Link, type LinkProps, type LoaderContext, type LoaderFunction, type NavigateOptions, type PrefetchOptions, PrefetchPageLinks, type PrefetchPriority, type PrefetchStrategy, type RouteDefinition, type RouteMatch, type RouteParams, RouterContext, type RouterContextValue, RouterProvider, type RouterProviderProps, type SearchParams, clearPrefetchCache, createLink, findRoute, generatePath, isActive, isPrefetched, matchRoute, navigate, observeForPrefetch, parseParams, prefetch, prefetchAll, prefetchPages, prefetchRoute, prefetchWhenIdle, redirect, setupIntentPrefetch, useLinkProps, useParams, usePathname, useRouter, useSearchParams };
|