Skip to content

Commit 88b6a8c

Browse files
Add jsdoc annotations to public api (#5574)
Refactor: Add JSDoc comments and improve API documentation Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent fbb8b37 commit 88b6a8c

File tree

13 files changed

+106
-29
lines changed

13 files changed

+106
-29
lines changed

packages/react-router/src/Scripts.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { useRouterState } from './useRouterState'
33
import { useRouter } from './useRouter'
44
import type { RouterManagedTag } from '@tanstack/router-core'
55

6+
/**
7+
* Render body script tags collected from route matches and SSR manifests.
8+
* Should be placed near the end of the document body.
9+
*/
610
export const Scripts = () => {
711
const router = useRouter()
812
const nonce = router.options.ssr?.nonce

packages/react-router/src/ScrollRestoration.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function useScrollRestoration() {
1717
}
1818

1919
/**
20-
* @deprecated use createRouter's `scrollRestoration` option instead
20+
* @deprecated Use the `scrollRestoration` router option instead.
2121
*/
2222
export function ScrollRestoration(_props: ScrollRestorationOptions) {
2323
useScrollRestoration()

packages/react-router/src/awaited.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type AwaitOptions<T> = {
77
promise: Promise<T>
88
}
99

10+
/** Suspend until a deferred promise resolves/rejects and return its data. */
1011
export function useAwaited<T>({
1112
promise: _promise,
1213
}: AwaitOptions<T>): [T, DeferredPromise<T>] {
@@ -23,6 +24,10 @@ export function useAwaited<T>({
2324
return [promise[TSR_DEFERRED_PROMISE].data, promise]
2425
}
2526

27+
/**
28+
* Component that suspends on a deferred promise and renders its child with
29+
* the resolved value. Optionally provides a Suspense fallback.
30+
*/
2631
export function Await<T>(
2732
props: AwaitOptions<T> & {
2833
fallback?: React.ReactNode

packages/react-router/src/route.tsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,6 @@ declare module '@tanstack/router-core' {
7878
}
7979
}
8080

81-
/**
82-
* Returns a route-specific API that exposes type-safe hooks pre-bound
83-
* to a single route ID. Useful for consuming a route's APIs from files
84-
* where the route object isn't directly imported (e.g. code-split files).
85-
*
86-
* @param id Route ID string literal for the target route.
87-
* @returns A `RouteApi` instance bound to the given route ID.
88-
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/getRouteApiFunction
89-
*/
90-
/**
91-
* Returns a route-specific API that exposes type-safe hooks pre-bound
92-
* to a single route ID. Useful for consuming a route's APIs from files
93-
* where the route object isn't directly imported (e.g. code-split files).
94-
*
95-
* @param id Route ID string literal for the target route.
96-
* @returns A `RouteApi` instance bound to the given route ID.
97-
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/getRouteApiFunction
98-
*/
9981
/**
10082
* Returns a route-specific API that exposes type-safe hooks pre-bound
10183
* to a single route ID. Useful for consuming a route's APIs from files

packages/react-router/src/useRouter.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ import warning from 'tiny-warning'
33
import { getRouterContext } from './routerContext'
44
import type { AnyRouter, RegisteredRouter } from '@tanstack/router-core'
55

6-
/**
7-
* Access the current TanStack Router instance from React context.
8-
* Must be used within a `RouterProvider`.
9-
*
10-
* Options:
11-
* - `warn`: Log a warning if no router context is found (default: true).
12-
*
13-
* @returns The registered router instance.
14-
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useRouterHook
15-
*/
166
/**
177
* Access the current TanStack Router instance from React context.
188
* Must be used within a `RouterProvider`.

packages/router-core/src/path.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Segment {
2222
readonly hasStaticAfter?: boolean
2323
}
2424

25+
/** Join path segments, cleaning duplicate slashes between parts. */
2526
export function joinPaths(paths: Array<string | undefined>) {
2627
return cleanPath(
2728
paths
@@ -32,23 +33,28 @@ export function joinPaths(paths: Array<string | undefined>) {
3233
)
3334
}
3435

36+
/** Remove repeated slashes from a path string. */
3537
export function cleanPath(path: string) {
3638
// remove double slashes
3739
return path.replace(/\/{2,}/g, '/')
3840
}
3941

42+
/** Trim leading slashes (except preserving root '/'). */
4043
export function trimPathLeft(path: string) {
4144
return path === '/' ? path : path.replace(/^\/{1,}/, '')
4245
}
4346

47+
/** Trim trailing slashes (except preserving root '/'). */
4448
export function trimPathRight(path: string) {
4549
return path === '/' ? path : path.replace(/\/{1,}$/, '')
4650
}
4751

52+
/** Trim both leading and trailing slashes. */
4853
export function trimPath(path: string) {
4954
return trimPathRight(trimPathLeft(path))
5055
}
5156

57+
/** Remove a trailing slash from value when appropriate for comparisons. */
5258
export function removeTrailingSlash(value: string, basepath: string): string {
5359
if (value?.endsWith('/') && value !== '/' && value !== `${basepath}/`) {
5460
return value.slice(0, -1)
@@ -149,6 +155,10 @@ function segmentToString(segment: Segment): string {
149155
return value
150156
}
151157

158+
/**
159+
* Resolve a destination path against a base, honoring trailing-slash policy
160+
* and supporting relative segments (`.`/`..`) and absolute `to` values.
161+
*/
152162
export function resolvePath({
153163
base,
154164
to,
@@ -384,6 +394,13 @@ type InterPolatePathResult = {
384394
usedParams: Record<string, unknown>
385395
isMissingParams: boolean // true if any params were not available when being looked up in the params object
386396
}
397+
/**
398+
* Interpolate params and wildcards into a route path template.
399+
*
400+
* - Encodes params safely (configurable allowed characters)
401+
* - Supports `{-$optional}` segments, `{prefix{$id}suffix}` and `{$}` wildcards
402+
* - Optionally leaves placeholders or wildcards in place
403+
*/
387404
export function interpolatePath({
388405
path,
389406
params,
@@ -510,6 +527,10 @@ function encodePathParam(value: string, decodeCharMap?: Map<string, string>) {
510527
return encoded
511528
}
512529

530+
/**
531+
* Match a pathname against a route destination and return extracted params
532+
* or `undefined`. Uses the same parsing as the router for consistency.
533+
*/
513534
export function matchPathname(
514535
currentPathname: string,
515536
matchLocation: Pick<MatchLocation, 'to' | 'fuzzy' | 'caseSensitive'>,
@@ -525,6 +546,7 @@ export function matchPathname(
525546
return pathParams ?? {}
526547
}
527548

549+
/** Low-level matcher that compares two path strings and extracts params. */
528550
export function matchByPath(
529551
from: string,
530552
{

packages/router-core/src/qss.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* // Expected output: "token=foo&key=value"
2323
* ```
2424
*/
25+
/** Encode a plain object into a URL query string using URLSearchParams. */
2526
export function encode(
2627
obj: Record<string, any>,
2728
stringify: (value: any) => string = String,
@@ -62,6 +63,7 @@ function toValue(str: unknown) {
6263
* // Example input: decode("token=foo&key=value")
6364
* // Expected output: { "token": "foo", "key": "value" }
6465
*/
66+
/** Decode a URL query string into an object with basic type coercion. */
6567
export function decode(str: any): any {
6668
const searchParams = new URLSearchParams(str)
6769

packages/router-core/src/redirect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,14 @@ export function isRedirect(obj: any): obj is AnyRedirect {
114114
return obj instanceof Response && !!(obj as any).options
115115
}
116116

117+
/** True if value is a redirect with a resolved `href` location. */
117118
export function isResolvedRedirect(
118119
obj: any,
119120
): obj is AnyRedirect & { options: { href: string } } {
120121
return isRedirect(obj) && !!obj.options.href
121122
}
122123

124+
/** Parse a serialized redirect object back into a redirect Response. */
123125
export function parseRedirect(obj: any) {
124126
if (obj !== null && typeof obj === 'object' && obj.isSerializedRedirect) {
125127
return redirect(obj)

packages/router-core/src/rewrite.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { joinPaths, trimPath } from './path'
22
import type { LocationRewrite } from './router'
33

4+
/** Compose multiple rewrite pairs into a single in/out rewrite. */
45
export function composeRewrites(rewrites: Array<LocationRewrite>) {
56
return {
67
input: ({ url }) => {
@@ -18,6 +19,7 @@ export function composeRewrites(rewrites: Array<LocationRewrite>) {
1819
} satisfies LocationRewrite
1920
}
2021

22+
/** Create a rewrite pair that strips/adds a basepath on input/output. */
2123
export function rewriteBasepath(opts: {
2224
basepath: string
2325
caseSensitive?: boolean
@@ -54,6 +56,7 @@ export function rewriteBasepath(opts: {
5456
} satisfies LocationRewrite
5557
}
5658

59+
/** Execute a location input rewrite if provided. */
5760
export function executeRewriteInput(
5861
rewrite: LocationRewrite | undefined,
5962
url: URL,
@@ -69,6 +72,7 @@ export function executeRewriteInput(
6972
return url
7073
}
7174

75+
/** Execute a location output rewrite if provided. */
7276
export function executeRewriteOutput(
7377
rewrite: LocationRewrite | undefined,
7478
url: URL,

packages/router-core/src/router.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,15 @@ export type CreateRouterFn = <
842842
TDehydrated
843843
>
844844

845+
/**
846+
* Core, framework-agnostic router engine that powers TanStack Router.
847+
*
848+
* Provides navigation, matching, loading, preloading, caching and event APIs
849+
* used by framework adapters (React/Solid). Prefer framework helpers like
850+
* `createRouter` in app code.
851+
*
852+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/RouterType
853+
*/
845854
export class RouterCore<
846855
in out TRouteTree extends AnyRoute,
847856
in out TTrailingSlashOption extends TrailingSlashOption,
@@ -1096,6 +1105,12 @@ export class RouterCore<
10961105
}
10971106
}
10981107

1108+
/**
1109+
* Subscribe to router lifecycle events like `onBeforeNavigate`, `onLoad`,
1110+
* `onResolved`, etc. Returns an unsubscribe function.
1111+
*
1112+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/RouterEventsType
1113+
*/
10991114
subscribe: SubscribeFn = (eventType, fn) => {
11001115
const listener: RouterListener<any> = {
11011116
eventType,
@@ -1117,6 +1132,10 @@ export class RouterCore<
11171132
})
11181133
}
11191134

1135+
/**
1136+
* Parse a HistoryLocation into a strongly-typed ParsedLocation using the
1137+
* current router options, rewrite rules and search parser/stringifier.
1138+
*/
11201139
parseLocation: ParseLocationFn<TRouteTree> = (
11211140
locationToParse,
11221141
previousLocation,
@@ -1172,6 +1191,7 @@ export class RouterCore<
11721191
return location
11731192
}
11741193

1194+
/** Resolve a path against the router basepath and trailing-slash policy. */
11751195
resolvePathWithBase = (from: string, path: string) => {
11761196
const resolvedPath = resolvePath({
11771197
base: from,
@@ -1538,6 +1558,13 @@ export class RouterCore<
15381558
})
15391559
}
15401560

1561+
/**
1562+
* Build the next ParsedLocation from navigation options without committing.
1563+
* Resolves `to`/`from`, params/search/hash/state, applies search validation
1564+
* and middlewares, and returns a stable, stringified location object.
1565+
*
1566+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/RouterType#buildlocation-method
1567+
*/
15411568
buildLocation: BuildLocationFn = (opts) => {
15421569
const build = (
15431570
dest: BuildNextOptions & {
@@ -1785,6 +1812,10 @@ export class RouterCore<
17851812

17861813
commitLocationPromise: undefined | ControlledPromise<void>
17871814

1815+
/**
1816+
* Commit a previously built location to history (push/replace), optionally
1817+
* using view transitions and scroll restoration options.
1818+
*/
17881819
commitLocation: CommitLocationFn = ({
17891820
viewTransition,
17901821
ignoreBlocker,
@@ -1875,6 +1906,7 @@ export class RouterCore<
18751906
return this.commitLocationPromise
18761907
}
18771908

1909+
/** Convenience helper: build a location from options, then commit it. */
18781910
buildAndCommitLocation = ({
18791911
replace,
18801912
resetScroll,
@@ -1911,6 +1943,13 @@ export class RouterCore<
19111943
})
19121944
}
19131945

1946+
/**
1947+
* Imperatively navigate using standard `NavigateOptions`. When `reloadDocument`
1948+
* or an absolute `href` is provided, performs a full document navigation.
1949+
* Otherwise, builds and commits a client-side location.
1950+
*
1951+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType
1952+
*/
19141953
navigate: NavigateFn = ({ to, reloadDocument, href, ...rest }) => {
19151954
if (!reloadDocument && href) {
19161955
try {

0 commit comments

Comments
 (0)