source: node_modules/@remix-run/router/dist/router.d.ts@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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