@remix-run/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.
Files changed (41) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +155 -0
  3. package/__tests__/TestSequences/EncodedReservedCharacters.d.ts +2 -0
  4. package/__tests__/TestSequences/GoBack.d.ts +2 -0
  5. package/__tests__/TestSequences/GoForward.d.ts +2 -0
  6. package/__tests__/TestSequences/InitialLocationDefaultKey.d.ts +2 -0
  7. package/__tests__/TestSequences/InitialLocationHasKey.d.ts +2 -0
  8. package/__tests__/TestSequences/Listen.d.ts +2 -0
  9. package/__tests__/TestSequences/ListenPopOnly.d.ts +2 -0
  10. package/__tests__/TestSequences/PushMissingPathname.d.ts +2 -0
  11. package/__tests__/TestSequences/PushNewLocation.d.ts +2 -0
  12. package/__tests__/TestSequences/PushRelativePathname.d.ts +2 -0
  13. package/__tests__/TestSequences/PushRelativePathnameWarning.d.ts +2 -0
  14. package/__tests__/TestSequences/PushSamePath.d.ts +2 -0
  15. package/__tests__/TestSequences/PushState.d.ts +2 -0
  16. package/__tests__/TestSequences/ReplaceNewLocation.d.ts +2 -0
  17. package/__tests__/TestSequences/ReplaceSamePath.d.ts +2 -0
  18. package/__tests__/TestSequences/ReplaceState.d.ts +2 -0
  19. package/__tests__/browser-test.d.ts +4 -0
  20. package/__tests__/create-path-test.d.ts +1 -0
  21. package/__tests__/hash-base-test.d.ts +4 -0
  22. package/__tests__/hash-test.d.ts +4 -0
  23. package/__tests__/memory-test.d.ts +1 -0
  24. package/__tests__/router-test.d.ts +2 -0
  25. package/__tests__/setup.d.ts +1 -0
  26. package/history.d.ts +226 -0
  27. package/index.d.ts +11 -0
  28. package/index.js +2392 -0
  29. package/index.js.map +1 -0
  30. package/main.js +19 -0
  31. package/package.json +22 -0
  32. package/router.d.ts +298 -0
  33. package/router.development.js +2226 -0
  34. package/router.development.js.map +1 -0
  35. package/router.production.min.js +12 -0
  36. package/router.production.min.js.map +1 -0
  37. package/umd/router.development.js +2418 -0
  38. package/umd/router.development.js.map +1 -0
  39. package/umd/router.production.min.js +12 -0
  40. package/umd/router.production.min.js.map +1 -0
  41. package/utils.d.ts +248 -0
