@tanstack/react-router 0.0.1-beta.207 → 0.0.1-beta.209

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/src/route.ts CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  UnionToIntersection,
12
12
  Assign,
13
13
  } from './utils'
14
- import { ParsePathParams, ToSubOptions } from './link'
14
+ import { NavigateOptions, ParsePathParams, ToSubOptions } from './link'
15
15
  import {
16
16
  ErrorRouteComponent,
17
17
  PendingRouteComponent,
@@ -29,6 +29,7 @@ export type RootRouteId = typeof rootRouteId
29
29
  export type AnyPathParams = {}
30
30
  export type AnySearchSchema = {}
31
31
  export type AnyContext = {}
32
+ export interface RouteContext {}
32
33
  export interface RouteMeta {}
33
34
 
34
35
  export type PreloadableObj = { preload?: () => Promise<void> }
@@ -44,13 +45,13 @@ export type RoutePathOptions<TCustomId, TPath> =
44
45
  export type RoutePathOptionsIntersection<TCustomId, TPath> =
45
46
  UnionToIntersection<RoutePathOptions<TCustomId, TPath>>
46
47
 
47
- // export type MetaOptions = keyof PickRequired<RouteMeta> extends never
48
- // ? {
49
- // meta?: RouteMeta
50
- // }
51
- // : {
52
- // meta: RouteMeta
53
- // }
48
+ export type MetaOptions = keyof PickRequired<RouteMeta> extends never
49
+ ? {
50
+ meta?: RouteMeta
51
+ }
52
+ : {
53
+ meta: RouteMeta
54
+ }
54
55
 
55
56
  export type RouteOptions<
56
57
  TParentRoute extends AnyRoute = AnyRoute,
@@ -60,8 +61,8 @@ export type RouteOptions<
60
61
  TFullSearchSchema extends Record<string, any> = TSearchSchema,
61
62
  TParams extends AnyPathParams = AnyPathParams,
62
63
  TAllParams extends AnyPathParams = TParams,
63
- TRouteMeta extends RouteMeta = RouteMeta,
64
- TAllMeta extends Record<string, any> = AnyContext,
64
+ TRouteContext extends RouteContext = RouteContext,
65
+ TAllContext extends Record<string, any> = AnyContext,
65
66
  > = BaseRouteOptions<
66
67
  TParentRoute,
67
68
  TCustomId,
@@ -70,10 +71,10 @@ export type RouteOptions<
70
71
  TFullSearchSchema,
71
72
  TParams,
72
73
  TAllParams,
73
- TRouteMeta,
74
- TAllMeta
74
+ TRouteContext,
75
+ TAllContext
75
76
  > &
76
- NoInfer<UpdatableRouteOptions<TFullSearchSchema, TAllParams, TAllMeta>>
77
+ NoInfer<UpdatableRouteOptions<TFullSearchSchema, TAllParams, TAllContext>>
77
78
 
78
79
  export type ParamsFallback<
79
80
  TPath extends string,
@@ -92,12 +93,12 @@ export type BaseRouteOptions<
92
93
  TFullSearchSchema extends Record<string, any> = TSearchSchema,
93
94
  TParams extends AnyPathParams = {},
94
95
  TAllParams = ParamsFallback<TPath, TParams>,
95
- TRouteMeta extends RouteMeta = RouteMeta,
96
+ TRouteContext extends RouteContext = RouteContext,
96
97
  TAllContext extends Record<string, any> = AnyContext,
97
98
  > = RoutePathOptions<TCustomId, TPath> & {
98
99
  getParentRoute: () => TParentRoute
99
100
  validateSearch?: SearchSchemaValidator<TSearchSchema>
100
- } & (keyof PickRequired<RouteMeta> extends never
101
+ } & (keyof PickRequired<RouteContext> extends never
101
102
  ? // This async function is called before a route is loaded.
102
103
  // If an error is thrown here, the route's loader will not be called.
103
104
  // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
@@ -107,7 +108,7 @@ export type BaseRouteOptions<
107
108
  TFullSearchSchema,
108
109
  TParentRoute,
109
110
  TAllParams,
110
- TRouteMeta
111
+ TRouteContext
111
112
  >
112
113
  }
113
114
  : {
@@ -115,14 +116,14 @@ export type BaseRouteOptions<
115
116
  TFullSearchSchema,
116
117
  TParentRoute,
117
118
  TAllParams,
118
- TRouteMeta
119
+ TRouteContext
119
120
  >
120
121
  }) & {
121
122
  load?: RouteLoadFn<
122
123
  TAllParams,
123
124
  TFullSearchSchema,
124
125
  NoInfer<TAllContext>,
125
- NoInfer<TRouteMeta>
126
+ NoInfer<TRouteContext>
126
127
  >
127
128
  } & (
128
129
  | {
@@ -146,60 +147,59 @@ type BeforeLoadFn<
146
147
  TFullSearchSchema extends Record<string, any>,
147
148
  TParentRoute extends AnyRoute,
148
149
  TAllParams,
149
- TRouteMeta,
150
+ TRouteContext,
150
151
  > = (opts: {
151
152
  search: TFullSearchSchema
152
153
  abortController: AbortController
153
154
  preload: boolean
154
155
  params: TAllParams
155
- meta: TParentRoute['types']['allMeta']
156
+ context: TParentRoute['types']['allContext']
156
157
  location: ParsedLocation
157
- }) => Promise<TRouteMeta> | TRouteMeta | void
158
+ navigate: (opts: NavigateOptions<AnyRoute>) => Promise<void>
159
+ }) => Promise<TRouteContext> | TRouteContext | void
158
160
 
