@tanstack/router-core 0.0.1-beta.41 → 0.0.1-beta.49

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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/actions.js +94 -0
  3. package/build/cjs/actions.js.map +1 -0
  4. package/build/cjs/history.js +163 -0
  5. package/build/cjs/history.js.map +1 -0
  6. package/build/cjs/index.js +18 -20
  7. package/build/cjs/index.js.map +1 -1
  8. package/build/cjs/interop.js +175 -0
  9. package/build/cjs/interop.js.map +1 -0
  10. package/build/cjs/path.js +4 -5
  11. package/build/cjs/path.js.map +1 -1
  12. package/build/cjs/route.js +16 -138
  13. package/build/cjs/route.js.map +1 -1
  14. package/build/cjs/routeConfig.js +1 -7
  15. package/build/cjs/routeConfig.js.map +1 -1
  16. package/build/cjs/routeMatch.js +194 -199
  17. package/build/cjs/routeMatch.js.map +1 -1
  18. package/build/cjs/router.js +726 -703
  19. package/build/cjs/router.js.map +1 -1
  20. package/build/cjs/store.js +54 -0
  21. package/build/cjs/store.js.map +1 -0
  22. package/build/esm/index.js +1305 -1114
  23. package/build/esm/index.js.map +1 -1
  24. package/build/stats-html.html +1 -1
  25. package/build/stats-react.json +229 -192
  26. package/build/types/index.d.ts +172 -109
  27. package/build/umd/index.development.js +1381 -2331
  28. package/build/umd/index.development.js.map +1 -1
  29. package/build/umd/index.production.js +1 -1
  30. package/build/umd/index.production.js.map +1 -1
  31. package/package.json +4 -4
  32. package/src/actions.ts +157 -0
  33. package/src/history.ts +199 -0
  34. package/src/index.ts +4 -7
  35. package/src/interop.ts +169 -0
  36. package/src/link.ts +2 -2
  37. package/src/route.ts +34 -239
  38. package/src/routeConfig.ts +3 -34
  39. package/src/routeInfo.ts +6 -21
  40. package/src/routeMatch.ts +270 -285
  41. package/src/router.ts +967 -963
  42. package/src/store.ts +52 -0
  43. package/build/cjs/sharedClone.js +0 -122
  44. package/build/cjs/sharedClone.js.map +0 -1
  45. package/src/sharedClone.ts +0 -118
package/src/route.ts CHANGED
@@ -1,255 +1,50 @@
1
- import {
2
- CheckRelativePath,
3
- LinkInfo,
4
- LinkOptions,
5
- ResolveRelativePath,
6
- ToOptions,
7
- } from './link'
8
- import { LoaderContext, RouteConfig, RouteOptions } from './routeConfig'
1
+ import { Action, ActionOptions } from './actions'
2
+ import { RouteConfig, RouteOptions } from './routeConfig'
9
3
  import {
10
4
  AnyAllRouteInfo,
11
5
  AnyRouteInfo,
12
6
  DefaultAllRouteInfo,
13
7
  RouteInfo,
14
- RouteInfoByPath,
15
8
  } from './routeInfo'
16
- import {
17
- Action,
18
- ActionState,
19
- Loader,
20
- LoaderState,
21
- MatchRouteOptions,
22
- Router,
23
- } from './router'
24
- import { NoInfer } from './utils'
25
- import { createStore } from '@solidjs/reactivity'
9
+ import { Router } from './router'
26
10
 
27
11
  export interface AnyRoute extends Route<any, any, any> {}
28
12
 
29
- export interface Route<
13
+ export class Route<
30
14
  TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
31
15
  TRouteInfo extends AnyRouteInfo = RouteInfo,
32
16
  TRouterContext = unknown,
