@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.
@@ -0,0 +1,481 @@
1
+ import type { Location, Path, To } from "./history";
2
+ /**
3
+ * Map of routeId -> data returned from a loader/action/error
4
+ */
5
+ export interface RouteData {
6
+ [routeId: string]: any;
7
+ }
8
+ export declare enum ResultType {
9
+ data = "data",
10
+ deferred = "deferred",
11
+ redirect = "redirect",
12
+ error = "error"
13
+ }
14
+ /**
15
+ * Successful result from a loader or action
16
+ */
17
+ export interface SuccessResult {
18
+ type: ResultType.data;
19
+ data: any;
20
+ statusCode?: number;
21
+ headers?: Headers;
22
+ }
23
+ /**
24
+ * Successful defer() result from a loader or action
25
+ */
26
+ export interface DeferredResult {
27
+ type: ResultType.deferred;
28
+ deferredData: DeferredData;
29
+ statusCode?: number;
30
+ headers?: Headers;
31
+ }
32
+ /**
33
+ * Redirect result from a loader or action
34
+ */
35
+ export interface RedirectResult {
36
+ type: ResultType.redirect;
37
+ status: number;
38
+ location: string;
39
+ revalidate: boolean;
40
+ }
41
+ /**
42
+ * Unsuccessful result from a loader or action
43
+ */
44
+ export interface ErrorResult {
45
+ type: ResultType.error;
46
+ error: any;
47
+ headers?: Headers;
48
+ }
49
+ /**
50
+ * Result from a loader or action - potentially successful or unsuccessful
51
+ */
52
+ export declare type DataResult = SuccessResult | DeferredResult | RedirectResult | ErrorResult;
53
+ export declare type MutationFormMethod = "post" | "put" | "patch" | "delete";
54
+ export declare type FormMethod = "get" | MutationFormMethod;
55
+ export declare type FormEncType = "application/x-www-form-urlencoded" | "multipart/form-data";
56
+ /**
57
+ * @private
58
+ * Internal interface to pass around for action submissions, not intended for
59
+ * external consumption
60
+ */
61
+ export interface Submission {
62
+ formMethod: FormMethod;
63
+ formAction: string;
64
+ formEncType: FormEncType;
65
+ formData: FormData;
66
+ }
67
+ /**
68
+ * @private
69
+ * Arguments passed to route loader/action functions. Same for now but we keep
70
+ * this as a private implementation detail in case they diverge in the future.
71
+ */
72
+ interface DataFunctionArgs {
73
+ request: Request;
74
+ params: Params;
75
+ context?: any;
76
+ }
77
+ declare type DataFunctionReturnValue = Promise<Response> | Response | Promise<any> | any;
78
+ /**
79
+ * Arguments passed to loader functions
80
+ */
81
+ export interface LoaderFunctionArgs extends DataFunctionArgs {
82
+ }
83
+ /**
84
+ * Arguments passed to action functions
85
+ */
86
+ export interface ActionFunctionArgs extends DataFunctionArgs {
87
+ }
88
+ /**
89
+ * Route loader function signature
90
+ */
91
+ export interface LoaderFunction {
92
+ (args: LoaderFunctionArgs): DataFunctionReturnValue;
93
+ }
94
+ /**
95
+ * Route action function signature
96
+ */
97
+ export interface ActionFunction {
98
+ (args: ActionFunctionArgs): DataFunctionReturnValue;
99
+ }
100
+ /**
101
+ * @private
102
+ * Arguments passed to route loader/action functions when middleware is enabled.
103
+ */
104
+ interface DataFunctionArgsWithMiddleware {
105
+ request: Request;
106
+ params: Params;
107
+ context: MiddlewareContext;
108
+ }
109
+ /**
110
+ * Arguments passed to middleware functions when middleware is enabled
111
+ */
112
+ export interface MiddlewareFunctionArgs extends DataFunctionArgsWithMiddleware {
113
+ }
114
+ /**
115
+ * Route loader function signature when middleware is enabled
116
+ */
117
+ export interface MiddlewareFunction {
118
+ (args: MiddlewareFunctionArgs): DataFunctionReturnValue;
119
+ }
120
+ /**
121
+ * Arguments passed to loader functions when middleware is enabled
122
+ */
123
+ export interface LoaderFunctionArgsWithMiddleware extends DataFunctionArgsWithMiddleware {
124
+ }
125
+ /**
126
+ * Route loader function signature when middleware is enabled
127
+ */
128
+ export interface LoaderFunctionWithMiddleware {
129
+ (args: LoaderFunctionArgsWithMiddleware): DataFunctionReturnValue;
130
+ }
131
+ /**
132
+ * Arguments passed to action functions when middleware is enabled
133
+ */
134
+ export interface ActionFunctionArgsWithMiddleware extends DataFunctionArgsWithMiddleware {
135
+ }
136
+ /**
137
+ * Route action function signature when middleware is enabled
138
+ */
139
+ export interface ActionFunctionWithMiddleware {
140
+ (args: ActionFunctionArgsWithMiddleware): DataFunctionReturnValue;
141
+ }
142
+ /**
143
+ * Route shouldRevalidate function signature. This runs after any submission
144
+ * (navigation or fetcher), so we flatten the navigation/fetcher submission
145
+ * onto the arguments. It shouldn't matter whether it came from a navigation
146
+ * or a fetcher, what really matters is the URLs and the formData since loaders
147
+ * have to re-run based on the data models that were potentially mutated.
148
+ */
149
+ export interface ShouldRevalidateFunction {
150
+ (args: {
151
+ currentUrl: URL;
152
+ currentParams: AgnosticDataRouteMatch["params"];
153
+ nextUrl: URL;
154
+ nextParams: AgnosticDataRouteMatch["params"];
155
+ formMethod?: Submission["formMethod"];
156
+ formAction?: Submission["formAction"];
157
+ formEncType?: Submission["formEncType"];
158
+ formData?: Submission["formData"];
159
+ actionResult?: DataResult;
160
+ defaultShouldRevalidate: boolean;
161
+ }): boolean;
162
+ }
163
+ /**
164
+ * Base RouteObject with common props shared by all types of routes
165
+ */
166
+ declare type AgnosticBaseRouteObject = {
167
+ caseSensitive?: boolean;
168
+ path?: string;
169
+ id?: string;
170
+ middleware?: MiddlewareFunction;
171
+ loader?: LoaderFunction | LoaderFunctionWithMiddleware;
172
+ action?: ActionFunction | ActionFunctionWithMiddleware;
173
+ hasErrorBoundary?: boolean;
174
+ shouldRevalidate?: ShouldRevalidateFunction;
175
+ handle?: any;
176
+ };
177
+ /**
178
+ * Index routes must not have children
179
+ */
180
+ export declare type AgnosticIndexRouteObject = AgnosticBaseRouteObject & {
181
+ children?: undefined;
182
+ index: true;
183
+ };
184
+ /**
185
+ * Non-index routes may have children, but cannot have index
186
+ */
187
+ export declare type AgnosticNonIndexRouteObject = AgnosticBaseRouteObject & {
188
+ children?: AgnosticRouteObject[];
189
+ index?: false;
190
+ };
191
+ /**
192
+ * A route object represents a logical route, with (optionally) its child
193
+ * routes organized in a tree-like structure.
194
+ */
195
+ export declare type AgnosticRouteObject = AgnosticIndexRouteObject | AgnosticNonIndexRouteObject;
196
+ export declare type AgnosticDataIndexRouteObject = AgnosticIndexRouteObject & {
197
+ id: string;
198
+ };
199
+ export declare type AgnosticDataNonIndexRouteObject = AgnosticNonIndexRouteObject & {
200
+ children?: AgnosticDataRouteObject[];
201
+ id: string;
202
+ };
203
+ /**
204
+ * A data route object, which is just a RouteObject with a required unique ID
205
+ */
206
+ export declare type AgnosticDataRouteObject = AgnosticDataIndexRouteObject | AgnosticDataNonIndexRouteObject;
207
+ declare type _PathParam<Path extends string> = Path extends `${infer L}/${infer R}` ? _PathParam<L> | _PathParam<R> : Path extends `:${infer Param}` ? Param extends `${infer Optional}?` ? Optional : Param : never;
208
+ /**
209
+ * Examples:
210
+ * "/a/b/*" -> "*"
211
+ * ":a" -> "a"
212
+ * "/a/:b" -> "b"
213
+ * "/a/blahblahblah:b" -> "b"
214
+ * "/:a/:b" -> "a" | "b"
215
+ * "/:a/b/:c/*" -> "a" | "c" | "*"
216
+ */
217
+ declare type PathParam<Path extends string> = Path extends "*" ? "*" : Path extends `${infer Rest}/*` ? "*" | _PathParam<Rest> : _PathParam<Path>;
218
+ export declare type ParamParseKey<Segment extends string> = [
219
+ PathParam<Segment>
220
+ ] extends [never] ? string : PathParam<Segment>;
221
+ /**
222
+ * The parameters that were parsed from the URL path.
223
+ */
224
+ export declare type Params<Key extends string = string> = {
225
+ readonly [key in Key]: string | undefined;
226
+ };
227
+ /**
228
+ * A RouteMatch contains info about how a route matched a URL.
229
+ */
230
+ export interface AgnosticRouteMatch<ParamKey extends string = string, RouteObjectType extends AgnosticRouteObject = AgnosticRouteObject> {
231
+ /**
232
+ * The names and values of dynamic parameters in the URL.
233
+ */
234
+ params: Params<ParamKey>;
235
+ /**
236
+ * The portion of the URL pathname that was matched.
237
+ */
238
+ pathname: string;
239
+ /**
240
+ * The portion of the URL pathname that was matched before child routes.
241
+ */
242
+ pathnameBase: string;
243
+ /**
244
+ * The route object that was used to match.
245
+ */
246
+ route: RouteObjectType;
247
+ }
248
+ export interface AgnosticDataRouteMatch extends AgnosticRouteMatch<string, AgnosticDataRouteObject> {
249
+ }
250
+ export declare function convertRoutesToDataRoutes(routes: AgnosticRouteObject[], parentPath?: number[], allIds?: Set<string>): AgnosticDataRouteObject[];
251
+ /**
252
+ * Matches the given routes to a location and returns the match data.
253
+ *
254
+ * @see https://reactrouter.com/utils/match-routes
255
+ */
256
+ export declare function matchRoutes<RouteObjectType extends AgnosticRouteObject = AgnosticRouteObject>(routes: RouteObjectType[], locationArg: Partial<Location> | string, basename?: string): AgnosticRouteMatch<string, RouteObjectType>[] | null;
257
+ /**
258
+ * Returns a path with params interpolated.
259
+ *
260
+ * @see https://reactrouter.com/utils/generate-path
261
+ */
262
+ export declare function generatePath<Path extends string>(originalPath: Path, params?: {
263
+ [key in PathParam<Path>]: string | null;
264
+ }): string;
265
+ /**
266
+ * A PathPattern is used to match on some portion of a URL pathname.
267
+ */
268
+ export interface PathPattern<Path extends string = string> {
269
+ /**
270
+ * A string to match against a URL pathname. May contain `:id`-style segments
271
+ * to indicate placeholders for dynamic parameters. May also end with `/*` to
272
+ * indicate matching the rest of the URL pathname.
273
+ */
274
+ path: Path;
275
+ /**
276
+ * Should be `true` if the static portions of the `path` should be matched in
277
+ * the same case.
278
+ */
279
+ caseSensitive?: boolean;
280
+ /**
281
+ * Should be `true` if this pattern should match the entire URL pathname.
282
+ */
283
+ end?: boolean;
284
+ }
285
+ /**
286
+ * A PathMatch contains info about how a PathPattern matched on a URL pathname.
287
+ */
288
+ export interface PathMatch<ParamKey extends string = string> {
289
+ /**
290
+ * The names and values of dynamic parameters in the URL.
291
+ */
292
+ params: Params<ParamKey>;
293
+ /**
294
+ * The portion of the URL pathname that was matched.
295
+ */
296
+ pathname: string;
297
+ /**
298
+ * The portion of the URL pathname that was matched before child routes.
299
+ */
300
+ pathnameBase: string;
301
+ /**
302
+ * The pattern that was used to match.
303
+ */
304
+ pattern: PathPattern;
305
+ }
306
+ /**
307
+ * Performs pattern matching on a URL pathname and returns information about
308
+ * the match.
309
+ *
310
+ * @see https://reactrouter.com/utils/match-path
311
+ */
312
+ export declare function matchPath<ParamKey extends ParamParseKey<Path>, Path extends string>(pattern: PathPattern<Path> | Path, pathname: string): PathMatch<ParamKey> | null;
313
+ /**
314
+ * @private
315
+ */
316
+ export declare function stripBasename(pathname: string, basename: string): string | null;
317
+ /**
318
+ * @private
319
+ */
320
+ export declare function warning(cond: any, message: string): void;
321
+ /**
322
+ * Returns a resolved path object relative to the given pathname.
323
+ *
324
+ * @see https://reactrouter.com/utils/resolve-path
325
+ */
326
+ export declare function resolvePath(to: To, fromPathname?: string): Path;
327
+ /**
328
+ * @private
329
+ *
330
+ * When processing relative navigation we want to ignore ancestor routes that
331
+ * do not contribute to the path, such that index/pathless layout routes don't
332
+ * interfere.
333
+ *
334
+ * For example, when moving a route element into an index route and/or a
335
+ * pathless layout route, relative link behavior contained within should stay
336
+ * the same. Both of the following examples should link back to the root:
337
+ *
338
+ * <Route path="/">
339
+ * <Route path="accounts" element={<Link to=".."}>
340
+ * </Route>
341
+ *
342
+ * <Route path="/">
343
+ * <Route path="accounts">
344
+ * <Route element={<AccountsLayout />}> // <-- Does not contribute
345
+ * <Route index element={<Link to=".."} /> // <-- Does not contribute
346
+ * </Route
347
+ * </Route>
348
+ * </Route>
349
+ */
350
+ export declare function getPathContributingMatches<T extends AgnosticRouteMatch = AgnosticRouteMatch>(matches: T[]): T[];
351
+ /**
352
+ * @private
353
+ */
354
+ export declare function resolveTo(toArg: To, routePathnames: string[], locationPathname: string, isPathRelative?: boolean): Path;
355
+ /**
356
+ * @private
357
+ */
358
+ export declare function getToPathname(to: To): string | undefined;
359
+ /**
360
+ * @private
361
+ */
362
+ export declare const joinPaths: (paths: string[]) => string;
363
+ /**
364
+ * @private
365
+ */
366
+ export declare const normalizePathname: (pathname: string) => string;
367
+ /**
368
+ * @private
369
+ */
370
+ export declare const normalizeSearch: (search: string) => string;
371
+ /**
372
+ * @private
373
+ */
374
+ export declare const normalizeHash: (hash: string) => string;
375
+ export declare type JsonFunction = <Data>(data: Data, init?: number | ResponseInit) => Response;
376
+ /**
377
+ * This is a shortcut for creating `application/json` responses. Converts `data`
378
+ * to JSON and sets the `Content-Type` header.
379
+ */
380
+ export declare const json: JsonFunction;
381
+ export interface TrackedPromise extends Promise<any> {
382
+ _tracked?: boolean;
383
+ _data?: any;
384
+ _error?: any;
385
+ }
386
+ export declare class AbortedDeferredError extends Error {
387
+ }
388
+ export declare class DeferredData {
389
+ private pendingKeysSet;
390
+ private controller;
391
+ private abortPromise;
392
+ private unlistenAbortSignal;
393
+ private subscribers;
394
+ data: Record<string, unknown>;
395
+ init?: ResponseInit;
396
+ deferredKeys: string[];
397
+ constructor(data: Record<string, unknown>, responseInit?: ResponseInit);
398
+ private trackPromise;
399
+ private onSettle;
400
+ private emit;
401
+ subscribe(fn: (aborted: boolean, settledKey?: string) => void): () => boolean;
402
+ cancel(): void;
403
+ resolveData(signal: AbortSignal): Promise<boolean>;
404
+ get done(): boolean;
405
+ get unwrappedData(): {};
406
+ get pendingKeys(): string[];
407
+ }
408
+ export declare type DeferFunction = (data: Record<string, unknown>, init?: number | ResponseInit) => DeferredData;
409
+ export declare const defer: DeferFunction;
410
+ export declare type RedirectFunction = (url: string, init?: number | ResponseInit) => Response;
411
+ /**
412
+ * A redirect response. Sets the status code and the `Location` header.
413
+ * Defaults to "302 Found".
414
+ */
415
+ export declare const redirect: RedirectFunction;
416
+ /**
417
+ * @private
418
+ * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
419
+ */
420
+ export declare class ErrorResponse {
421
+ status: number;
422
+ statusText: string;
423
+ data: any;
424
+ error?: Error;
425
+ internal: boolean;
426
+ constructor(status: number, statusText: string | undefined, data: any, internal?: boolean);
427
+ }
428
+ /**
429
+ * Check if the given error is an ErrorResponse generated from a 4xx/5xx
430
+ * Response thrown from an action/loader
431
+ */
432
+ export declare function isRouteErrorResponse(error: any): error is ErrorResponse;
433
+ /**
434
+ * Context object passed through middleware functions and into action/loaders.
435
+ *
436
+ * Supports only key/value for now, eventually will be enhanced
437
+ */
438
+ export interface MiddlewareContext {
439
+ /**
440
+ * Retrieve a value from context
441
+ */
442
+ get<T>(key: MiddlewareContextInstance<T>): T;
443
+ /**
444
+ * Set a value from context
445
+ */
446
+ set<T>(key: MiddlewareContextInstance<T>, value: T): void;
447
+ /**
448
+ * Call any child middlewares and the destination loader/action
449
+ */
450
+ next: () => DataFunctionReturnValue;
451
+ /**
452
+ * @internal
453
+ * PRIVATE - DO NOT USE
454
+ *
455
+ * Return the entries - needed so we can copy values from the serverMiddleware
456
+ * context into route-specific contexts
457
+ */
458
+ entries(): IterableIterator<[MiddlewareContextInstance<unknown>, unknown]>;
459
+ }
460
+ /**
461
+ * Generic class to "hold" a default middleware value and the generic type so
462
+ * we can enforce typings on middleware.get/set
463
+ */
464
+ export declare class MiddlewareContextInstance<T> {
465
+ private defaultValue;
466
+ constructor(defaultValue?: T);
467
+ getDefaultValue(): T;
468
+ }
469
+ /**
470
+ * Create a middleware context that can be used as a "key" to set/get middleware
471
+ * values in a strongly-typed fashion
472
+ */
473
+ export declare function createMiddlewareContext<T extends unknown>(defaultValue?: T): MiddlewareContextInstance<T>;
474
+ /**
475
+ * @internal
476
+ * PRIVATE - DO NOT USE
477
+ *
478
+ * Create a middleware "context" to store values and provide a next() hook
479
+ */
480
+ export declare function createMiddlewareStore(initialMiddlewareContext?: MiddlewareContext): MiddlewareContext;
481
+ export {};