159
161
  export type UpdatableRouteOptions<
160
162
  TFullSearchSchema extends Record<string, any>,
161
163
  TAllParams extends AnyPathParams,
162
164
  TAllContext extends AnyContext,
163
- > =
164
- // MetaOptions &
165
- {
166
- // test?: (args: TAllContext) => void
167
- // If true, this route will be matched as case-sensitive
168
- caseSensitive?: boolean
169
- // If true, this route will be forcefully wrapped in a suspense boundary
170
- wrapInSuspense?: boolean
171
- // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`
172
- component?: RouteComponent<TFullSearchSchema, TAllParams, TAllContext>
173
- // The content to be rendered when the route encounters an error
174
- errorComponent?: ErrorRouteComponent<
175
- TFullSearchSchema,
176
- TAllParams,
177
- {}
178
- // TAllContext // TODO: I have no idea why this breaks the universe,
179
- // so we'll come back to it later.
180
- > //
181
- // If supported by your framework, the content to be rendered as the fallback content until the route is ready to render
182
- pendingComponent?: PendingRouteComponent<
183
- TFullSearchSchema,
184
- TAllParams,
185
- TAllContext
186
- >
187
- // Filter functions that can manipulate search params *before* they are passed to links and navigate
188
- // calls that match this route.
189
- preSearchFilters?: SearchFilter<TFullSearchSchema>[]
190
- // Filter functions that can manipulate search params *after* they are passed to links and navigate
191
- // calls that match this route.
192
- postSearchFilters?: SearchFilter<TFullSearchSchema>[]
193
- onError?: (err: any) => void
194
- // These functions are called as route matches are loaded, stick around and leave the active
195
- // matches
196
- onEnter?: (match: AnyRouteMatch) => void
197
- onTransition?: (match: AnyRouteMatch) => void
198
- onLeave?: (match: AnyRouteMatch) => void
199
- // Set this to true or false to specifically set whether or not this route should be preloaded. If unset, will
200
- // default to router.options.reloadOnWindowFocus
201
- reloadOnWindowFocus?: boolean
202
- }
165
+ > = MetaOptions & {
166
+ // test?: (args: TAllContext) => void
167
+ // If true, this route will be matched as case-sensitive
168
+ caseSensitive?: boolean
169
+ // If true, this route will be forcefully wrapped in a suspense boundary
170
+ wrapInSuspense?: boolean
171
+ // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`
172
+ component?: RouteComponent<TFullSearchSchema, TAllParams, TAllContext>
173
+ // The content to be rendered when the route encounters an error
174
+ errorComponent?: ErrorRouteComponent<
175
+ TFullSearchSchema,
176
+ TAllParams,
177
+ {}
178
+ // TAllContext // TODO: I have no idea why this breaks the universe,
179
+ // so we'll come back to it later.
180
+ > //
181
+ // If supported by your framework, the content to be rendered as the fallback content until the route is ready to render
182
+ pendingComponent?: PendingRouteComponent<
183
+ TFullSearchSchema,
184
+ TAllParams,
185
+ TAllContext
186
+ >
187
+ // Filter functions that can manipulate search params *before* they are passed to links and navigate
188
+ // calls that match this route.
189
+ preSearchFilters?: SearchFilter<TFullSearchSchema>[]
190
+ // Filter functions that can manipulate search params *after* they are passed to links and navigate
191
+ // calls that match this route.
192
+ postSearchFilters?: SearchFilter<TFullSearchSchema>[]
193
+ onError?: (err: any) => void
194
+ // These functions are called as route matches are loaded, stick around and leave the active
195
+ // matches
196
+ onEnter?: (match: AnyRouteMatch) => void
197
+ onTransition?: (match: AnyRouteMatch) => void
198
+ onLeave?: (match: AnyRouteMatch) => void
199
+ // Set this to true or false to specifically set whether or not this route should be preloaded. If unset, will
200
+ // default to router.options.reloadOnWindowFocus
201
+ reloadOnWindowFocus?: boolean
202
+ }
203
203
 
