@remix-run/router 0.0.0-experimental-48058118
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/CHANGELOG.md +170 -0
- package/LICENSE.md +22 -0
- package/README.md +107 -0
- package/dist/history.d.ts +249 -0
- package/dist/index.d.ts +7 -0
- package/dist/router.cjs.js +4204 -0
- package/dist/router.cjs.js.map +1 -0
- package/dist/router.d.ts +437 -0
- package/dist/router.js +4117 -0
- package/dist/router.js.map +1 -0
- package/dist/router.umd.js +4210 -0
- package/dist/router.umd.js.map +1 -0
- package/dist/router.umd.min.js +12 -0
- package/dist/router.umd.min.js.map +1 -0
- package/dist/utils.d.ts +481 -0
- package/history.ts +721 -0
- package/index.ts +93 -0
- package/package.json +33 -0
- package/router.ts +3851 -0
- package/utils.ts +1559 -0
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import type { History, Location, Path, To } from "./history";
|
|
2
|
+
import { Action as HistoryAction } from "./history";
|
|
3
|
+
import type { AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticRouteMatch, AgnosticRouteObject, FormEncType, FormMethod, MiddlewareContext, RouteData } from "./utils";
|
|
4
|
+
import { DeferredData } from "./utils";
|
|
5
|
+
/**
|
|
6
|
+
* A Router instance manages all navigation and data loading/mutations
|
|
7
|
+
*/
|
|
8
|
+
export interface Router {
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
* PRIVATE - DO NOT USE
|
|
12
|
+
*
|
|
13
|
+
* Return the basename for the router
|
|
14
|
+
*/
|
|
15
|
+
get basename(): RouterInit["basename"];
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
* PRIVATE - DO NOT USE
|
|
19
|
+
*
|
|
20
|
+
* Return the current state of the router
|
|
21
|
+
*/
|
|
22
|
+
get state(): RouterState;
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
* PRIVATE - DO NOT USE
|
|
26
|
+
*
|
|
27
|
+
* Return the routes for this router instance
|
|
28
|
+
*/
|
|
29
|
+
get routes(): AgnosticDataRouteObject[];
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
* PRIVATE - DO NOT USE
|
|
33
|
+
*
|
|
34
|
+
* Initialize the router, including adding history listeners and kicking off
|
|
35
|
+
* initial data fetches. Returns a function to cleanup listeners and abort
|
|
36
|
+
* any in-progress loads
|
|
37
|
+
*/
|
|
38
|
+
initialize(): Router;
|
|
39
|
+
/**
|
|
40
|
+
* @internal
|
|
41
|
+
* PRIVATE - DO NOT USE
|
|
42
|
+
*
|
|
43
|
+
* Subscribe to router.state updates
|
|
44
|
+
*
|
|
45
|
+
* @param fn function to call with the new state
|
|
46
|
+
*/
|
|
47
|
+
subscribe(fn: RouterSubscriber): () => void;
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
* PRIVATE - DO NOT USE
|
|
51
|
+
*
|
|
52
|
+
* Enable scroll restoration behavior in the router
|
|
53
|
+
*
|
|
54
|
+
* @param savedScrollPositions Object that will manage positions, in case
|
|
55
|
+
* it's being restored from sessionStorage
|
|
56
|
+
* @param getScrollPosition Function to get the active Y scroll position
|
|
57
|
+
* @param getKey Function to get the key to use for restoration
|
|
58
|
+
*/
|
|
59
|
+
enableScrollRestoration(savedScrollPositions: Record<string, number>, getScrollPosition: GetScrollPositionFunction, getKey?: GetScrollRestorationKeyFunction): () => void;
|
|
60
|
+
/**
|
|
61
|
+
* @internal
|
|
62
|
+
* PRIVATE - DO NOT USE
|
|
63
|
+
*
|
|
64
|
+
* Navigate forward/backward in the history stack
|
|
65
|
+
* @param to Delta to move in the history stack
|
|
66
|
+
*/
|
|
67
|
+
navigate(to: number): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Navigate to the given path
|
|
70
|
+
* @param to Path to navigate to
|
|
71
|
+
* @param opts Navigation options (method, submission, etc.)
|
|
72
|
+
*/
|
|
73
|
+
navigate(to: To, opts?: RouterNavigateOptions): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* @internal
|
|
76
|
+
* PRIVATE - DO NOT USE
|
|
77
|
+
*
|
|
78
|
+
* Trigger a fetcher load/submission
|
|
79
|
+
*
|
|
80
|
+
* @param key Fetcher key
|
|
81
|
+
* @param routeId Route that owns the fetcher
|
|
82
|
+
* @param href href to fetch
|
|
83
|
+
* @param opts Fetcher options, (method, submission, etc.)
|
|
84
|
+
*/
|
|
85
|
+
fetch(key: string, routeId: string, href: string, opts?: RouterNavigateOptions): void;
|
|
86
|
+
/**
|
|
87
|
+
* @internal
|
|
88
|
+
* PRIVATE - DO NOT USE
|
|
89
|
+
*
|
|
90
|
+
* Trigger a revalidation of all current route loaders and fetcher loads
|
|
91
|
+
*/
|
|
92
|
+
revalidate(): void;
|
|
93
|
+
/**
|
|
94
|
+
* @internal
|
|
95
|
+
* PRIVATE - DO NOT USE
|
|
96
|
+
*
|
|
97
|
+
* Utility function to create an href for the given location
|
|
98
|
+
* @param location
|
|
99
|
+
*/
|
|
100
|
+
createHref(location: Location | URL): string;
|
|
101
|
+
/**
|
|
102
|
+
* @internal
|
|
103
|
+
* PRIVATE - DO NOT USE
|
|
104
|
+
*
|
|
105
|
+
* Utility function to URL encode a destination path according to the internal
|
|
106
|
+
* history implementation
|
|
107
|
+
* @param to
|
|
108
|
+
*/
|
|
109
|
+
encodeLocation(to: To): Path;
|
|
110
|
+
/**
|
|
111
|
+
* @internal
|
|
112
|
+
* PRIVATE - DO NOT USE
|
|
113
|
+
*
|
|
114
|
+
* Get/create a fetcher for the given key
|
|
115
|
+
* @param key
|
|
116
|
+
*/
|
|
117
|
+
getFetcher<TData = any>(key?: string): Fetcher<TData>;
|
|
118
|
+
/**
|
|
119
|
+
* @internal
|
|
120
|
+
* PRIVATE - DO NOT USE
|
|
121
|
+
*
|
|
122
|
+
* Delete the fetcher for a given key
|
|
123
|
+
* @param key
|
|
124
|
+
*/
|
|
125
|
+
deleteFetcher(key?: string): void;
|
|
126
|
+
/**
|
|
127
|
+
* @internal
|
|
128
|
+
* PRIVATE - DO NOT USE
|
|
129
|
+
*
|
|
130
|
+
* Cleanup listeners and abort any in-progress loads
|
|
131
|
+
*/
|
|
132
|
+
dispose(): void;
|
|
133
|
+
/**
|
|
134
|
+
* @internal
|
|
135
|
+
* PRIVATE - DO NOT USE
|
|
136
|
+
*
|
|
137
|
+
* Get a navigation blocker
|
|
138
|
+
* @param key The identifier for the blocker
|
|
139
|
+
* @param fn The blocker function implementation
|
|
140
|
+
*/
|
|
141
|
+
getBlocker(key: string, fn: BlockerFunction): Blocker;
|
|
142
|
+
/**
|
|
143
|
+
* @internal
|
|
144
|
+
* PRIVATE - DO NOT USE
|
|
145
|
+
*
|
|
146
|
+
* Delete a navigation blocker
|
|
147
|
+
* @param key The identifier for the blocker
|
|
148
|
+
*/
|
|
149
|
+
deleteBlocker(key: string): void;
|
|
150
|
+
/**
|
|
151
|
+
* @internal
|
|
152
|
+
* PRIVATE - DO NOT USE
|
|
153
|
+
*
|
|
154
|
+
* Internal fetch AbortControllers accessed by unit tests
|
|
155
|
+
*/
|
|
156
|
+
_internalFetchControllers: Map<string, AbortController>;
|
|
157
|
+
/**
|
|
158
|
+
* @internal
|
|
159
|
+
* PRIVATE - DO NOT USE
|
|
160
|
+
*
|
|
161
|
+
* Internal pending DeferredData instances accessed by unit tests
|
|
162
|
+
*/
|
|
163
|
+
_internalActiveDeferreds: Map<string, DeferredData>;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* State maintained internally by the router. During a navigation, all states
|
|
167
|
+
* reflect the the "old" location unless otherwise noted.
|
|
168
|
+
*/
|
|
169
|
+
export interface RouterState {
|
|
170
|
+
/**
|
|
171
|
+
* The action of the most recent navigation
|
|
172
|
+
*/
|
|
173
|
+
historyAction: HistoryAction;
|
|
174
|
+
/**
|
|
175
|
+
* The current location reflected by the router
|
|
176
|
+
*/
|
|
177
|
+
location: Location;
|
|
178
|
+
/**
|
|
179
|
+
* The current set of route matches
|
|
180
|
+
*/
|
|
181
|
+
matches: AgnosticDataRouteMatch[];
|
|
182
|
+
/**
|
|
183
|
+
* Tracks whether we've completed our initial data load
|
|
184
|
+
*/
|
|
185
|
+
initialized: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Current scroll position we should start at for a new view
|
|
188
|
+
* - number -> scroll position to restore to
|
|
189
|
+
* - false -> do not restore scroll at all (used during submissions)
|
|
190
|
+
* - null -> don't have a saved position, scroll to hash or top of page
|
|
191
|
+
*/
|
|
192
|
+
restoreScrollPosition: number | false | null;
|
|
193
|
+
/**
|
|
194
|
+
* Indicate whether this navigation should skip resetting the scroll position
|
|
195
|
+
* if we are unable to restore the scroll position
|
|
196
|
+
*/
|
|
197
|
+
preventScrollReset: boolean;
|
|
198
|
+
/**
|
|
199
|
+
* Tracks the state of the current navigation
|
|
200
|
+
*/
|
|
201
|
+
navigation: Navigation;
|
|
202
|
+
/**
|
|
203
|
+
* Tracks any in-progress revalidations
|
|
204
|
+
*/
|
|
205
|
+
revalidation: RevalidationState;
|
|
206
|
+
/**
|
|
207
|
+
* Data from the loaders for the current matches
|
|
208
|
+
*/
|
|
209
|
+
loaderData: RouteData;
|
|
210
|
+
/**
|
|
211
|
+
* Data from the action for the current matches
|
|
212
|
+
*/
|
|
213
|
+
actionData: RouteData | null;
|
|
214
|
+
/**
|
|
215
|
+
* Errors caught from loaders for the current matches
|
|
216
|
+
*/
|
|
217
|
+
errors: RouteData | null;
|
|
218
|
+
/**
|
|
219
|
+
* Map of current fetchers
|
|
220
|
+
*/
|
|
221
|
+
fetchers: Map<string, Fetcher>;
|
|
222
|
+
/**
|
|
223
|
+
* Map of current blockers
|
|
224
|
+
*/
|
|
225
|
+
blockers: Map<string, Blocker>;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Data that can be passed into hydrate a Router from SSR
|
|
229
|
+
*/
|
|
230
|
+
export declare type HydrationState = Partial<Pick<RouterState, "loaderData" | "actionData" | "errors">>;
|
|
231
|
+
/**
|
|
232
|
+
* Future flags to toggle on new feature behavior
|
|
233
|
+
*/
|
|
234
|
+
export interface FutureConfig {
|
|
235
|
+
unstable_middleware: boolean;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Initialization options for createRouter
|
|
239
|
+
*/
|
|
240
|
+
export interface RouterInit {
|
|
241
|
+
basename?: string;
|
|
242
|
+
routes: AgnosticRouteObject[];
|
|
243
|
+
history: History;
|
|
244
|
+
hydrationData?: HydrationState;
|
|
245
|
+
future?: FutureConfig;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* State returned from a server-side query() call
|
|
249
|
+
*/
|
|
250
|
+
export interface StaticHandlerContext {
|
|
251
|
+
basename: Router["basename"];
|
|
252
|
+
location: RouterState["location"];
|
|
253
|
+
matches: RouterState["matches"];
|
|
254
|
+
loaderData: RouterState["loaderData"];
|
|
255
|
+
actionData: RouterState["actionData"];
|
|
256
|
+
errors: RouterState["errors"];
|
|
257
|
+
statusCode: number;
|
|
258
|
+
loaderHeaders: Record<string, Headers>;
|
|
259
|
+
actionHeaders: Record<string, Headers>;
|
|
260
|
+
activeDeferreds: Record<string, DeferredData> | null;
|
|
261
|
+
_deepestRenderedBoundaryId?: string | null;
|
|
262
|
+
}
|
|
263
|
+
interface StaticHandlerQueryOpts {
|
|
264
|
+
requestContext?: unknown;
|
|
265
|
+
middlewareContext?: MiddlewareContext;
|
|
266
|
+
}
|
|
267
|
+
interface StaticHandlerQueryRouteOpts extends StaticHandlerQueryOpts {
|
|
268
|
+
routeId?: string;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* A StaticHandler instance manages a singular SSR navigation/fetch event
|
|
272
|
+
*/
|
|
273
|
+
export interface StaticHandler {
|
|
274
|
+
dataRoutes: AgnosticDataRouteObject[];
|
|
275
|
+
query(request: Request, opts?: StaticHandlerQueryOpts): Promise<StaticHandlerContext | Response>;
|
|
276
|
+
queryRoute(request: Request, opts?: StaticHandlerQueryRouteOpts): Promise<any>;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Subscriber function signature for changes to router state
|
|
280
|
+
*/
|
|
281
|
+
export interface RouterSubscriber {
|
|
282
|
+
(state: RouterState): void;
|
|
283
|
+
}
|
|
284
|
+
interface UseMatchesMatch {
|
|
285
|
+
id: string;
|
|
286
|
+
pathname: string;
|
|
287
|
+
params: AgnosticRouteMatch["params"];
|
|
288
|
+
data: unknown;
|
|
289
|
+
handle: unknown;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Function signature for determining the key to be used in scroll restoration
|
|
293
|
+
* for a given location
|
|
294
|
+
*/
|
|
295
|
+
export interface GetScrollRestorationKeyFunction {
|
|
296
|
+
(location: Location, matches: UseMatchesMatch[]): string | null;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Function signature for determining the current scroll position
|
|
300
|
+
*/
|
|
301
|
+
export interface GetScrollPositionFunction {
|
|
302
|
+
(): number;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Options for a navigate() call for a Link navigation
|
|
306
|
+
*/
|
|
307
|
+
declare type LinkNavigateOptions = {
|
|
308
|
+
replace?: boolean;
|
|
309
|
+
state?: any;
|
|
310
|
+
preventScrollReset?: boolean;
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Options for a navigate() call for a Form navigation
|
|
314
|
+
*/
|
|
315
|
+
declare type SubmissionNavigateOptions = {
|
|
316
|
+
replace?: boolean;
|
|
317
|
+
state?: any;
|
|
318
|
+
preventScrollReset?: boolean;
|
|
319
|
+
formMethod?: FormMethod;
|
|
320
|
+
formEncType?: FormEncType;
|
|
321
|
+
formData: FormData;
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Options to pass to navigate() for either a Link or Form navigation
|
|
325
|
+
*/
|
|
326
|
+
export declare type RouterNavigateOptions = LinkNavigateOptions | SubmissionNavigateOptions;
|
|
327
|
+
/**
|
|
328
|
+
* Options to pass to fetch()
|
|
329
|
+
*/
|
|
330
|
+
export declare type RouterFetchOptions = Omit<LinkNavigateOptions, "replace"> | Omit<SubmissionNavigateOptions, "replace">;
|
|
331
|
+
/**
|
|
332
|
+
* Potential states for state.navigation
|
|
333
|
+
*/
|
|
334
|
+
export declare type NavigationStates = {
|
|
335
|
+
Idle: {
|
|
336
|
+
state: "idle";
|
|
337
|
+
location: undefined;
|
|
338
|
+
formMethod: undefined;
|
|
339
|
+
formAction: undefined;
|
|
340
|
+
formEncType: undefined;
|
|
341
|
+
formData: undefined;
|
|
342
|
+
};
|
|
343
|
+
Loading: {
|
|
344
|
+
state: "loading";
|
|
345
|
+
location: Location;
|
|
346
|
+
formMethod: FormMethod | undefined;
|
|
347
|
+
formAction: string | undefined;
|
|
348
|
+
formEncType: FormEncType | undefined;
|
|
349
|
+
formData: FormData | undefined;
|
|
350
|
+
};
|
|
351
|
+
Submitting: {
|
|
352
|
+
state: "submitting";
|
|
353
|
+
location: Location;
|
|
354
|
+
formMethod: FormMethod;
|
|
355
|
+
formAction: string;
|
|
356
|
+
formEncType: FormEncType;
|
|
357
|
+
formData: FormData;
|
|
358
|
+
};
|
|
359
|
+
};
|
|
360
|
+
export declare type Navigation = NavigationStates[keyof NavigationStates];
|
|
361
|
+
export declare type RevalidationState = "idle" | "loading";
|
|
362
|
+
/**
|
|
363
|
+
* Potential states for fetchers
|
|
364
|
+
*/
|
|
365
|
+
declare type FetcherStates<TData = any> = {
|
|
366
|
+
Idle: {
|
|
367
|
+
state: "idle";
|
|
368
|
+
formMethod: undefined;
|
|
369
|
+
formAction: undefined;
|
|
370
|
+
formEncType: undefined;
|
|
371
|
+
formData: undefined;
|
|
372
|
+
data: TData | undefined;
|
|
373
|
+
" _hasFetcherDoneAnything "?: boolean;
|
|
374
|
+
};
|
|
375
|
+
Loading: {
|
|
376
|
+
state: "loading";
|
|
377
|
+
formMethod: FormMethod | undefined;
|
|
378
|
+
formAction: string | undefined;
|
|
379
|
+
formEncType: FormEncType | undefined;
|
|
380
|
+
formData: FormData | undefined;
|
|
381
|
+
data: TData | undefined;
|
|
382
|
+
" _hasFetcherDoneAnything "?: boolean;
|
|
383
|
+
};
|
|
384
|
+
Submitting: {
|
|
385
|
+
state: "submitting";
|
|
386
|
+
formMethod: FormMethod;
|
|
387
|
+
formAction: string;
|
|
388
|
+
formEncType: FormEncType;
|
|
389
|
+
formData: FormData;
|
|
390
|
+
data: TData | undefined;
|
|
391
|
+
" _hasFetcherDoneAnything "?: boolean;
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
export declare type Fetcher<TData = any> = FetcherStates<TData>[keyof FetcherStates<TData>];
|
|
395
|
+
interface BlockerBlocked {
|
|
396
|
+
state: "blocked";
|
|
397
|
+
reset(): void;
|
|
398
|
+
proceed(): void;
|
|
399
|
+
location: Location;
|
|
400
|
+
}
|
|
401
|
+
interface BlockerUnblocked {
|
|
402
|
+
state: "unblocked";
|
|
403
|
+
reset: undefined;
|
|
404
|
+
proceed: undefined;
|
|
405
|
+
location: undefined;
|
|
406
|
+
}
|
|
407
|
+
interface BlockerProceeding {
|
|
408
|
+
state: "proceeding";
|
|
409
|
+
reset: undefined;
|
|
410
|
+
proceed: undefined;
|
|
411
|
+
location: Location;
|
|
412
|
+
}
|
|
413
|
+
export declare type Blocker = BlockerUnblocked | BlockerBlocked | BlockerProceeding;
|
|
414
|
+
export declare type BlockerFunction = (args: {
|
|
415
|
+
currentLocation: Location;
|
|
416
|
+
nextLocation: Location;
|
|
417
|
+
historyAction: HistoryAction;
|
|
418
|
+
}) => boolean;
|
|
419
|
+
export declare const IDLE_NAVIGATION: NavigationStates["Idle"];
|
|
420
|
+
export declare const IDLE_FETCHER: FetcherStates["Idle"];
|
|
421
|
+
export declare const IDLE_BLOCKER: BlockerUnblocked;
|
|
422
|
+
/**
|
|
423
|
+
* Create a router and listen to history POP navigations
|
|
424
|
+
*/
|
|
425
|
+
export declare function createRouter(init: RouterInit): Router;
|
|
426
|
+
export declare const UNSAFE_DEFERRED_SYMBOL: unique symbol;
|
|
427
|
+
export interface StaticHandlerInit {
|
|
428
|
+
basename?: string;
|
|
429
|
+
future?: FutureConfig;
|
|
430
|
+
}
|
|
431
|
+
export declare function createStaticHandler(routes: AgnosticRouteObject[], init?: StaticHandlerInit): StaticHandler;
|
|
432
|
+
/**
|
|
433
|
+
* Given an existing StaticHandlerContext and an error thrown at render time,
|
|
434
|
+
* provide an updated StaticHandlerContext suitable for a second SSR render
|
|
435
|
+
*/
|
|
436
|
+
export declare function getStaticContextFromError(routes: AgnosticDataRouteObject[], context: StaticHandlerContext, error: any): StaticHandlerContext;
|
|
437
|
+
export {};
|