@tanstack/router-core 0.0.1-alpha.0
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/build/cjs/_virtual/_rollupPluginBabelHelpers.js +33 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/cjs/packages/location-core/src/index.js +1313 -0
- package/build/cjs/packages/location-core/src/index.js.map +1 -0
- package/build/cjs/packages/location-core/src/qss.js +70 -0
- package/build/cjs/packages/location-core/src/qss.js.map +1 -0
- package/build/cjs/packages/router-core/src/createRoutes.js +106 -0
- package/build/cjs/packages/router-core/src/createRoutes.js.map +1 -0
- package/build/cjs/packages/router-core/src/createRoutes.test.js +160 -0
- package/build/cjs/packages/router-core/src/createRoutes.test.js.map +1 -0
- package/build/cjs/packages/router-core/src/index.js +1444 -0
- package/build/cjs/packages/router-core/src/index.js.map +1 -0
- package/build/cjs/packages/router-core/src/qss.js +70 -0
- package/build/cjs/packages/router-core/src/qss.js.map +1 -0
- package/build/esm/index.js +2297 -0
- package/build/esm/index.js.map +1 -0
- package/build/stats-html.html +4034 -0
- package/build/stats-react.json +157 -0
- package/build/types/createRoutes.d.ts +10 -0
- package/build/types/createRoutes.test.d.ts +1 -0
- package/build/types/index.d.ts +517 -0
- package/build/types/qss.d.ts +2 -0
- package/build/types/react-router/src/createRoutes.test.d.ts +0 -0
- package/build/types/react-router/src/index.d.ts +59 -0
- package/build/types/router-core/src/createRoutes.test.d.ts +1 -0
- package/build/types/router-core/src/index.d.ts +504 -0
- package/build/types/router-core/src/qss.d.ts +2 -0
- package/build/umd/index.development.js +2322 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +12 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +48 -0
- package/src/createRoutes.test.ts +318 -0
- package/src/index.ts +2974 -0
- package/src/package.json +48 -0
- package/src/qss.ts +53 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { Route } from '@tanstack/router-core'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import {
|
|
4
|
+
createRouter,
|
|
5
|
+
AllRouteInfo,
|
|
6
|
+
createRouteConfig,
|
|
7
|
+
RelativeToPathAutoComplete,
|
|
8
|
+
} from '.'
|
|
9
|
+
|
|
10
|
+
// Build our routes. We could do this in our component, too.
|
|
11
|
+
const routeConfig = createRouteConfig().addChildren((createRoute) => [
|
|
12
|
+
createRoute({
|
|
13
|
+
path: '/',
|
|
14
|
+
validateSearch: (search) =>
|
|
15
|
+
z
|
|
16
|
+
.object({
|
|
17
|
+
version: z.number(),
|
|
18
|
+
})
|
|
19
|
+
.parse(search),
|
|
20
|
+
}),
|
|
21
|
+
createRoute({
|
|
22
|
+
path: '/test',
|
|
23
|
+
validateSearch: (search) =>
|
|
24
|
+
z
|
|
25
|
+
.object({
|
|
26
|
+
version: z.number(),
|
|
27
|
+
isGood: z.boolean(),
|
|
28
|
+
})
|
|
29
|
+
.parse(search),
|
|
30
|
+
}),
|
|
31
|
+
createRoute({
|
|
32
|
+
path: 'dashboard',
|
|
33
|
+
loader: async () => {
|
|
34
|
+
console.log('Fetching all invoices...')
|
|
35
|
+
return {
|
|
36
|
+
invoices: 'await fetchInvoices()',
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
}).addChildren((createRoute) => [
|
|
40
|
+
createRoute({ path: '/' }),
|
|
41
|
+
createRoute({
|
|
42
|
+
path: 'invoices',
|
|
43
|
+
}).addChildren((createRoute) => [
|
|
44
|
+
createRoute({
|
|
45
|
+
path: '/',
|
|
46
|
+
action: async (partialInvoice: { amount: number }) => {
|
|
47
|
+
const invoice: { id: number; amount: number } = null!
|
|
48
|
+
// // Redirect to the new invoice
|
|
49
|
+
// ctx.router.navigate({
|
|
50
|
+
// to: invoice.id,
|
|
51
|
+
// // Use the current match for relative paths
|
|
52
|
+
// from: ctx.match.pathname,
|
|
53
|
+
// })
|
|
54
|
+
return invoice
|
|
55
|
+
},
|
|
56
|
+
}),
|
|
57
|
+
createRoute({
|
|
58
|
+
path: ':invoiceId',
|
|
59
|
+
parseParams: ({ invoiceId }) => ({ invoiceId: Number(invoiceId) }),
|
|
60
|
+
stringifyParams: ({ invoiceId }) => ({ invoiceId: String(invoiceId) }),
|
|
61
|
+
loader: async ({ params: { invoiceId } }) => {
|
|
62
|
+
console.log('Fetching invoice...')
|
|
63
|
+
return {
|
|
64
|
+
invoice: 'await fetchInvoiceById(invoiceId!)',
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
]),
|
|
69
|
+
createRoute({
|
|
70
|
+
path: 'users',
|
|
71
|
+
loader: async () => {
|
|
72
|
+
return {
|
|
73
|
+
users: 'await fetchUsers()',
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
validateSearch: (search) =>
|
|
77
|
+
z
|
|
78
|
+
.object({
|
|
79
|
+
usersView: z
|
|
80
|
+
.object({
|
|
81
|
+
sortBy: z.enum(['name', 'id', 'email']).optional(),
|
|
82
|
+
filterBy: z.string().optional(),
|
|
83
|
+
})
|
|
84
|
+
.optional(),
|
|
85
|
+
})
|
|
86
|
+
.parse(search),
|
|
87
|
+
preSearchFilters: [
|
|
88
|
+
// Keep the usersView search param around
|
|
89
|
+
// while in this route (or it's children!)
|
|
90
|
+
(search) => ({
|
|
91
|
+
...search,
|
|
92
|
+
usersView: {
|
|
93
|
+
...search.usersView,
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
],
|
|
97
|
+
}).addChildren((createRoute) => [
|
|
98
|
+
createRoute({
|
|
99
|
+
path: ':userId',
|
|
100
|
+
loader: async ({ params: { userId }, search }) => {
|
|
101
|
+
return {
|
|
102
|
+
user: 'await fetchUserById(userId!)',
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
action: async (partialUser: { amount: number }) => {
|
|
106
|
+
const invoice: { id: number; amount: number } = null!
|
|
107
|
+
// // Redirect to the new invoice
|
|
108
|
+
// ctx.router.navigate({
|
|
109
|
+
// to: invoice.id,
|
|
110
|
+
// // Use the current match for relative paths
|
|
111
|
+
// from: ctx.match.pathname,
|
|
112
|
+
// })
|
|
113
|
+
return invoice
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
]),
|
|
117
|
+
]),
|
|
118
|
+
// Obviously, you can put routes in other files, too
|
|
119
|
+
// reallyExpensiveRoute,
|
|
120
|
+
createRoute({
|
|
121
|
+
path: 'authenticated/', // Trailing slash doesn't mean anything
|
|
122
|
+
}).addChildren((createRoute) => [
|
|
123
|
+
createRoute({
|
|
124
|
+
path: '/',
|
|
125
|
+
}),
|
|
126
|
+
]),
|
|
127
|
+
])
|
|
128
|
+
|
|
129
|
+
type MyRoutesInfo = AllRouteInfo<typeof routeConfig>
|
|
130
|
+
// ^?
|
|
131
|
+
type RouteInfo = MyRoutesInfo['routeInfo']
|
|
132
|
+
type RoutesById = MyRoutesInfo['routeInfoById']
|
|
133
|
+
type RoutesTest = Route<MyRoutesInfo, MyRoutesInfo['routeInfoByFullPath']['/']>
|
|
134
|
+
// ^?
|
|
135
|
+
type RoutePaths = MyRoutesInfo['routeInfoByFullPath']
|
|
136
|
+
// ^?
|
|
137
|
+
type InvoiceRouteInfo = RoutesById['/dashboard/invoices/']
|
|
138
|
+
// ^?
|
|
139
|
+
type InvoiceLoaderData = InvoiceRouteInfo['loaderData']
|
|
140
|
+
// ^?//
|
|
141
|
+
type InvoiceAction = InvoiceRouteInfo['actionPayload']
|
|
142
|
+
// ^?
|
|
143
|
+
|
|
144
|
+
const router = createRouter({
|
|
145
|
+
routeConfig,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const loaderData = router.getRoute('/dashboard/users/:userId')
|
|
149
|
+
// ^?
|
|
150
|
+
const route = router.getRoute('/dashboard/users/:userId')
|
|
151
|
+
// ^?
|
|
152
|
+
const action = route.action
|
|
153
|
+
// ^?
|
|
154
|
+
const result = action.submit({ amount: 10000 })
|
|
155
|
+
// ^?
|
|
156
|
+
|
|
157
|
+
router.buildLink({
|
|
158
|
+
to: '/dashboard/users/:userId',
|
|
159
|
+
params: {
|
|
160
|
+
userId: '2',
|
|
161
|
+
},
|
|
162
|
+
search: (prev) => ({
|
|
163
|
+
usersView: {
|
|
164
|
+
sortBy: 'email',
|
|
165
|
+
},
|
|
166
|
+
}),
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
// @ts-expect-error
|
|
170
|
+
router.buildLink({
|
|
171
|
+
from: '/',
|
|
172
|
+
to: '/test',
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
router.buildLink({
|
|
176
|
+
from: '/',
|
|
177
|
+
to: '/test',
|
|
178
|
+
search: () => {
|
|
179
|
+
return {
|
|
180
|
+
version: 2,
|
|
181
|
+
isGood: true,
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
router.buildLink({
|
|
187
|
+
from: '/test',
|
|
188
|
+
to: '/',
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
route.buildLink({
|
|
192
|
+
to: '',
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
router.getRoute('/dashboard').buildLink({
|
|
196
|
+
to: '/dashboard/invoices',
|
|
197
|
+
params: {
|
|
198
|
+
// @ts-expect-error
|
|
199
|
+
invoiceId: 2,
|
|
200
|
+
},
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
router.getRoute('/dashboard').buildLink({
|
|
204
|
+
to: '/dashboard/invoices/:invoiceId',
|
|
205
|
+
params: {
|
|
206
|
+
// @ts-expect-error
|
|
207
|
+
invoiceId: '2',
|
|
208
|
+
},
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
router.getRoute('/').buildLink({
|
|
212
|
+
to: '/dashboard/invoices/:invoiceId',
|
|
213
|
+
params: {
|
|
214
|
+
invoiceId: 2,
|
|
215
|
+
},
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
router.getRoute('/').buildLink({
|
|
219
|
+
to: '/',
|
|
220
|
+
search: {
|
|
221
|
+
version: 2,
|
|
222
|
+
},
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
router.getRoute('/').buildLink({
|
|
226
|
+
to: '/dashboard/users/:userId',
|
|
227
|
+
params: (current) => ({
|
|
228
|
+
userId:
|
|
229
|
+
// @ts-expect-error
|
|
230
|
+
current?.invoiceId,
|
|
231
|
+
}),
|
|
232
|
+
search: (old) => ({
|
|
233
|
+
usersView: {
|
|
234
|
+
sortBy: 'email' as const,
|
|
235
|
+
filterBy: String(old.version),
|
|
236
|
+
},
|
|
237
|
+
}),
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
router.getRoute('/dashboard/invoices/:invoiceId').buildLink({
|
|
241
|
+
to: '/dashboard/users/:userId',
|
|
242
|
+
params: (current) => ({
|
|
243
|
+
userId: `${current?.invoiceId}`,
|
|
244
|
+
}),
|
|
245
|
+
search: (prev) => {
|
|
246
|
+
return {
|
|
247
|
+
usersView: {
|
|
248
|
+
sortBy: 'name' as const,
|
|
249
|
+
filterBy: 'tanner',
|
|
250
|
+
},
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
router.getRoute('/dashboard/users/:userId').buildLink({
|
|
256
|
+
to: '/',
|
|
257
|
+
search: (prev) => {
|
|
258
|
+
return {
|
|
259
|
+
version: 2,
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
router.buildLink({
|
|
265
|
+
from: '/',
|
|
266
|
+
to: '/dashboard/users/:userId',
|
|
267
|
+
params: {
|
|
268
|
+
userId: '2',
|
|
269
|
+
},
|
|
270
|
+
search: (prev) => ({
|
|
271
|
+
usersView: {
|
|
272
|
+
sortBy: 'id',
|
|
273
|
+
filterBy: `${prev.version}`,
|
|
274
|
+
},
|
|
275
|
+
}),
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
router.getRoute('/').navigate({
|
|
279
|
+
// to: '.',
|
|
280
|
+
// TODO: What the heck? Why is any required here?
|
|
281
|
+
search: (prev: any) => ({
|
|
282
|
+
version: prev.version,
|
|
283
|
+
}),
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
router.buildLink({
|
|
287
|
+
from: '/dashboard/invoices',
|
|
288
|
+
to: '/dashboard',
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
router.getRoute('/').buildLink({
|
|
292
|
+
to: '/dashboard/invoices/:invoiceId',
|
|
293
|
+
params: {
|
|
294
|
+
invoiceId: 2,
|
|
295
|
+
},
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
router.getRoute('/dashboard/invoices/:invoiceId').buildLink({
|
|
299
|
+
to: '.',
|
|
300
|
+
params: (d) => ({
|
|
301
|
+
invoiceId: d.invoiceId,
|
|
302
|
+
}),
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
type test = RelativeToPathAutoComplete<
|
|
306
|
+
// ^?
|
|
307
|
+
MyRoutesInfo['fullPath'],
|
|
308
|
+
'/dashboard/invoices',
|
|
309
|
+
'..'
|
|
310
|
+
>
|
|
311
|
+
|
|
312
|
+
router.getRoute('/dashboard/invoices/:invoiceId').buildLink({
|
|
313
|
+
to: '../test',
|
|
314
|
+
search: {
|
|
315
|
+
version: 2,
|
|
316
|
+
isGood: true,
|
|
317
|
+
},
|
|
318
|
+
})
|