204
204
  export type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn<
205
205
  TPath,
@@ -242,13 +242,13 @@ export type RouteLoadFn<
242
242
  TAllParams = {},
243
243
  TFullSearchSchema extends Record<string, any> = {},
244
244
  TAllContext extends Record<string, any> = AnyContext,
245
- TRouteMeta extends Record<string, any> = AnyContext,
245
+ TRouteContext extends Record<string, any> = AnyContext,
246
246
  > = (
247
247
  match: LoadFnContext<
248
248
  TAllParams,
249
249
  TFullSearchSchema,
250
250
  TAllContext,
251
- TRouteMeta
251
+ TRouteContext
252
252
  > & {
253
253
  parentMatchPromise?: Promise<void>
254
254
  },
@@ -258,13 +258,15 @@ export interface LoadFnContext<
258
258
  TAllParams = {},
259
259
  TFullSearchSchema extends Record<string, any> = {},
260
260
  TAllContext extends Record<string, any> = AnyContext,
261
- TRouteMeta extends Record<string, any> = AnyContext,
261
+ TRouteContext extends Record<string, any> = AnyContext,
262
262
  > {
263
263
  abortController: AbortController
264
264
  preload: boolean
265
265
  params: TAllParams
266
266
  search: TFullSearchSchema
267
- meta: Expand<Assign<TAllContext, TRouteMeta>>
267
+ context: Expand<Assign<TAllContext, TRouteContext>>
268
+ location: ParsedLocation<TFullSearchSchema>
269
+ navigate: (opts: NavigateOptions<AnyRoute>) => Promise<void>
268
270
  }
269
271
 
270
272
  export type SearchFilter<T, U = T> = (prev: T) => U
@@ -336,9 +338,9 @@ export type RouteConstraints = {
336
338
  TParams: Record<string, any>
337
339
  TAllParams: Record<string, any>
338
340
  TParentContext: AnyContext
339
- TRouteMeta: RouteMeta
341
+ TRouteContext: RouteContext
340
342
  TAllContext: AnyContext
341
- TRouterMeta: AnyContext
343
+ TRouterContext: AnyContext
342
344
  TChildren: unknown
343
345
  TRouteTree: AnyRoute
344
346
  }
@@ -368,11 +370,13 @@ export class Route<
368
370
  TParentRoute,
369
371
  TParams
370
372
  >,
371
- TRouteMeta extends RouteConstraints['TRouteMeta'] = RouteMeta,
372
- TAllMeta extends Expand<
373
- Assign<IsAny<TParentRoute['types']['allMeta'], {}>, TRouteMeta>
374
- > = Expand<Assign<IsAny<TParentRoute['types']['allMeta'], {}>, TRouteMeta>>,
375
- TRouterMeta extends RouteConstraints['TRouterMeta'] = AnyContext,
373
+ TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
374
+ TAllContext extends Expand<
375
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
376
+ > = Expand<
377
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
378
+ >,
379
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
376
380
  TChildren extends RouteConstraints['TChildren'] = unknown,
377
381
  TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
378
382
  > {
@@ -385,11 +389,13 @@ export class Route<
385
389
  TFullSearchSchema,
386
390
  TParams,
387
391
  TAllParams,
388
- TRouteMeta,
389
- TAllMeta
392
+ TRouteContext,
393
+ TAllContext
390
394
  >
391
395
 
392
- test!: Expand<Assign<IsAny<TParentRoute['types']['allMeta'], {}>, TRouteMeta>>
396
+ test!: Expand<
397
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
398
+ >
393
399
 
394
400
  // Set up in this.init()
395
401
  parentRoute!: TParentRoute
@@ -414,8 +420,8 @@ export class Route<
414
420
  TFullSearchSchema,
415
421
  TParams,
416
422
  TAllParams,
417
- TRouteMeta,
418
- TAllMeta
423
+ TRouteContext,
424
+ TAllContext
419
425
  >,
420
426
  ) {
421
427
  this.options = (options as any) || {}
@@ -434,11 +440,11 @@ export class Route<
434
440
  fullSearchSchema: TFullSearchSchema
435
441
  params: TParams
436
442
  allParams: TAllParams
437
- routeMeta: TRouteMeta
438
- allMeta: TAllMeta
443
+ routeContext: TRouteContext
444
+ allContext: TAllContext
439
445
  children: TChildren
440
446
  routeTree: TRouteTree
441
- routerMeta: TRouterMeta
447
+ routerContext: TRouterContext
442
448
  }
443
449
 
444
450
  init = (opts: { originalIndex: number }) => {
@@ -452,8 +458,8 @@ export class Route<
452
458
  TFullSearchSchema,
453
459
  TParams,
454
460
  TAllParams,
455
- TRouteMeta,
456
- TAllMeta
461
+ TRouteContext,
462
+ TAllContext
457
463
  > &
458
464
  RoutePathOptionsIntersection<TCustomId, TPath>
459
465
 
@@ -519,9 +525,9 @@ export class Route<
519
525
  TFullSearchSchema,
520
526
  TParams,
521
527
  TAllParams,
522
- TRouteMeta,
523
- TAllMeta,
524
- TRouterMeta,
528
+ TRouteContext,
529
+ TAllContext,
530
+ TRouterContext,
525
531
  TNewChildren,
526
532
  TRouteTree
527
533
  > => {
@@ -533,7 +539,9 @@ export class Route<
533
539
  options: UpdatableRouteOptions<
534
540
  TFullSearchSchema,
535
541
  TAllParams,
536
- Expand<Assign<IsAny<TParentRoute['types']['allMeta'], {}>, TRouteMeta>>
542
+ Expand<
543
+ Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
544
+ >
537
545
  >,
538
546
  ) => {
539
547
  Object.assign(this.options, options)
@@ -545,18 +553,18 @@ export class Route<
545
553
  // replaced by a framework specific implementation if necessary
546
554
  }
547
555
 
548
- useMatch = <TSelected = TAllMeta>(opts?: {
549
- select?: (search: TAllMeta) => TSelected
556
+ useMatch = <TSelected = TAllContext>(opts?: {
557
+ select?: (search: TAllContext) => TSelected
550
558
  }): TSelected => {
551
559
  return useMatch({ ...opts, from: this.id }) as any
552
560
  }
553
- useRouteMeta = <TSelected = TAllMeta>(opts?: {
554
- select?: (search: TAllMeta) => TSelected
561
+ useRouteContext = <TSelected = TAllContext>(opts?: {
562
+ select?: (search: TAllContext) => TSelected
555
563
  }): TSelected => {
556
564
  return useMatch({
557
565
  ...opts,
558
566
  from: this.id,
559
- select: (d: any) => (opts?.select ? opts.select(d.meta) : d.meta),
567
+ select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
560
568
  } as any)
561
569
  }
562
570
  useSearch = <TSelected = TFullSearchSchema>(opts?: {
@@ -573,12 +581,10 @@ export class Route<
573
581
 
574
582
  export type AnyRootRoute = RootRoute<any, any, any>
575
583
 
576
- export class RouterMeta<TRouterMeta extends {}> {
577
- constructor() {}
578
-
579
- createRootRoute = <
584
+ export function rootRouteWithContext<TRouterContext extends {}>() {
585
+ return <
580
586
  TSearchSchema extends Record<string, any> = {},
581
- TRouteMeta extends RouteMeta = RouteMeta,
587
+ TRouteContext extends RouteContext = RouteContext,
582
588
  >(
583
589
  options?: Omit<
584
590
  RouteOptions<
@@ -589,8 +595,8 @@ export class RouterMeta<TRouterMeta extends {}> {
589
595
  TSearchSchema, // TFullSearchSchema
590
596
  {}, // TParams
591
597
  {}, // TAllParams
592
- TRouteMeta, // TRouteMeta
593
- Assign<TRouterMeta, TRouteMeta> // TAllContext
598
+ TRouteContext, // TRouteContext
599
+ Assign<TRouterContext, TRouteContext> // TAllContext
594
600
  >,
595
601
  | 'path'
596
602
  | 'id'
@@ -599,15 +605,15 @@ export class RouterMeta<TRouterMeta extends {}> {
599
605
  | 'parseParams'
600
606
  | 'stringifyParams'
601
607
  >,
602
- ): RootRoute<TSearchSchema, TRouteMeta, TRouterMeta> => {
608
+ ): RootRoute<TSearchSchema, TRouteContext, TRouterContext> => {
603
609
  return new RootRoute(options) as any
604
610
  }
605
611
  }
606
612
 
607
613
  export class RootRoute<
608
614
  TSearchSchema extends Record<string, any> = {},
609
- TRouteMeta extends RouteMeta = RouteMeta,
610
- TRouterMeta extends {} = {},
615
+ TRouteContext extends RouteContext = RouteContext,
616
+ TRouterContext extends {} = {},
611
617
  > extends Route<
612
618
  any, // TParentRoute
613
619
  '/', // TPath
@@ -618,9 +624,9 @@ export class RootRoute<
618
624
  TSearchSchema, // TFullSearchSchema
619
625
  {}, // TParams
620
626
  {}, // TAllParams
621
- TRouteMeta, // TRouteMeta
622
- Expand<Assign<TRouterMeta, TRouteMeta>>, // TAllContext
623
- TRouterMeta, // TRouterMeta
627
+ TRouteContext, // TRouteContext
628
+ Expand<Assign<TRouterContext, TRouteContext>>, // TAllContext
629
+ TRouterContext, // TRouterContext
624
630
  any, // TChildren
625
631
  any // TRouteTree
626
632
  > {
@@ -634,8 +640,8 @@ export class RootRoute<
634
640
  TSearchSchema, // TFullSearchSchema
635
641
  {}, // TParams
636
642
  {}, // TAllParams
637
- TRouteMeta, // TRouteMeta
638
- Assign<TRouterMeta, TRouteMeta> // TAllContext
643
+ TRouteContext, // TRouteContext
644
+ Assign<TRouterContext, TRouteContext> // TAllContext
639
645
  >,
640
646
  | 'path'
641
647
  | 'id'
package/src/router.ts CHANGED
@@ -50,12 +50,12 @@ export type HydrationCtx = {
50
50
  }
51
51
 
52
52
  export type RouterContextOptions<TRouteTree extends AnyRoute> =
53
- AnyContext extends TRouteTree['types']['routerMeta']
53
+ AnyContext extends TRouteTree['types']['routerContext']
54
54
  ? {
55
- meta?: TRouteTree['types']['routerMeta']
55
+ context?: TRouteTree['types']['routerContext']
56
56
  }
57
57
  : {
58
- meta: TRouteTree['types']['routerMeta']
58
+ context: TRouteTree['types']['routerContext']
59
59
  }
60
60
 
61
61
  export interface RouterOptions<
@@ -85,7 +85,7 @@ export interface RouterOptions<
85
85
  routeTree?: TRouteTree
86
86
  basepath?: string
87
87
  createRoute?: (opts: { route: AnyRoute; router: AnyRouter }) => void
88
- meta?: TRouteTree['types']['routerMeta']
88
+ context?: TRouteTree['types']['routerContext']
89
89
  // dehydrate?: () => TDehydrated
90
90
  // hydrate?: (dehydrated: TDehydrated) => void
91
91
  routeMasks?: RouteMask<TRouteTree>[]
@@ -174,7 +174,7 @@ export class Router<
174
174
  > {
175
175
  options: PickAsRequired<
176
176
  RouterOptions<TRouteTree, TDehydrated>,
177
- 'stringifySearch' | 'parseSearch' | 'meta'
177
+ 'stringifySearch' | 'parseSearch' | 'context'
178
178
  >
179
179
  routeTree: TRouteTree
180
180
  // dehydratedData?: TDehydrated
@@ -184,7 +184,7 @@ export class Router<
184
184
  constructor(options: RouterConstructorOptions<TRouteTree, TDehydrated>) {
185
185
  this.options = {
186
186
  defaultPreloadDelay: 50,
187
- meta: undefined!,
187
+ context: undefined!,
188
188
  ...options,
189
189
  stringifySearch: options?.stringifySearch ?? defaultStringifySearch,
190
190
  parseSearch: options?.parseSearch ?? defaultParseSearch,