@tanstack/router-core 1.114.23 → 1.114.25

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
@@ -1,4 +1,8 @@
1
+ import { joinPaths, trimPathLeft } from './path'
2
+ import { notFound } from './not-found'
3
+ import { rootRouteId } from './root'
1
4
  import type { LazyRoute } from './fileRoute'
5
+ import type { NotFoundError } from './not-found'
2
6
  import type { NavigateOptions, ParsePathParams } from './link'
3
7
  import type { ParsedLocation } from './location'
4
8
  import type {
@@ -1250,4 +1254,368 @@ export type NotFoundRouteProps = {
1250
1254
  data: unknown
1251
1255
  }
1252
1256
 
1257
+ export class BaseRoute<
1258
+ in out TParentRoute extends AnyRoute = AnyRoute,
1259
+ in out TPath extends string = '/',
1260
+ in out TFullPath extends string = ResolveFullPath<TParentRoute, TPath>,
1261
+ in out TCustomId extends string = string,
1262
+ in out TId extends string = ResolveId<TParentRoute, TCustomId, TPath>,
1263
+ in out TSearchValidator = undefined,
1264
+ in out TParams = ResolveParams<TPath>,
1265
+ in out TRouterContext = AnyContext,
1266
+ in out TRouteContextFn = AnyContext,
1267
+ in out TBeforeLoadFn = AnyContext,
1268
+ in out TLoaderDeps extends Record<string, any> = {},
1269
+ in out TLoaderFn = undefined,
1270
+ in out TChildren = unknown,
1271
+ in out TFileRouteTypes = unknown,
1272
+ > implements
1273
+ Route<
1274
+ TParentRoute,
1275
+ TPath,
1276
+ TFullPath,
1277
+ TCustomId,
1278
+ TId,
1279
+ TSearchValidator,
1280
+ TParams,
1281
+ TRouterContext,
1282
+ TRouteContextFn,
1283
+ TBeforeLoadFn,
1284
+ TLoaderDeps,
1285
+ TLoaderFn,
1286
+ TChildren,
1287
+ TFileRouteTypes
1288
+ >
1289
+ {
1290
+ isRoot: TParentRoute extends AnyRoute ? true : false
1291
+ options: RouteOptions<
1292
+ TParentRoute,
1293
+ TId,
1294
+ TCustomId,
1295
+ TFullPath,
1296
+ TPath,
1297
+ TSearchValidator,
1298
+ TParams,
1299
+ TLoaderDeps,
1300
+ TLoaderFn,
1301
+ TRouterContext,
1302
+ TRouteContextFn,
1303
+ TBeforeLoadFn
1304
+ >
1305
+
1306
+ // The following properties are set up in this.init()
1307
+ parentRoute!: TParentRoute
1308
+ private _id!: TId
1309
+ private _path!: TPath
1310
+ private _fullPath!: TFullPath
1311
+ private _to!: TrimPathRight<TFullPath>
1312
+ private _ssr!: boolean
1313
+
1314
+ public get to() {
1315
+ return this._to
1316
+ }
1317
+
1318
+ public get id() {
1319
+ return this._id
1320
+ }
1321
+
1322
+ public get path() {
1323
+ return this._path
1324
+ }
1325
+
1326
+ public get fullPath() {
1327
+ return this._fullPath
1328
+ }
1329
+
1330
+ public get ssr() {
1331
+ return this._ssr
1332
+ }
1333
+
1334
+ // Optional
1335
+ children?: TChildren
1336
+ originalIndex?: number
1337
+ rank!: number
1338
+ lazyFn?: () => Promise<LazyRoute>
1339
+ _lazyPromise?: Promise<void>
1340
+ _componentsPromise?: Promise<Array<void>>
1341
+
1342
+ constructor(
1343
+ options?: RouteOptions<
1344
+ TParentRoute,
1345
+ TId,
1346
+ TCustomId,
1347
+ TFullPath,
1348
+ TPath,
1349
+ TSearchValidator,
1350
+ TParams,
1351
+ TLoaderDeps,
1352
+ TLoaderFn,
1353
+ TRouterContext,
1354
+ TRouteContextFn,
1355
+ TBeforeLoadFn
1356
+ >,
1357
+ ) {
1358
+ this.options = (options as any) || {}
1359
+ this.isRoot = !options?.getParentRoute as any
1360
+
1361
+ if ((options as any)?.id && (options as any)?.path) {
1362
+ throw new Error(`Route cannot have both an 'id' and a 'path' option.`)
1363
+ }
1364
+ }
1365
+
1366
+ types!: RouteTypes<
1367
+ TParentRoute,
1368
+ TPath,
1369
+ TFullPath,
1370
+ TCustomId,
1371
+ TId,
1372
+ TSearchValidator,
1373
+ TParams,
1374
+ TRouterContext,
1375
+ TRouteContextFn,
1376
+ TBeforeLoadFn,
1377
+ TLoaderDeps,
1378
+ TLoaderFn,
1379
+ TChildren,
1380
+ TFileRouteTypes
1381
+ >
1382
+
1383
+ init = (opts: { originalIndex: number; defaultSsr?: boolean }): void => {
1384
+ this.originalIndex = opts.originalIndex
1385
+
1386
+ const options = this.options as
1387
+ | (RouteOptions<
1388
+ TParentRoute,
1389
+ TId,
1390
+ TCustomId,
1391
+ TFullPath,
1392
+ TPath,
1393
+ TSearchValidator,
1394
+ TParams,
1395
+ TLoaderDeps,
1396
+ TLoaderFn,
1397
+ TRouterContext,
1398
+ TRouteContextFn,
1399
+ TBeforeLoadFn
1400
+ > &
1401
+ RoutePathOptionsIntersection<TCustomId, TPath>)
1402
+ | undefined
1403
+
1404
+ const isRoot = !options?.path && !options?.id
1405
+
1406
+ this.parentRoute = this.options.getParentRoute?.()
1407
+
1408
+ if (isRoot) {
1409
+ this._path = rootRouteId as TPath
1410
+ } else if (!this.parentRoute) {
1411
+ throw new Error(
1412
+ `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`,
1413
+ )
1414
+ }
1415
+
1416
+ let path: undefined | string = isRoot ? rootRouteId : options?.path
1417
+
1418
+ // If the path is anything other than an index path, trim it up
1419
+ if (path && path !== '/') {
1420
+ path = trimPathLeft(path)
1421
+ }
1422
+
1423
+ const customId = options?.id || path
1424
+
1425
+ // Strip the parentId prefix from the first level of children
1426
+ let id = isRoot
1427
+ ? rootRouteId
1428
+ : joinPaths([
1429
+ this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id,
1430
+ customId,
1431
+ ])
1432
+
1433
+ if (path === rootRouteId) {
1434
+ path = '/'
1435
+ }
1436
+
1437
+ if (id !== rootRouteId) {
1438
+ id = joinPaths(['/', id])
1439
+ }
1440
+
1441
+ const fullPath =
1442
+ id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path])
1443
+
1444
+ this._path = path as TPath
1445
+ this._id = id as TId
1446
+ this._fullPath = fullPath as TFullPath
1447
+ this._to = fullPath as TrimPathRight<TFullPath>
1448
+ this._ssr = options?.ssr ?? opts.defaultSsr ?? true
1449
+ }
1450
+
1451
+ addChildren: RouteAddChildrenFn<
1452
+ TParentRoute,
1453
+ TPath,
1454
+ TFullPath,
1455
+ TCustomId,
1456
+ TId,
1457
+ TSearchValidator,
1458
+ TParams,
1459
+ TRouterContext,
1460
+ TRouteContextFn,
1461
+ TBeforeLoadFn,
1462
+ TLoaderDeps,
1463
+ TLoaderFn,
1464
+ TFileRouteTypes
1465
+ > = (children) => {
1466
+ return this._addFileChildren(children) as any
1467
+ }
1468
+
1469
+ _addFileChildren: RouteAddFileChildrenFn<
1470
+ TParentRoute,
1471
+ TPath,
1472
+ TFullPath,
1473
+ TCustomId,
1474
+ TId,
1475
+ TSearchValidator,
1476
+ TParams,
1477
+ TRouterContext,
1478
+ TRouteContextFn,
1479
+ TBeforeLoadFn,
1480
+ TLoaderDeps,
1481
+ TLoaderFn,
1482
+ TFileRouteTypes
1483
+ > = (children) => {
1484
+ if (Array.isArray(children)) {
1485
+ this.children = children as TChildren
1486
+ }
1487
+
1488
+ if (typeof children === 'object' && children !== null) {
1489
+ this.children = Object.values(children) as TChildren
1490
+ }
1491
+
1492
+ return this as any
1493
+ }
1494
+
1495
+ _addFileTypes: RouteAddFileTypesFn<
1496
+ TParentRoute,
1497
+ TPath,
1498
+ TFullPath,
1499
+ TCustomId,
1500
+ TId,
1501
+ TSearchValidator,
1502
+ TParams,
1503
+ TRouterContext,
1504
+ TRouteContextFn,
1505
+ TBeforeLoadFn,
1506
+ TLoaderDeps,
1507
+ TLoaderFn,
1508
+ TChildren
1509
+ > = () => {
1510
+ return this as any
1511
+ }
1512
+
1513
+ updateLoader = <TNewLoaderFn>(options: {
1514
+ loader: Constrain<
1515
+ TNewLoaderFn,
1516
+ RouteLoaderFn<
1517
+ TParentRoute,
1518
+ TCustomId,
1519
+ TParams,
1520
+ TLoaderDeps,
1521
+ TRouterContext,
1522
+ TRouteContextFn,
1523
+ TBeforeLoadFn
1524
+ >
1525
+ >
1526
+ }) => {
1527
+ Object.assign(this.options, options)
1528
+ return this as unknown as BaseRoute<
1529
+ TParentRoute,
1530
+ TPath,
1531
+ TFullPath,
1532
+ TCustomId,
1533
+ TId,
1534
+ TSearchValidator,
1535
+ TParams,
1536
+ TRouterContext,
1537
+ TRouteContextFn,
1538
+ TBeforeLoadFn,
1539
+ TLoaderDeps,
1540
+ TNewLoaderFn,
1541
+ TChildren,
1542
+ TFileRouteTypes
1543
+ >
1544
+ }
1545
+
1546
+ update = (
1547
+ options: UpdatableRouteOptions<
1548
+ TParentRoute,
1549
+ TCustomId,
1550
+ TFullPath,
1551
+ TParams,
1552
+ TSearchValidator,
1553
+ TLoaderFn,
1554
+ TLoaderDeps,
1555
+ TRouterContext,
1556
+ TRouteContextFn,
1557
+ TBeforeLoadFn
1558
+ >,
1559
+ ): this => {
1560
+ Object.assign(this.options, options)
1561
+ return this
1562
+ }
1563
+
1564
+ lazy: RouteLazyFn<this> = (lazyFn) => {
1565
+ this.lazyFn = lazyFn
1566
+ return this
1567
+ }
1568
+ }
1569
+
1570
+ export class BaseRouteApi<TId, TRouter extends AnyRouter = RegisteredRouter> {
1571
+ id: TId
1572
+
1573
+ constructor({ id }: { id: TId }) {
1574
+ this.id = id as any
1575
+ }
1576
+
1577
+ notFound = (opts?: NotFoundError) => {
1578
+ return notFound({ routeId: this.id as string, ...opts })
1579
+ }
1580
+ }
1581
+
1582
+ export class BaseRootRoute<
1583
+ in out TSearchValidator = undefined,
1584
+ in out TRouterContext = {},
1585
+ in out TRouteContextFn = AnyContext,
1586
+ in out TBeforeLoadFn = AnyContext,
1587
+ in out TLoaderDeps extends Record<string, any> = {},
1588
+ in out TLoaderFn = undefined,
1589
+ in out TChildren = unknown,
1590
+ in out TFileRouteTypes = unknown,
1591
+ > extends BaseRoute<
1592
+ any, // TParentRoute
1593
+ '/', // TPath
1594
+ '/', // TFullPath
1595
+ string, // TCustomId
1596
+ RootRouteId, // TId
1597
+ TSearchValidator, // TSearchValidator
1598
+ {}, // TParams
1599
+ TRouterContext,
1600
+ TRouteContextFn,
1601
+ TBeforeLoadFn,
1602
+ TLoaderDeps,
1603
+ TLoaderFn,
1604
+ TChildren, // TChildren
1605
+ TFileRouteTypes
1606
+ > {
1607
+ constructor(
1608
+ options?: RootRouteOptions<
1609
+ TSearchValidator,
1610
+ TRouterContext,
1611
+ TRouteContextFn,
1612
+ TBeforeLoadFn,
1613
+ TLoaderDeps,
1614
+ TLoaderFn
1615
+ >,
1616
+ ) {
1617
+ super(options as any)
1618
+ }
1619
+ }
1620
+
1253
1621
  //