33
17
  > {
34
- routeInfo: TRouteInfo
35
- routeId: TRouteInfo['id']
36
- routeRouteId: TRouteInfo['routeId']
37
- routePath: TRouteInfo['path']
38
- fullPath: TRouteInfo['fullPath']
39
- parentRoute?: AnyRoute
18
+ routeInfo!: TRouteInfo
19
+ id!: TRouteInfo['id']
20
+ customId!: TRouteInfo['customId']
21
+ path!: TRouteInfo['path']
22
+ fullPath!: TRouteInfo['fullPath']
23
+ getParentRoute!: () => undefined | AnyRoute
40
24
  childRoutes?: AnyRoute[]
41
- options: RouteOptions
42
- originalIndex: number
43
- router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>
44
- action: unknown extends TRouteInfo['actionResponse']
45
- ?
46
- | Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
47
- | undefined
48
- : Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
49
- loader: unknown extends TRouteInfo['routeLoaderData']
50
- ?
51
- | Action<
52
- LoaderContext<
53
- TRouteInfo['fullSearchSchema'],
54
- TRouteInfo['allParams']
55
- >,
56
- TRouteInfo['routeLoaderData']
57
- >
58
- | undefined
59
- : Loader<
60
- TRouteInfo['fullSearchSchema'],
61
- TRouteInfo['allParams'],
62
- TRouteInfo['routeLoaderData']
63
- >
64
- // buildLink: <TTo extends string = '.'>(
65
- // options: Omit<
66
- // LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
67
- // 'from'
68
- // >,
69
- // ) => LinkInfo
70
- // matchRoute: <
71
- // TTo extends string = '.',
72
- // TResolved extends string = ResolveRelativePath<TRouteInfo['id'], TTo>,
73
- // >(
74
- // matchLocation: CheckRelativePath<
75
- // TAllRouteInfo,
76
- // TRouteInfo['fullPath'],
77
- // NoInfer<TTo>
78
- // > &
79
- // Omit<ToOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>, 'from'>,
80
- // opts?: MatchRouteOptions,
81
- // ) => RouteInfoByPath<TAllRouteInfo, TResolved>['allParams']
82
- // navigate: <TTo extends string = '.'>(
83
- // options: Omit<
84
- // LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
85
- // 'from'
86
- // >,
87
- // ) => Promise<void>
88
- }
89
-
90
- export function createRoute<
91
- TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
92
- TRouteInfo extends AnyRouteInfo = RouteInfo,
93
- TRouterContext = unknown,
94
- >(
95
- routeConfig: RouteConfig,
96
- options: TRouteInfo['options'],
97
- originalIndex: number,
98
- parent: undefined | Route<TAllRouteInfo, any>,
99
- router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>,
100
- ): Route<TAllRouteInfo, TRouteInfo, TRouterContext> {
101
- const { id, routeId, path: routePath, fullPath } = routeConfig
102
-
103
- let route: Route<TAllRouteInfo, TRouteInfo, TRouterContext> = {
104
- routeInfo: undefined!,
105
- routeId: id,
106
- routeRouteId: routeId,
107
- originalIndex,
108
- routePath,
109
- fullPath,
110
- options,
111
- router,
112
- childRoutes: undefined!,
113
- parentRoute: parent,
114
- get action() {
115
- let action =
116
- router.store.actions[id] ||
117
- (() => {
118
- router.setStore((s) => {
119
- s.actions[id] = {
120
- submissions: [],
121
- submit: async <T, U>(
122
- submission: T,
123
- actionOpts?: { invalidate?: boolean; multi?: boolean },
124
- ) => {
125
- if (!route) {
126
- return
127
- }
128
-
129
- const invalidate = actionOpts?.invalidate ?? true
130
-
131
- const [actionStore, setActionStore] = createStore<
132
- ActionState<T, U>
133
- >({
134
- submittedAt: Date.now(),
135
- status: 'pending',
136
- submission,
137
- isMulti: !!actionOpts?.multi,
138
- })
139
-
140
- router.setStore((s) => {
141
- if (!actionOpts?.multi) {
142
- s.actions[id]!.submissions = action.submissions.filter(
143
- (d) => d.isMulti,
144
- )
145
- }
146
-
147
- s.actions[id]!.current = actionStore
148
- s.actions[id]!.latest = actionStore
149
- s.actions[id]!.submissions.push(actionStore)
150
- })
151
-
152
- try {
153
- const res = await route.options.action?.(submission)
154
-
155
- setActionStore((s) => {
156
- s.data = res as U
157
- })
158
-
159
- if (invalidate) {
160
- router.invalidateRoute({ to: '.', fromCurrent: true })
161
- await router.reload()
162
- }
163
-
164
- setActionStore((s) => {
165
- s.status = 'success'
166
- })
167
-
168
- return res
169
- } catch (err) {
170
- console.error(err)
171
- setActionStore((s) => {
172
- s.error = err
173
- s.status = 'error'
174
- })
175
- }
176
- },
177
- }
178
- })
179
-
180
- return router.store.actions[id]!
181
- })()
182
-
183
- return action
184
- },
185
- get loader() {
186
- let loader =
187
- router.store.loaders[id] ||
188
- (() => {
189
- router.setStore((s) => {
190
- s.loaders[id] = {
191
- pending: [],
192
- fetch: (async (loaderContext: LoaderContext<any, any>) => {
193
- if (!route) {
194
- return
195
- }
196
-
197
- const loaderState: LoaderState<any, any> = {
198
- loadedAt: Date.now(),
199
- loaderContext,
200
- }
201
-
202
- router.setStore((s) => {
203
- s.loaders[id]!.current = loaderState
204
- s.loaders[id]!.latest = loaderState
205
- s.loaders[id]!.pending.push(loaderState)
206
- })
207
-
208
- try {
209
- return await route.options.loader?.(loaderContext)
210
- } finally {
211
- router.setStore((s) => {
212
- s.loaders[id]!.pending = s.loaders[id]!.pending.filter(
213
- (d) => d !== loaderState,
214
- )
215
- })
216
- }
217
- }) as any,
218
- }
219
- })
220
-
221
- return router.store.loaders[id]!
222
- })()
223
-
224
- return loader as any
225
- },
226
-
227
- // buildLink: (options) => {
228
- // return router.buildLink({
229
- // ...options,
230
- // from: fullPath,
231
- // } as any) as any
232
- // },
233
-
234
- // navigate: (options) => {
235
- // return router.navigate({
236
- // ...options,
237
- // from: fullPath,
238
- // } as any) as any
239
- // },
240
-
241
- // matchRoute: (matchLocation, opts) => {
242
- // return router.matchRoute(
243
- // {
244
- // ...matchLocation,
245
- // from: fullPath,
246
- // } as any,
247
- // opts,
248
- // ) as any
249
- // },
25
+ options!: RouteOptions
26
+ originalIndex!: number
27
+ getRouter!: () => Router<
28
+ TAllRouteInfo['routeConfig'],
29
+ TAllRouteInfo,
30
+ TRouterContext
31
+ >
32
+ constructor(
33
+ routeConfig: RouteConfig,
34
+ options: TRouteInfo['options'],
35
+ originalIndex: number,
36
+ parent: undefined | Route<TAllRouteInfo, any>,
37
+ router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>,
38
+ ) {
39
+ Object.assign(this, {
40
+ ...routeConfig,
41
+ originalIndex,
42
+ options,
43
+ getRouter: () => router,
44
+ childRoutes: undefined!,
45
+ getParentRoute: () => parent,
46
+ })
47
+
48
+ router.options.createRoute?.({ router, route: this })
250
49
  }
251
-
252
- router.options.createRoute?.({ router, route })
253
-
254
- return route
255
50
  }
@@ -4,7 +4,7 @@ import { ParsePathParams } from './link'
4
4
  import { joinPaths, trimPath, trimPathRight } from './path'
5
5
  import { RouteInfo } from './routeInfo'
6
6
  import { RouteMatch } from './routeMatch'
7
- import { RegisteredRouter, Router } from './router'
7
+ import { AnyRouter, RegisteredRouter, Router } from './router'
8
8
  import { Expand, IsAny, NoInfer, PickUnsafe } from './utils'
9
9
 
10
10
  export const rootRouteId = '__root__' as const
@@ -62,10 +62,6 @@ export interface LoaderContext<
62
62
  // parentLoaderPromise?: Promise<TParentRouteLoaderData>
63
63
  }
