@tanstack/router-core 0.0.1-alpha.1 → 0.0.1-alpha.11

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 (43) hide show
  1. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +30 -0
  2. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +1 -0
  3. package/build/cjs/packages/router-core/src/index.js +35 -1421
  4. package/build/cjs/packages/router-core/src/index.js.map +1 -1
  5. package/build/cjs/packages/router-core/src/path.js +222 -0
  6. package/build/cjs/packages/router-core/src/path.js.map +1 -0
  7. package/build/cjs/packages/router-core/src/qss.js +1 -1
  8. package/build/cjs/packages/router-core/src/qss.js.map +1 -1
  9. package/build/cjs/packages/router-core/src/route.js +161 -0
  10. package/build/cjs/packages/router-core/src/route.js.map +1 -0
  11. package/build/cjs/packages/router-core/src/routeConfig.js +69 -0
  12. package/build/cjs/packages/router-core/src/routeConfig.js.map +1 -0
  13. package/build/cjs/packages/router-core/src/routeMatch.js +266 -0
  14. package/build/cjs/packages/router-core/src/routeMatch.js.map +1 -0
  15. package/build/cjs/packages/router-core/src/router.js +789 -0
  16. package/build/cjs/packages/router-core/src/router.js.map +1 -0
  17. package/build/cjs/packages/router-core/src/searchParams.js +70 -0
  18. package/build/cjs/packages/router-core/src/searchParams.js.map +1 -0
  19. package/build/cjs/packages/router-core/src/utils.js +118 -0
  20. package/build/cjs/packages/router-core/src/utils.js.map +1 -0
  21. package/build/esm/index.js +1385 -1232
  22. package/build/esm/index.js.map +1 -1
  23. package/build/stats-html.html +1 -1
  24. package/build/stats-react.json +388 -46
  25. package/build/types/index.d.ts +433 -338
  26. package/build/umd/index.development.js +1226 -1065
  27. package/build/umd/index.development.js.map +1 -1
  28. package/build/umd/index.production.js +1 -1
  29. package/build/umd/index.production.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/frameworks.ts +12 -0
  32. package/src/index.ts +15 -2969
  33. package/src/link.ts +319 -0
  34. package/src/path.ts +236 -0
  35. package/src/qss.ts +1 -1
  36. package/src/route.ts +243 -0
  37. package/src/routeConfig.ts +495 -0
  38. package/src/routeInfo.ts +228 -0
  39. package/src/routeMatch.ts +374 -0
  40. package/src/router.ts +1230 -0
  41. package/src/searchParams.ts +54 -0
  42. package/src/utils.ts +157 -0
  43. package/src/createRoutes.test.ts +0 -328
