@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/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) React Training 2015-2019
4
+ Copyright (c) Remix Software 2020-2022
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # Router
2
+
3
+ The `@remix-run/router` package is the heart of [React Router](https://github.com/remix-run/react-router) and provides all the core functionality for routing, data loading, data mutations, and navigation.
4
+
5
+ If you're using React Router, you should never `import` anything directly from
6
+ the `@remix-run/router` or `react-router` packages, but you should have everything
7
+ you need in either `react-router-dom` or `react-router-native`. Both of those
8
+ packages re-export everything from `@remix-run/router` and `react-router`.
9
+
10
+ ## API
11
+
12
+ A Router instance can be created using `createRouter`:
13
+
14
+ ```js
15
+ let router = createRouter({
16
+ // Routes array using react-router RouteObject's
17
+ routes,
18
+ // History instance
19
+ history,
20
+ // Callback function executed on every state change
21
+ onChange: (state) => { ... },
22
+ // Optional hydration data for SSR apps
23
+ hydrationData?: HydrationState;
24
+ }
25
+ ```
26
+
27
+ Internally, the Router represents the state in an object of the following format, which is available through `router.state` or via the `onChange` callback function:
28
+
29
+ ```ts
30
+ interface RouterState {
31
+ // The `history` action of the most recently completed navigation
32
+ action: Action;
33
+ // The current location of the router. During a navigation this reflects
34
+ // the "old" location and is updated upon completion of the navigation
35
+ location: Location;
36
+ // The current set of route matches
37
+ matches: RouteMatch[];
38
+ // The state of the current navigation
39
+ navigation: Navigation;
40
+ // Data from the loaders for the current matches
41
+ loaderData: RouteData;
42
+ // Data from the action for the current matches
43
+ actionData: RouteData | null;
44
+ // Errors thrown from loaders/actions for the current matches
45
+ errors: RouteData | null;
46
+ }
47
+ ```
48
+
49
+ ### Navigations
50
+
51
+ All navigations are done through the `router.navigate` API which is overloaded to support different types of navigations:
52
+
53
+ ```js
54
+ // Link navigation (pushes onto the history stack by default)
55
+ router.navigate('/page');
56
+
57
+ // Link navigation (replacing the history stack)
58
+ router.navigate('/page', { replace: true });
59
+
60
+ // Pop navigation (moving backward/forward in the history stack)
61
+ router.navigate(-1);
62
+
63
+ // Form navigation
64
+ router.navigate('/page', {
65
+ formMethod: 'GET',
66
+ formData: new FormData(...),
67
+ });
68
+ ```
69
+
70
+ ## Navigation Flows
71
+
72
+ Each navigation (link click or form submission) in the Router is reflected via an internal `state.navigation` that indicates the state of the navigation. This concept of a `navigation` is a complex and heavily async bit of logic that is foundational to the Router's ability to manage data loading, submission, error handling, redirects, interruptions, and so on. Due to the user-driven nature of interruptions we don't quite believe it can be modeled as a finite state machine, however we have modeled some of the happy path flows below for clarity.
73
+
74
+ _Note: This does not depict error or interruption flows._
75
+
76
+ ```mermaid
77
+ graph LR
78
+ %% Link click
79
+ idle -->|link clicked| loading/normalLoad
80
+ subgraph "<Link> click"
81
+ loading/normalLoad -->|loader redirected| loading/normalRedirect
82
+ loading/normalRedirect --> loading/normalRedirect
83
+ end
84
+ loading/normalLoad -->|loaders completed| idle
85
+ loading/normalRedirect -->|loaders completed| idle
86
+
87
+ %% Form method=get
88
+ idle -->|form method=get| submitting/loaderSubmission
89
+ subgraph "<Form method=get>"
90
+ submitting/loaderSubmission -->|loader redirected| R1[loading/submissionRedirect]
91
+ R1[loading/submissionRedirect] --> R1[loading/submissionRedirect]
92
+ end
93
+ submitting/loaderSubmission -->|loaders completed| idle
94
+ R1[loading/submissionRedirect] -->|loaders completed| idle
95
+
96
+ %% Form method=post
97
+ idle -->|form method=post| submitting/actionSubmission
98
+ subgraph "<Form method=post>"
99
+ submitting/actionSubmission -->|action returned| loading/actionReload
100
+ submitting/actionSubmission -->|action redirected| R2[loading/submissionRedirect]
101
+ loading/actionReload -->|loader redirected| R2[loading/submissionRedirect]
102
+ R2[loading/submissionRedirect] --> R2[loading/submissionRedirect]
103
+ end
104
+ loading/actionReload -->|loaders completed| idle
105
+ R2[loading/submissionRedirect] -->|loaders completed| idle
106
+
107
+ idle -->|fetcher action redirect| R3[loading/submissionRedirect]
108
+ subgraph "Fetcher action redirect"
109
+ R3[loading/submissionRedirect] --> R3[loading/submissionRedirect]
110
+ end
111
+ R3[loading/submissionRedirect] -->|loaders completed| idle
112
+ ```
113
+
114
+ ## Fetcher Flows
115
+
116
+ Fetcher submissions and loads in the Router can each have their own internal states, indicated on `fetcher.state` and `fetcher.type`. As with navigations, these states are a complex and heavily async bit of logic that is foundational to the Router's ability to manage data loading, submission, error handling, redirects, interruptions, and so on. Due to the user-driven nature of interruptions we don't quite believe it can be modeled as a finite state machine, however we have modeled some of the happy path flows below for clarity.
117
+
118
+ _Note: This does not depict error or interruption flows, nor the ability to re-use fetchers once they've reached `idle/done`._
119
+
120
+ ```mermaid
121
+ graph LR
122
+ idle/init -->|"load"| loading/normalLoad
123
+ subgraph "Normal Fetch"
124
+ loading/normalLoad -.->|loader redirected| T1{{navigation}}
125
+ end
126
+ loading/normalLoad -->|loader completed| idle/done
127
+ T1{{navigation}} -.-> idle/done
128
+
129
+ idle/init -->|"submit (get)"| submitting/loaderSubmission
130
+ subgraph "Loader Submission"
131
+ submitting/loaderSubmission -.->|"loader redirected"| T2{{navigation}}
132
+ end
133
+ submitting/loaderSubmission -->|loader completed| idle/done
134
+ T2{{navigation}} -.-> idle/done
135
+
136
+ idle/init -->|"submit (post)"| submitting/actionSubmission
137
+ subgraph "Action Submission"
138
+ submitting/actionSubmission -->|action completed| loading/actionReload
139
+ submitting/actionSubmission -->|action redirected| loading/submissionRedirect
140
+ loading/submissionRedirect -.-> T3{{navigation}}
141
+ loading/actionReload -.-> |loaders redirected| T3{{navigation}}
142
+ end
143
+ T3{{navigation}} -.-> idle/done
144
+ loading/actionReload --> |loaders completed| idle/done
145
+
146
+ idle/done -->|"revalidate"| loading/revalidate
147
+ subgraph "Fetcher Revalidation"
148
+ loading/revalidate -.->|loader redirected| T4{{navigation}}
149
+ end
150
+ loading/revalidate -->|loader completed| idle/done
151
+ T4{{navigation}} -.-> idle/done
152
+
153
+ classDef navigation fill:lightgreen;
154
+ class T1,T2,T3,T4 navigation;
155
+ ```
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function EncodeReservedCharacters(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function GoBack(history: History): Promise<void>;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function GoForward(history: History): Promise<void>;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function InitialLocationDefaultKey(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function InitialLocationHasKey(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function Listen(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function ListenPopOnly(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function PushMissingPathname(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function PushNewLocation(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function PushRelativePathname(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function PushRelativePathnameWarning(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function PushSamePath(history: History): Promise<void>;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function PushState(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function ReplaceNewLocation(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function ReplaceSamePath(history: History): void;
@@ -0,0 +1,2 @@
1
+ import type { History } from "../../history";
2
+ export default function ReplaceState(history: History): void;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @jest-environment ./__tests__/custom-environment.js
3
+ */
4
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @jest-environment ./__tests__/custom-environment.js
3
+ */
4
+ export {};
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @jest-environment ./__tests__/custom-environment.js
3
+ */
4
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function invariant(value: boolean, message?: string): asserts value;
2
+ export declare function invariant<T>(value: T | null | undefined, message?: string): asserts value is T;
@@ -0,0 +1 @@
1
+ export {};
package/history.d.ts ADDED
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Actions represent the type of change to a location value.
3
+ */
4
+ export declare enum Action {
5
+ /**
6
+ * A POP indicates a change to an arbitrary index in the history stack, such
7
+ * as a back or forward navigation. It does not describe the direction of the
8
+ * navigation, only that the current index changed.
9
+ *
10
+ * Note: This is the default action for newly created history objects.
11
+ */
12
+ Pop = "POP",
13
+ /**
14
+ * A PUSH indicates a new entry being added to the history stack, such as when
15
+ * a link is clicked and a new page loads. When this happens, all subsequent
16
+ * entries in the stack are lost.
17
+ */
18
+ Push = "PUSH",
19
+ /**
20
+ * A REPLACE indicates the entry at the current index in the history stack
21
+ * being replaced by a new one.
22
+ */
23
+ Replace = "REPLACE"
24
+ }
25
+ /**
26
+ * The pathname, search, and hash values of a URL.
27
+ */
28
+ export interface Path {
29
+ /**
30
+ * A URL pathname, beginning with a /.
31
+ */
32
+ pathname: string;
33
+ /**
34
+ * A URL search string, beginning with a ?.
35
+ */
36
+ search: string;
37
+ /**
38
+ * A URL fragment identifier, beginning with a #.
39
+ */
40
+ hash: string;
41
+ }
42
+ /**
43
+ * An entry in a history stack. A location contains information about the
44
+ * URL path, as well as possibly some arbitrary state and a key.
45
+ */
46
+ export interface Location extends Path {
47
+ /**
48
+ * A value of arbitrary data associated with this location.
49
+ */
50
+ state: any;
51
+ /**
52
+ * A unique string associated with this location. May be used to safely store
53
+ * and retrieve data in some other storage API, like `localStorage`.
54
+ *
55
+ * Note: This value is always "default" on the initial location.
56
+ */
57
+ key: string;
58
+ }
59
+ /**
60
+ * A change to the current location.
61
+ */
62
+ export interface Update {
63
+ /**
64
+ * The action that triggered the change.
65
+ */
66
+ action: Action;
67
+ /**
68
+ * The new location.
69
+ */
70
+ location: Location;
71
+ }
72
+ /**
73
+ * A function that receives notifications about location changes.
74
+ */
75
+ export interface Listener {
76
+ (update: Update): void;
77
+ }
78
+ /**
79
+ * Describes a location that is the destination of some navigation, either via
80
+ * `history.push` or `history.replace`. May be either a URL or the pieces of a
81
+ * URL path.
82
+ */
83
+ export declare type To = string | Partial<Path>;
84
+ /**
85
+ * A history is an interface to the navigation stack. The history serves as the
86
+ * source of truth for the current location, as well as provides a set of
87
+ * methods that may be used to change it.
88
+ *
89
+ * It is similar to the DOM's `window.history` object, but with a smaller, more
90
+ * focused API.
91
+ */
92
+ export interface History {
93
+ /**
94
+ * The last action that modified the current location. This will always be
95
+ * Action.Pop when a history instance is first created. This value is mutable.
96
+ */
97
+ readonly action: Action;
98
+ /**
99
+ * The current location. This value is mutable.
100
+ */
101
+ readonly location: Location;
102
+ /**
103
+ * Returns a valid href for the given `to` value that may be used as
104
+ * the value of an <a href> attribute.
105
+ *
106
+ * @param to - The destination URL
107
+ */
108
+ createHref(to: To): string;
109
+ /**
110
+ * Pushes a new location onto the history stack, increasing its length by one.
111
+ * If there were any entries in the stack after the current one, they are
112
+ * lost.
113
+ *
114
+ * @param to - The new URL
115
+ * @param state - Data to associate with the new location
116
+ */
117
+ push(to: To, state?: any): void;
118
+ /**
119
+ * Replaces the current location in the history stack with a new one. The
120
+ * location that was replaced will no longer be available.
121
+ *
122
+ * @param to - The new URL
123
+ * @param state - Data to associate with the new location
124
+ */
125
+ replace(to: To, state?: any): void;
126
+ /**
127
+ * Navigates `n` entries backward/forward in the history stack relative to the
128
+ * current index. For example, a "back" navigation would use go(-1).
129
+ *
130
+ * @param delta - The delta in the stack index
131
+ */
132
+ go(delta: number): void;
133
+ /**
134
+ * Sets up a listener that will be called whenever the current location
135
+ * changes.
136
+ *
137
+ * @param listener - A function that will be called when the location changes
138
+ * @returns unlisten - A function that may be used to stop listening
139
+ */
140
+ listen(listener: Listener): () => void;
141
+ }
142
+ /**
143
+ * A user-supplied object that describes a location. Used when providing
144
+ * entries to `createMemoryHistory` via its `initialEntries` option.
145
+ */
146
+ export declare type InitialEntry = string | Partial<Location>;
147
+ export declare type MemoryHistoryOptions = {
148
+ initialEntries?: InitialEntry[];
149
+ initialIndex?: number;
150
+ v5Compat?: boolean;
151
+ };
152
+ /**
153
+ * A memory history stores locations in memory. This is useful in stateful
154
+ * environments where there is no web browser, such as node tests or React
155
+ * Native.
156
+ */
157
+ export interface MemoryHistory extends History {
158
+ /**
159
+ * The current index in the history stack.
160
+ */
161
+ readonly index: number;
162
+ }
163
+ /**
164
+ * Memory history stores the current location in memory. It is designed for use
165
+ * in stateful non-browser environments like tests and React Native.
166
+ */
167
+ export declare function createMemoryHistory(options?: MemoryHistoryOptions): MemoryHistory;
168
+ /**
169
+ * A browser history stores the current location in regular URLs in a web
170
+ * browser environment. This is the standard for most web apps and provides the
171
+ * cleanest URLs the browser's address bar.
172
+ *
173
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#browserhistory
174
+ */
175
+ export interface BrowserHistory extends UrlHistory {
176
+ }
177
+ export declare type BrowserHistoryOptions = UrlHistoryOptions;
178
+ /**
179
+ * Browser history stores the location in regular URLs. This is the standard for
180
+ * most web apps, but it requires some configuration on the server to ensure you
181
+ * serve the same app at multiple URLs.
182
+ *
183
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
184
+ */
185
+ export declare function createBrowserHistory(options?: BrowserHistoryOptions): BrowserHistory;
186
+ /**
187
+ * A hash history stores the current location in the fragment identifier portion
188
+ * of the URL in a web browser environment.
189
+ *
190
+ * This is ideal for apps that do not control the server for some reason
191
+ * (because the fragment identifier is never sent to the server), including some
192
+ * shared hosting environments that do not provide fine-grained controls over
193
+ * which pages are served at which URLs.
194
+ *
195
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#hashhistory
196
+ */
197
+ export interface HashHistory extends UrlHistory {
198
+ }
199
+ export declare type HashHistoryOptions = UrlHistoryOptions;
200
+ /**
201
+ * Hash history stores the location in window.location.hash. This makes it ideal
202
+ * for situations where you don't want to send the location to the server for
203
+ * some reason, either because you do cannot configure it or the URL space is
204
+ * reserved for something else.
205
+ *
206
+ * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
207
+ */
208
+ export declare function createHashHistory(options?: HashHistoryOptions): HashHistory;
209
+ /**
210
+ * Creates a Location object with a unique key from the given Path
211
+ */
212
+ export declare function createLocation(current: string | Location, to: To, state?: any, key?: string): Location;
213
+ /**
214
+ * Creates a string URL path from the given pathname, search, and hash components.
215
+ */
216
+ export declare function createPath({ pathname, search, hash, }: Partial<Path>): string;
217
+ /**
218
+ * Parses a string URL path into its separate pathname, search, and hash components.
219
+ */
220
+ export declare function parsePath(path: string): Partial<Path>;
221
+ export interface UrlHistory extends History {
222
+ }
223
+ export declare type UrlHistoryOptions = {
224
+ window?: Window;
225
+ v5Compat?: boolean;
226
+ };
package/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { BrowserHistoryOptions, HashHistoryOptions, MemoryHistoryOptions } from "./history";
2
+ import type { Router, RouterInit } from "./router";
3
+ declare function createMemoryRouter({ initialEntries, initialIndex, ...routerInit }: MemoryHistoryOptions & Omit<RouterInit, "history">): Router;
4
+ declare function createBrowserRouter({ window, ...routerInit }: BrowserHistoryOptions & Omit<RouterInit, "history">): Router;
5
+ declare function createHashRouter({ window, ...routerInit }: HashHistoryOptions & Omit<RouterInit, "history">): Router;
6
+ export * from "./router";
7
+ export type { ActionFunction, DataRouteObject, FormEncType, FormMethod, JsonFunction, LoaderFunction, ParamParseKey, Params, PathMatch, PathPattern, RedirectFunction, RouteMatch, RouteObject, ShouldRevalidateFunction, Submission, } from "./utils";
8
+ export { generatePath, getToPathname, invariant, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, redirect, resolvePath, resolveTo, stripBasename, warning, } from "./utils";
9
+ export type { BrowserHistory, HashHistory, History, InitialEntry, Location, MemoryHistory, Path, To, } from "./history";
10
+ export { Action, createBrowserHistory, createPath, createHashHistory, createMemoryHistory, parsePath, } from "./history";
11
+ export { createBrowserRouter, createHashRouter, createMemoryRouter };