64
64
 
65
- export type ActionFn<TActionPayload = unknown, TActionResponse = unknown> = (
66
- submission: TActionPayload,
67
- ) => TActionResponse | Promise<TActionResponse>
68
-
69
65
  export type UnloaderFn<TPath extends string> = (
70
66
  routeMatch: RouteMatch<any, RouteInfo<string, TPath>>,
71
67
  ) => void
@@ -77,8 +73,6 @@ export type RouteOptions<
77
73
  TRouteLoaderData extends AnyLoaderData = {},
78
74
  TParentLoaderData extends AnyLoaderData = {},
79
75
  TLoaderData extends AnyLoaderData = {},
80
- TActionPayload = unknown,
81
- TActionResponse = unknown,
82
76
  TParentSearchSchema extends {} = {},
83
77
  TSearchSchema extends AnySearchSchema = {},
84
78
  TFullSearchSchema extends AnySearchSchema = TSearchSchema,
@@ -120,15 +114,12 @@ export type RouteOptions<
120
114
  // The max age to cache the loader data for this route in milliseconds from the time of route inactivity
121
115
  // before it is garbage collected.
122
116
  loaderGcMaxAge?: number
123
- // An asynchronous function made available to the route for performing asynchronous or mutative actions that
124
- // might invalidate the route's data.
125
- action?: ActionFn<TActionPayload, TActionResponse>
126
117
  // This async function is called before a route is loaded.
127
118
  // If an error is thrown here, the route's loader will not be called.
128
119
  // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onLoadError` function.
129
120
  // If thrown during a preload event, the error will be logged to the console.