package/router.d.ts ADDED
@@ -0,0 +1,298 @@
1
+ import { History, Location, To } from "./history";
2
+ import { Action as HistoryAction } from "./history";
3
+ import { DataRouteObject, FormEncType, FormMethod, RouteMatch, RouteObject } from "./utils";
4
+ /**
5
+ * Map of routeId -> data returned from a loader/action/error
6
+ */
7
+ export interface RouteData {
8
+ [routeId: string]: any;
9
+ }
10
+ export interface DataRouteMatch extends RouteMatch<string, DataRouteObject> {
11
+ }
12
+ /**
13
+ * A Router instance manages all navigation and data loading/mutations
14
+ */
15
+ export interface Router {
16
+ /**
17
+ * Return the current state of the router
18
+ */
19
+ get state(): RouterState;
20
+ /**
21
+ * Initialize the router, including adding history listeners and kicking off
22
+ * initial data fetches. Returns a function to cleanup listeners and abort
23
+ * any in-progress loads
24
+ */
25
+ initialize(): Router;
26
+ /**
27
+ * Subscribe to router.state updates
28
+ *
29
+ * @param fn function to call with the new state
30
+ */
31
+ subscribe(fn: RouterSubscriber): () => void;
32
+ /**
33
+ * Enable scroll restoration behavior in the router
34
+ *
35
+ * @param savedScrollPositions Object that will manage positions, in case
36
+ * it's being restored from sessionStorage
37
+ * @param getScrollPosition Function to get the active Y scroll position
38
+ * @param getKey Function to get the key to use for restoration
39
+ */
40
+ enableScrollRestoration(savedScrollPositions: Record<string, number>, getScrollPosition: GetScrollPositionFunction, getKey?: GetScrollRestorationKeyFunction): () => void;
41
+ /**
42
+ * Navigate forward/backward in the history stack
43
+ * @param path Delta to move in the history stack
44
+ */
45
+ navigate(path: number): void;
46
+ /**
47
+ * Navigate to the given path
48
+ * @param path Path to navigate to
49
+ * @param opts Navigation options (method, submission, etc.)
50
+ */
51
+ navigate(path: To, opts?: NavigateOptions): void;
52
+ /**
53
+ * Trigger a fetcher load/submission
54
+ *
55
+ * @param key Fetcher key
56
+ * @param href href to fetch
57
+ * @param opts Fetcher options, (method, submission, etc.)
58
+ */
59
+ fetch(key: string, href: string, opts?: NavigateOptions): void;
60
+ /**
61
+ * Trigger a revalidation of all current route loaders and fetcher loads
62
+ */
63
+ revalidate(): void;
64
+ /**
65
+ * Utility function to create an href for the given location
66
+ * @param location
67
+ */
68
+ createHref(location: Location | URL): string;
69
+ /**
70
+ * Get/create a fetcher for the given key
71
+ * @param key
72
+ */
73
+ getFetcher<TData = any>(key?: string): Fetcher<TData>;
74
+ /**
75
+ * Delete the fetcher for a given key
76
+ * @param key
77
+ */
78
+ deleteFetcher(key?: string): void;
79
+ /**
80
+ * Cleanup listeners and abort any in-progress loads
81
+ */
82
+ dispose(): void;
83
+ /**
84
+ * Internal fetch AbortControllers accessed by unit tests
85
+ * @private
86
+ */
87
+ _internalFetchControllers: Map<string, AbortController>;
88
+ }
89
+ /**
90
+ * State maintained internally by the router. During a navigation, all states
91
+ * reflect the the "old" location unless otherwise noted.
92
+ */
93
+ export interface RouterState {
94
+ /**
95
+ * The action of the most recent navigation
96
+ */
97
+ historyAction: HistoryAction;
98
+ /**
99
+ * The current location reflected by the router
100
+ */
101
+ location: Location;
102
+ /**
103
+ * The current set of route matches
104
+ */
105
+ matches: DataRouteMatch[];
106
+ /**
107
+ * Tracks whether we've completed our initial data load
108
+ */
109
+ initialized: boolean;
110
+ /**
111
+ * Current scroll position we should start at for a new view
112
+ * - number -> scroll position to restore to
113
+ * - false -> do not restore scroll at all (used during submissions)
114
+ * - null -> don't have a saved position, scroll to hash or top of page
115
+ */
116
+ restoreScrollPosition: number | false | null;
117
+ /**
118
+ * Indicate whether this navigation should reset the scroll position if we
119
+ * are unable to restore the scroll position
120
+ */
121
+ resetScrollPosition: boolean;
122
+ /**
123
+ * Tracks the state of the current navigation
124
+ */
125
+ navigation: Navigation;
126
+ /**
127
+ * Tracks any in-progress revalidations
128
+ */
129
+ revalidation: RevalidationState;
130
+ /**
131
+ * Data from the loaders for the current matches
132
+ */
133
+ loaderData: RouteData;
134
+ /**
135
+ * Data from the action for the current matches
136
+ */
137
+ actionData: RouteData | null;
138
+ /**
139
+ * Errors caught from loaders for the current matches
140
+ */
141
+ errors: RouteData | null;
142
+ /**
143
+ * Map of current fetchers
144
+ */
145
+ fetchers: Map<string, Fetcher>;
146
+ }
147
+ /**
148
+ * Data that can be passed into hydrate a Router from SSR
149
+ */
150
+ export declare type HydrationState = Partial<Pick<RouterState, "loaderData" | "actionData" | "errors">>;
151
+ /**
152
+ * Initialization options for createRouter
153
+ */
154
+ export interface RouterInit {
155
+ routes: RouteObject[];
156
+ history: History;
157
+ hydrationData?: HydrationState;
158
+ }
159
+ /**
160
+ * Subscriber function signature for changes to router state
161
+ */
162
+ export interface RouterSubscriber {
163
+ (state: RouterState): void;
164
+ }
165
+ /**
166
+ * Function signature for determining the key to be used in scroll restoration
167
+ * for a given location
168
+ */
169
+ export interface GetScrollRestorationKeyFunction {
170
+ (location: Location, matches: DataRouteMatch[]): string | null;
171
+ }
172
+ /**
173
+ * Function signature for determining the current scroll position
174
+ */
175
+ export interface GetScrollPositionFunction {
176
+ (): number;
177
+ }
178
+ /**
179
+ * Options for a navigate() call for a Link navigation
180
+ */
181
+ declare type LinkNavigateOptions = {
182
+ replace?: boolean;
183
+ state?: any;
184
+ };
185
+ /**
186
+ * Options for a navigate() call for a Form navigation
187
+ */
188
+ declare type SubmissionNavigateOptions = {
189
+ replace?: boolean;
190
+ state?: any;
191
+ formMethod?: FormMethod;
192
+ formEncType?: FormEncType;
193
+ formData: FormData;
194
+ };
195
+ /**
196
+ * Options to pass to navigate() for either a Link or Form navigation
197
+ */
198
+ export declare type NavigateOptions = LinkNavigateOptions | SubmissionNavigateOptions;
199
+ /**
200
+ * Potential states for state.navigation
201
+ */
202
+ export declare type NavigationStates = {
203
+ Idle: {
204
+ state: "idle";
205
+ location: undefined;
206
+ formMethod: undefined;
207
+ formAction: undefined;
208
+ formEncType: undefined;
209
+ formData: undefined;
210
+ };
211
+ Loading: {
212
+ state: "loading";
213
+ location: Location;
214
+ formMethod: FormMethod | undefined;
215
+ formAction: string | undefined;
216
+ formEncType: FormEncType | undefined;
217
+ formData: FormData | undefined;
218
+ };
219
+ Submitting: {
220
+ state: "submitting";
221
+ location: Location;
222
+ formMethod: FormMethod;
223
+ formAction: string;
224
+ formEncType: FormEncType;
225
+ formData: FormData;
226
+ };
227
+ };
228
+ export declare type Navigation = NavigationStates[keyof NavigationStates];
229
+ export declare type RevalidationState = "idle" | "loading";
230
+ /**
231
+ * Potential states for fetchers
232
+ */
233
+ declare type FetcherStates<TData = any> = {
234
+ Idle: {
235
+ state: "idle";
236
+ formMethod: undefined;
237
+ formAction: undefined;
238
+ formEncType: undefined;
239
+ formData: undefined;
240
+ data: TData | undefined;
241
+ };
242
+ Loading: {
243
+ state: "loading";
244
+ formMethod: FormMethod | undefined;
245
+ formAction: string | undefined;
246
+ formEncType: FormEncType | undefined;
247
+ formData: FormData | undefined;
248
+ data: TData | undefined;
249
+ };
250
+ Submitting: {
251
+ state: "submitting";
252
+ formMethod: FormMethod;
253
+ formAction: string;
254
+ formEncType: FormEncType;
255
+ formData: FormData;
256
+ data: TData | undefined;
257
+ };
258
+ };
259
+ export declare type Fetcher<TData = any> = FetcherStates<TData>[keyof FetcherStates<TData>];
260
+ declare enum ResultType {
261
+ data = "data",
262
+ redirect = "redirect",
263
+ error = "error"
264
+ }
265
+ /**
266
+ * Successful result from a loader or action
267
+ */
268
+ export interface SuccessResult {
269
+ type: ResultType.data;
270
+ data: any;
271
+ }
272
+ /**
273
+ * Redirect result from a loader or action
274
+ */
275
+ export interface RedirectResult {
276
+ type: ResultType.redirect;
277
+ status: number;
278
+ location: string;
279
+ revalidate: boolean;
280
+ }
281
+ /**
282
+ * Unsuccessful result from a loader or action
283
+ */
284
+ export interface ErrorResult {
285
+ type: ResultType.error;
286
+ error: any;
287
+ }
288
+ /**
289
+ * Result from a loader or action - potentially successful or unsuccessful
290
+ */
291
+ export declare type DataResult = SuccessResult | RedirectResult | ErrorResult;
292
+ export declare const IDLE_NAVIGATION: NavigationStates["Idle"];
293
+ export declare const IDLE_FETCHER: FetcherStates["Idle"];
294
+ /**
295
+ * Create a router and listen to history POP navigations
296
+ */
297
+ export declare function createRouter(init: RouterInit): Router;
298
+ export {};