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

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