130
121
  beforeLoad?: (opts: {
131
- router: Router<any, any, unknown>
122
+ router: AnyRouter
132
123
  match: RouteMatch
133
124
  }) => Promise<void> | void
134
125
  // This function will be called if the route's loader throws an error **during an attempted navigation**.
@@ -181,8 +172,6 @@ export interface RouteConfig<
181
172
  TRouteLoaderData extends AnyLoaderData = AnyLoaderData,
182
173
  TParentLoaderData extends AnyLoaderData = {},
183
174
  TLoaderData extends AnyLoaderData = AnyLoaderData,
184
- TActionPayload = unknown,
185
- TActionResponse = unknown,
186
175
  TParentSearchSchema extends {} = {},
187
176
  TSearchSchema extends AnySearchSchema = {},
188
177
  TFullSearchSchema extends AnySearchSchema = {},
@@ -202,8 +191,6 @@ export interface RouteConfig<
202
191
  TRouteLoaderData,
203
192
  TParentLoaderData,
204
193
  TLoaderData,
205
- TActionPayload,
206
- TActionResponse,
207
194
  TParentSearchSchema,
208
195
  TSearchSchema,
209
196
  TFullSearchSchema,
@@ -228,8 +215,6 @@ export interface RouteConfig<
228
215
  TRouteLoaderData,
229
216
  TParentLoaderData,
230
217
  TLoaderData,
231
- TActionPayload,
232
- TActionResponse,
233
218
  TParentSearchSchema,
234
219
  TSearchSchema,
235
220
  TFullSearchSchema,
@@ -267,8 +252,6 @@ type GenerateFn<
267
252
  TParentParams extends AnyPathParams = {},
268
253
  > = <
269
254
  TRouteLoaderData extends AnyLoaderData = AnyLoaderData,
270
- TActionPayload = unknown,
271
- TActionResponse = unknown,
272
255
  TSearchSchema extends AnySearchSchema = {},
273
256
  TParams extends Record<ParsePathParams<TPath>, unknown> = Record<
274
257
  ParsePathParams<TPath>,
@@ -288,8 +271,6 @@ type GenerateFn<
288
271
  TRouteLoaderData,
289
272
  TParentLoaderData,
290
273
  Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>,
291
- TActionPayload,
292
- TActionResponse,
293
274
  TParentSearchSchema,
294
275
  TSearchSchema,
295
276
  Expand<TParentSearchSchema & TSearchSchema>,
@@ -313,8 +294,6 @@ type CreateRouteConfigFn<
313
294
  TRouteId extends string,
314
295
  TPath extends string,
315
296
  TRouteLoaderData extends AnyLoaderData,
316
- TActionPayload,
317
- TActionResponse,
318
297
  TSearchSchema extends AnySearchSchema = AnySearchSchema,
319
298
  TParams extends Record<ParsePathParams<TPath>, unknown> = Record<
320
299
  ParsePathParams<TPath>,
@@ -341,8 +320,6 @@ type CreateRouteConfigFn<
341
320
  TRouteLoaderData,
342
321
  TParentLoaderData,
343
322
  Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>,
344
- TActionPayload,
345
- TActionResponse,
346
323
  TParentSearchSchema,
347
324
  TSearchSchema,
348
325
  Expand<TParentSearchSchema & TSearchSchema>,
@@ -359,8 +336,6 @@ type CreateRouteConfigFn<
359
336
  TRouteLoaderData,
360
337
  TParentLoaderData,
361
338
  Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>,
362
- TActionPayload,
363
- TActionResponse,
364
339
  TParentSearchSchema,
365
340
  TSearchSchema,
366
341
  Expand<TParentSearchSchema & TSearchSchema>,
@@ -381,8 +356,6 @@ type CreateRouteConfigFn<
381
356
  TRouteLoaderData,
382
357
  TParentLoaderData,
383
358
  Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>,
384
- TActionPayload,
385
- TActionResponse,
386
359
  TParentSearchSchema,
387
360
  TSearchSchema,
388
361
  Expand<TParentSearchSchema & TSearchSchema>,
@@ -423,8 +396,6 @@ export interface AnyRouteConfig
423
396
  any,
424
397
  any,
425
398
  any,
426
- any,
427
- any,
428
399
  any
429
400
  > {}
430
401
 
@@ -444,8 +415,6 @@ export interface AnyRouteConfigWithChildren<TChildren>
444
415
  any,
445
416
  any,
446
417
  any,
447
- any,
448
- any,
449
418
  TChildren
450
419
  > {}
451
420
 