package/src/route.ts ADDED
@@ -0,0 +1,243 @@
1
+ import {
2
+ CheckRelativePath,
3
+ LinkInfo,
4
+ LinkOptions,
5
+ ResolveRelativePath,
6
+ ToOptions,
7
+ } from './link'
8
+ import { LoaderContext, RouteConfig, RouteOptions } from './routeConfig'
9
+ import {
10
+ AnyAllRouteInfo,
11
+ AnyRouteInfo,
12
+ DefaultAllRouteInfo,
13
+ RouteInfo,
14
+ RouteInfoByPath,
15
+ } from './routeInfo'
16
+ import { RouteMatch } from './routeMatch'
17
+ import {
18
+ Action,
19
+ ActionState,
20
+ Loader,
21
+ LoaderState,
22
+ MatchRouteOptions,
23
+ Router,
24
+ } from './router'
25
+ import { NoInfer, replaceEqualDeep } from './utils'
26
+
27
+ export interface AnyRoute extends Route<any, any> {}
28
+
29
+ export interface Route<
30
+ TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
31
+ TRouteInfo extends AnyRouteInfo = RouteInfo,
32
+ > {
33
+ routeId: TRouteInfo['id']
34
+ routeRouteId: TRouteInfo['routeId']
35
+ routePath: TRouteInfo['path']
36
+ fullPath: TRouteInfo['fullPath']
37
+ parentRoute?: AnyRoute
38
+ childRoutes?: AnyRoute[]
39
+ options: RouteOptions
40
+ router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo>
41
+ buildLink: <TTo extends string = '.'>(
42
+ options: Omit<
43
+ LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
44
+ 'from'
45
+ >,
46
+ ) => LinkInfo
47
+ matchRoute: <
48
+ TTo extends string = '.',
49
+ TResolved extends string = ResolveRelativePath<TRouteInfo['id'], TTo>,
50
+ >(
51
+ matchLocation: CheckRelativePath<
52
+ TAllRouteInfo,
53
+ TRouteInfo['fullPath'],
54
+ NoInfer<TTo>
55
+ > &
56
+ Omit<ToOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>, 'from'>,
57
+ opts?: MatchRouteOptions,
58
+ ) => RouteInfoByPath<TAllRouteInfo, TResolved>['allParams']
59
+ navigate: <TTo extends string = '.'>(
60
+ options: Omit<LinkOptions<TAllRouteInfo, TRouteInfo['id'], TTo>, 'from'>,
61
+ ) => Promise<void>
62
+ action: unknown extends TRouteInfo['actionResponse']
63
+ ?
64
+ | Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
65
+ | undefined
66
+ : Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
67
+ loader: unknown extends TRouteInfo['routeLoaderData']
68
+ ?
69
+ | Action<
70
+ LoaderContext<
71
+ TRouteInfo['fullSearchSchema'],
72
+ TRouteInfo['allParams']
73
+ >,
74
+ TRouteInfo['routeLoaderData']
75
+ >
76
+ | undefined
77
+ : Loader<
78
+ TRouteInfo['fullSearchSchema'],
79
+ TRouteInfo['allParams'],
80
+ TRouteInfo['routeLoaderData']
81
+ >
82
+ }
83
+
84
+ export function createRoute<
85
+ TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
86
+ TRouteInfo extends AnyRouteInfo = RouteInfo,
87
+ >(
88
+ routeConfig: RouteConfig,
89
+ options: TRouteInfo['options'],
90
+ parent: undefined | Route<TAllRouteInfo, any>,
91
+ router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo>,
92
+ ): Route<TAllRouteInfo, TRouteInfo> {
93
+ const { id, routeId, path: routePath, fullPath } = routeConfig
94
+
95
+ const action =
96
+ router.state.actions[id] ||
97
+ (() => {
98
+ router.state.actions[id] = {
99
+ pending: [],
100
+ submit: async <T, U>(
101
+ submission: T,
102
+ actionOpts?: { invalidate?: boolean },
103
+ ) => {
104
+ if (!route) {
105
+ return
106
+ }
107
+
108
+ const invalidate = actionOpts?.invalidate ?? true
109
+
110
+ const actionState: ActionState<T, U> = {
111
+ submittedAt: Date.now(),
112
+ status: 'pending',
113
+ submission,
114
+ }
115
+
116
+ action.current = actionState
117
+ action.latest = actionState
118
+ action.pending.push(actionState)
119
+
120
+ router.state = {
121
+ ...router.state,
122
+ currentAction: actionState,
123
+ latestAction: actionState,
124
+ }
125
+
126
+ router.notify()
127
+
128
+ try {
129
+ const res = await route.options.action?.(submission)
130
+ actionState.data = res as U
131
+ if (invalidate) {
132
+ router.invalidateRoute({ to: '.', fromCurrent: true })
133
+ await router.reload()
134
+ }
135
+ actionState.status = 'success'
136
+ return res
137
+ } catch (err) {
138
+ console.error(err)
139
+ actionState.error = err
140
+ actionState.status = 'error'
141
+ } finally {
142
+ action.pending = action.pending.filter((d) => d !== actionState)
143
+ router.removeActionQueue.push({ action, actionState })
144
+ router.notify()
145
+ }
146
+ },
147
+ }
148
+ return router.state.actions[id]!
149
+ })()
150
+
151
+ const loader =
152
+ router.state.loaders[id] ||
153
+ (() => {
154
+ router.state.loaders[id] = {
155
+ pending: [],
156
+ fetch: (async (loaderContext: LoaderContext<any, any>) => {
157
+ if (!route) {
158
+ return
159
+ }
160
+
161
+ const loaderState: LoaderState<any, any> = {
162
+ loadedAt: Date.now(),
163
+ loaderContext,
164
+ }
165
+
166
+ loader.current = loaderState
167
+ loader.latest = loaderState
168
+ loader.pending.push(loaderState)
169
+
170
+ // router.state = {
171
+ // ...router.state,
172
+ // currentAction: loaderState,
173
+ // latestAction: loaderState,
174
+ // }
175
+
176
+ router.notify()
177
+
178
+ try {
179
+ return await route.options.loader?.(loaderContext)
180
+ } finally {
181
+ loader.pending = loader.pending.filter((d) => d !== loaderState)
182
+ // router.removeActionQueue.push({ loader, loaderState })
183
+ router.notify()
184
+ }
185
+ }) as any,
186
+ }
187
+ return router.state.loaders[id]!
188
+ })()
189
+
190
+ let route: Route<TAllRouteInfo, TRouteInfo> = {
191
+ routeId: id,
192
+ routeRouteId: routeId,
193
+ routePath,
194
+ fullPath,
195
+ options,
196
+ router,
197
+ childRoutes: undefined!,
198
+ parentRoute: parent,
199
+ action,
200
+ loader: loader as any,
201
+
202
+ buildLink: (options) => {
203
+ return router.buildLink({
204
+ ...options,
205
+ from: fullPath,
206
+ } as any) as any
207
+ },
208
+
209
+ navigate: (options) => {
210
+ return router.navigate({
211
+ ...options,
212
+ from: fullPath,
213
+ } as any) as any
214
+ },
215
+
216
+ matchRoute: (matchLocation, opts) => {
217
+ return router.matchRoute(
218
+ {
219
+ ...matchLocation,
220
+ from: fullPath,
221
+ } as any,
222
+ opts,
223
+ )
224
+ },
225
+ }
226
+
227
+ router.options.createRoute?.({ router, route })
228
+
229
+ return route
230
+ }
231
+
232
+ export function cascadeLoaderData(matches: RouteMatch<any, any>[]) {
233
+ matches.forEach((match, index) => {
234
+ const parent = matches[index - 1]
235
+
236
+ if (parent) {
237
+ match.loaderData = replaceEqualDeep(match.loaderData, {
238
+ ...parent.loaderData,
239
+ ...match.routeLoaderData,
240
+ })
241
+ }
242
+ })
243
+ }