@remix-run/router 0.0.0-experimental-e960cf1a → 0.0.0-experimental-a0888892
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 +211 -8
- package/dist/index.d.ts +2 -2
- package/dist/router.cjs.js +306 -272
- package/dist/router.cjs.js.map +1 -1
- package/dist/router.d.ts +2 -1
- package/dist/router.js +295 -259
- package/dist/router.js.map +1 -1
- package/dist/router.umd.js +306 -272
- package/dist/router.umd.js.map +1 -1
- package/dist/router.umd.min.js +2 -2
- package/dist/router.umd.min.js.map +1 -1
- package/dist/utils.d.ts +21 -21
- package/history.ts +4 -0
- package/index.ts +1 -3
- package/package.json +1 -1
- package/router.ts +467 -406
- package/utils.ts +60 -63
package/utils.ts
CHANGED
|
@@ -21,8 +21,7 @@ export enum ResultType {
|
|
|
21
21
|
export interface SuccessResult {
|
|
22
22
|
type: ResultType.data;
|
|
23
23
|
data: any;
|
|
24
|
-
|
|
25
|
-
headers?: Headers;
|
|
24
|
+
response?: Response;
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
/**
|
|
@@ -40,10 +39,7 @@ export interface DeferredResult {
|
|
|
40
39
|
*/
|
|
41
40
|
export interface RedirectResult {
|
|
42
41
|
type: ResultType.redirect;
|
|
43
|
-
|
|
44
|
-
location: string;
|
|
45
|
-
revalidate: boolean;
|
|
46
|
-
reloadDocument?: boolean;
|
|
42
|
+
response: Response;
|
|
47
43
|
}
|
|
48
44
|
|
|
49
45
|
/**
|
|
@@ -52,7 +48,7 @@ export interface RedirectResult {
|
|
|
52
48
|
export interface ErrorResult {
|
|
53
49
|
type: ResultType.error;
|
|
54
50
|
error: any;
|
|
55
|
-
|
|
51
|
+
response?: Response;
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
/**
|
|
@@ -64,6 +60,14 @@ export type DataResult =
|
|
|
64
60
|
| RedirectResult
|
|
65
61
|
| ErrorResult;
|
|
66
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Result from a loader or action called via dataStrategy
|
|
65
|
+
*/
|
|
66
|
+
export interface HandlerResult {
|
|
67
|
+
type: ResultType.data | ResultType.error;
|
|
68
|
+
result: any; // data, Error, Response, DeferredData
|
|
69
|
+
}
|
|
70
|
+
|
|
67
71
|
type LowerCaseFormMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
68
72
|
type UpperCaseFormMethod = Uppercase<LowerCaseFormMethod>;
|
|
69
73
|
|
|
@@ -166,22 +170,26 @@ export interface ActionFunctionArgs<Context = any>
|
|
|
166
170
|
*/
|
|
167
171
|
type DataFunctionValue = Response | NonNullable<unknown> | null;
|
|
168
172
|
|
|
173
|
+
type DataFunctionReturnValue = Promise<DataFunctionValue> | DataFunctionValue;
|
|
174
|
+
|
|
169
175
|
/**
|
|
170
176
|
* Route loader function signature
|
|
171
177
|
*/
|
|
172
178
|
export type LoaderFunction<Context = any> = {
|
|
173
|
-
(
|
|
174
|
-
|
|
175
|
-
|
|
179
|
+
(
|
|
180
|
+
args: LoaderFunctionArgs<Context>,
|
|
181
|
+
handlerCtx?: unknown
|
|
182
|
+
): DataFunctionReturnValue;
|
|
176
183
|
} & { hydrate?: boolean };
|
|
177
184
|
|
|
178
185
|
/**
|
|
179
186
|
* Route action function signature
|
|
180
187
|
*/
|
|
181
188
|
export interface ActionFunction<Context = any> {
|
|
182
|
-
(
|
|
183
|
-
|
|
184
|
-
|
|
189
|
+
(
|
|
190
|
+
args: ActionFunctionArgs<Context>,
|
|
191
|
+
handlerCtx?: unknown
|
|
192
|
+
): DataFunctionReturnValue;
|
|
185
193
|
}
|
|
186
194
|
|
|
187
195
|
/**
|
|
@@ -223,15 +231,23 @@ export interface DetectErrorBoundaryFunction {
|
|
|
223
231
|
(route: AgnosticRouteObject): boolean;
|
|
224
232
|
}
|
|
225
233
|
|
|
226
|
-
export interface
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
234
|
+
export interface DataStrategyMatch
|
|
235
|
+
extends AgnosticRouteMatch<string, AgnosticDataRouteObject> {
|
|
236
|
+
// TODO: What to call these?
|
|
237
|
+
bikeshed_loadRoute: (
|
|
238
|
+
handlerOverride?: (
|
|
239
|
+
handler: (ctx?: unknown) => DataFunctionReturnValue
|
|
240
|
+
) => DataFunctionReturnValue
|
|
241
|
+
) => Promise<HandlerResult>;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface DataStrategyFunctionArgs<Context = any>
|
|
245
|
+
extends DataFunctionArgs<Context> {
|
|
246
|
+
matches: DataStrategyMatch[];
|
|
231
247
|
}
|
|
232
248
|
|
|
233
249
|
export interface DataStrategyFunction {
|
|
234
|
-
(args: DataStrategyFunctionArgs): Promise<
|
|
250
|
+
(args: DataStrategyFunctionArgs): Promise<HandlerResult[]>;
|
|
235
251
|
}
|
|
236
252
|
|
|
237
253
|
/**
|
|
@@ -288,8 +304,8 @@ type AgnosticBaseRouteObject = {
|
|
|
288
304
|
caseSensitive?: boolean;
|
|
289
305
|
path?: string;
|
|
290
306
|
id?: string;
|
|
291
|
-
loader?: LoaderFunction;
|
|
292
|
-
action?: ActionFunction;
|
|
307
|
+
loader?: LoaderFunction | boolean;
|
|
308
|
+
action?: ActionFunction | boolean;
|
|
293
309
|
hasErrorBoundary?: boolean;
|
|
294
310
|
shouldRevalidate?: ShouldRevalidateFunction;
|
|
295
311
|
handle?: any;
|
|
@@ -412,14 +428,6 @@ export interface AgnosticRouteMatch<
|
|
|
412
428
|
export interface AgnosticDataRouteMatch
|
|
413
429
|
extends AgnosticRouteMatch<string, AgnosticDataRouteObject> {}
|
|
414
430
|
|
|
415
|
-
export type LazyRoutePromise = PromiseLike<AgnosticDataRouteObject> &
|
|
416
|
-
AgnosticDataRouteObject;
|
|
417
|
-
|
|
418
|
-
export interface AgnosticDataStrategyMatch
|
|
419
|
-
extends Omit<AgnosticRouteMatch<string, AgnosticDataRouteObject>, "route"> {
|
|
420
|
-
route: LazyRoutePromise;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
431
|
function isIndexRoute(
|
|
424
432
|
route: AgnosticRouteObject
|
|
425
433
|
): route is AgnosticIndexRouteObject {
|
|
@@ -504,16 +512,14 @@ export function matchRoutes<
|
|
|
504
512
|
|
|
505
513
|
let matches = null;
|
|
506
514
|
for (let i = 0; matches == null && i < branches.length; ++i) {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
safelyDecodeURI(pathname)
|
|
516
|
-
);
|
|
515
|
+
// Incoming pathnames are generally encoded from either window.location
|
|
516
|
+
// or from router.navigate, but we want to match against the unencoded
|
|
517
|
+
// paths in the route definitions. Memory router locations won't be
|
|
518
|
+
// encoded here but there also shouldn't be anything to decode so this
|
|
519
|
+
// should be a safe operation. This avoids needing matchRoutes to be
|
|
520
|
+
// history-aware.
|
|
521
|
+
let decoded = decodePath(pathname);
|
|
522
|
+
matches = matchRouteBranch<string, RouteObjectType>(branches[i], decoded);
|
|
517
523
|
}
|
|
518
524
|
|
|
519
525
|
return matches;
|
|
@@ -704,7 +710,7 @@ function rankRouteBranches(branches: RouteBranch[]): void {
|
|
|
704
710
|
);
|
|
705
711
|
}
|
|
706
712
|
|
|
707
|
-
const paramRe =
|
|
713
|
+
const paramRe = /^:[\w-]+$/;
|
|
708
714
|
const dynamicSegmentValue = 3;
|
|
709
715
|
const indexRouteValue = 2;
|
|
710
716
|
const emptySegmentValue = 1;
|
|
@@ -841,7 +847,7 @@ export function generatePath<Path extends string>(
|
|
|
841
847
|
return stringify(params[star]);
|
|
842
848
|
}
|
|
843
849
|
|
|
844
|
-
const keyMatch = segment.match(/^:(\w+)(\??)$/);
|
|
850
|
+
const keyMatch = segment.match(/^:([\w-]+)(\??)$/);
|
|
845
851
|
if (keyMatch) {
|
|
846
852
|
const [, key, optional] = keyMatch;
|
|
847
853
|
let param = params[key as PathParam<Path>];
|
|
@@ -949,7 +955,7 @@ export function matchPath<
|
|
|
949
955
|
if (isOptional && !value) {
|
|
950
956
|
memo[paramName] = undefined;
|
|
951
957
|
} else {
|
|
952
|
-
memo[paramName] =
|
|
958
|
+
memo[paramName] = (value || "").replace(/%2F/g, "/");
|
|
953
959
|
}
|
|
954
960
|
return memo;
|
|
955
961
|
},
|
|
@@ -986,10 +992,13 @@ function compilePath(
|
|
|
986
992
|
.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
|
|
987
993
|
.replace(/^\/*/, "/") // Make sure it has a leading /
|
|
988
994
|
.replace(/[\\.*+^${}|()[\]]/g, "\\$&") // Escape special regex chars
|
|
989
|
-
.replace(
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
995
|
+
.replace(
|
|
996
|
+
/\/:([\w-]+)(\?)?/g,
|
|
997
|
+
(_: string, paramName: string, isOptional) => {
|
|
998
|
+
params.push({ paramName, isOptional: isOptional != null });
|
|
999
|
+
return isOptional ? "/?([^\\/]+)?" : "/([^\\/]+)";
|
|
1000
|
+
}
|
|
1001
|
+
);
|
|
993
1002
|
|
|
994
1003
|
if (path.endsWith("*")) {
|
|
995
1004
|
params.push({ paramName: "*" });
|
|
@@ -1018,9 +1027,12 @@ function compilePath(
|
|
|
1018
1027
|
return [matcher, params];
|
|
1019
1028
|
}
|
|
1020
1029
|
|
|
1021
|
-
function
|
|
1030
|
+
function decodePath(value: string) {
|
|
1022
1031
|
try {
|
|
1023
|
-
return
|
|
1032
|
+
return value
|
|
1033
|
+
.split("/")
|
|
1034
|
+
.map((v) => decodeURIComponent(v).replace(/\//g, "%2F"))
|
|
1035
|
+
.join("/");
|
|
1024
1036
|
} catch (error) {
|
|
1025
1037
|
warning(
|
|
1026
1038
|
false,
|
|
@@ -1033,21 +1045,6 @@ function safelyDecodeURI(value: string) {
|
|
|
1033
1045
|
}
|
|
1034
1046
|
}
|
|
1035
1047
|
|
|
1036
|
-
function safelyDecodeURIComponent(value: string, paramName: string) {
|
|
1037
|
-
try {
|
|
1038
|
-
return decodeURIComponent(value);
|
|
1039
|
-
} catch (error) {
|
|
1040
|
-
warning(
|
|
1041
|
-
false,
|
|
1042
|
-
`The value for the URL param "${paramName}" will not be decoded because` +
|
|
1043
|
-
` the string "${value}" is a malformed URL segment. This is probably` +
|
|
1044
|
-
` due to a bad percent encoding (${error}).`
|
|
1045
|
-
);
|
|
1046
|
-
|
|
1047
|
-
return value;
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
1048
|
/**
|
|
1052
1049
|
* @private
|
|
1053
1050
|
*/
|