@@ -466,7 +435,7 @@ type TrimPathRight<T extends string> = T extends '/'
466
435
 
467
436
  export const createRouteConfig: CreateRouteConfigFn<true> = (
468
437
  options = {} as any,
469
- children,
438
+ children = [] as any,
470
439
  isRoot = true,
471
440
  parentId,
472
441
  parentPath,
package/src/routeInfo.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { ParsePathParams } from './link'
2
1
  import { Route } from './route'
3
2
  import {
4
3
  AnyLoaderData,
@@ -65,15 +64,13 @@ type ParseRouteChild<TRouteConfig, TId> = TRouteConfig & {
65
64
  // Generics!
66
65
  export type RouteConfigRoute<TRouteConfig> = TRouteConfig extends RouteConfig<
67
66
  infer TId,
68
- infer TRouteId,
67
+ infer TCustomId,
69
68
  infer TPath,
70
69
  infer TFullPath,
71
70
  infer TParentRouteLoaderData,
72
71
  infer TRouteLoaderData,
73
72
  infer TParentLoaderData,
74
73
  infer TLoaderData,
75
- infer TActionPayload,
76
- infer TActionResponse,
77
74
  infer TParentSearchSchema,
78
75
  infer TSearchSchema,
79
76
  infer TFullSearchSchema,
@@ -82,19 +79,17 @@ export type RouteConfigRoute<TRouteConfig> = TRouteConfig extends RouteConfig<
82
79
  infer TAllParams,
83
80
  any
84
81
  >
85
- ? string extends TRouteId
82
+ ? string extends TCustomId
86
83
  ? never
87
84
  : RouteInfo<
88
85
  TId,
89
- TRouteId,
86
+ TCustomId,
90
87
  TPath,
91
88
  TFullPath,
92
89
  TParentRouteLoaderData,
93
90
  TRouteLoaderData,
94
91
  TParentLoaderData,
95
92
  TLoaderData,
96
- TActionPayload,
97
- TActionResponse,
98
93
  TParentSearchSchema,
99
94
  TSearchSchema,
100
95
  TFullSearchSchema,
@@ -120,8 +115,6 @@ export interface RoutesInfoInner<
120
115
  any,
121
116
  any,
122
117
  any,
123
- any,
124
- any,
125
118
  any
126
119
  > = RouteInfo,
127
120
  TRouteInfoById = { '/': TRouteInfo } & {
@@ -160,22 +153,18 @@ export interface AnyRouteInfo
160
153
  any,
161
154
  any,
162
155
  any,
163
- any,
164
- any,
165
156
  any
166
157
  > {}
167
158
 
168
159
  export interface RouteInfo<
169
160
  TId extends string = string,
170
- TRouteId extends string = string,
161
+ TCustomId extends string = string,
171
162
  TPath extends string = string,
172
163
  TFullPath extends string = '/',
173
164
  TParentRouteLoaderData extends AnyLoaderData = {},
174
165
  TRouteLoaderData extends AnyLoaderData = {},
175
166
  TParentLoaderData extends AnyLoaderData = {},
176
167
  TLoaderData extends AnyLoaderData = {},
177
- TActionPayload = unknown,
178
- TActionResponse = unknown,
179
168
  TParentSearchSchema extends {} = {},
180
169
  TSearchSchema extends AnySearchSchema = {},
181
170
  TFullSearchSchema extends AnySearchSchema = {},
@@ -184,29 +173,25 @@ export interface RouteInfo<
184
173
  TAllParams extends AnyPathParams = {},
185
174
  > {
186
175
  id: TId
187
- routeId: TRouteId
176
+ customId: TCustomId
188
177
  path: TPath
189
178
  fullPath: TFullPath
190
179
  parentRouteLoaderData: TParentRouteLoaderData
191
180
  routeLoaderData: TRouteLoaderData
192
181
  parentLoaderData: TParentLoaderData
193
182
  loaderData: TLoaderData
194
- actionPayload: TActionPayload
195
- actionResponse: TActionResponse
196
183
  searchSchema: TSearchSchema
197
184
  fullSearchSchema: TFullSearchSchema
198
185
  parentParams: TParentParams
199
186
  params: TParams
200
187
  allParams: TAllParams
201
188
  options: RouteOptions<
202
- TRouteId,
189
+ TCustomId,
203
190
  TPath,
204
191
  TParentRouteLoaderData,
205
192
  TRouteLoaderData,
206
193
  TParentLoaderData,
207
194
  TLoaderData,
208
- TActionPayload,
209
- TActionResponse,
210
195
  TParentSearchSchema,
211
196
  TSearchSchema,
212
197
  TFullSearchSchema,