Skip to content

Commit 91d9940

Browse files
Add jsdoc annotations to public api (#5587)
* docs: Add JSDoc annotations to core router APIs Co-authored-by: tannerlinsley <tannerlinsley@gmail.com> * Remove JSDoc notes file Co-authored-by: tannerlinsley <tannerlinsley@gmail.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent 47bc1c3 commit 91d9940

File tree

9 files changed

+61
-3
lines changed

9 files changed

+61
-3
lines changed

packages/router-core/src/defer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { defaultSerializeError } from './router'
22

3+
/**
4+
* Well-known symbol used by {@link defer} to tag a promise with
5+
* its deferred state. Consumers can read `promise[TSR_DEFERRED_PROMISE]`
6+
* to access `status`, `data`, or `error`.
7+
*/
38
export const TSR_DEFERRED_PROMISE = Symbol.for('TSR_DEFERRED_PROMISE')
49

510
export type DeferredPromiseState<T> =

packages/router-core/src/path.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export function removeTrailingSlash(value: string, basepath: string): string {
6666
// see the usage in the isActive under useLinkProps
6767
// /sample/path1 = /sample/path1/
6868
// /sample/path1/some <> /sample/path1
69+
/**
70+
* Compare two pathnames for exact equality after normalizing trailing slashes
71+
* relative to the provided `basepath`.
72+
*/
6973
export function exactPathTest(
7074
pathName1: string,
7175
pathName2: string,
@@ -222,6 +226,10 @@ export const parseRoutePathSegments = (
222226
cache?: ParsePathnameCache,
223227
): ReadonlyArray<Segment> => parsePathname(pathname, cache, false)
224228

229+
/**
230+
* Parse a pathname into an array of typed segments used by the router's
231+
* matcher. Results are optionally cached via an LRU cache.
232+
*/
225233
export const parsePathname = (
226234
pathname?: string,
227235
cache?: ParsePathnameCache,

packages/router-core/src/process-route-tree.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ export type ProcessRouteTreeResult<TRouteLike extends RouteLike> = {
184184
flatRoutes: Array<TRouteLike>
185185
}
186186

187+
/**
188+
* Build lookup maps and a specificity-sorted flat list from a route tree.
189+
* Returns `routesById`, `routesByPath`, and `flatRoutes`.
190+
*/
191+
/**
192+
* Build lookup maps and a specificity-sorted flat list from a route tree.
193+
* Returns `routesById`, `routesByPath`, and `flatRoutes`.
194+
*/
187195
export function processRouteTree<TRouteLike extends RouteLike>({
188196
routeTree,
189197
initRoute,

packages/router-core/src/redirect.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export type ResolvedRedirect<
7171
* @returns A Response augmented with router navigation options.
7272
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/redirectFunction
7373
*/
74+
/**
75+
* Create a redirect Response understood by TanStack Router.
76+
* Use inside loaders/beforeLoad or server handlers to trigger navigation.
77+
*/
7478
export function redirect<
7579
TRouter extends AnyRouter = RegisteredRouter,
7680
const TTo extends string | undefined = '.',
@@ -109,18 +113,21 @@ export function redirect<
109113
return response as Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
110114
}
111115

116+
/** Check whether a value is a TanStack Router redirect Response. */
112117
/** Check whether a value is a TanStack Router redirect Response. */
113118
export function isRedirect(obj: any): obj is AnyRedirect {
114119
return obj instanceof Response && !!(obj as any).options
115120
}
116121

122+
/** True if value is a redirect with a resolved `href` location. */
117123
/** True if value is a redirect with a resolved `href` location. */
118124
export function isResolvedRedirect(
119125
obj: any,
120126
): obj is AnyRedirect & { options: { href: string } } {
121127
return isRedirect(obj) && !!obj.options.href
122128
}
123129

130+
/** Parse a serialized redirect object back into a redirect Response. */
124131
/** Parse a serialized redirect object back into a redirect Response. */
125132
export function parseRedirect(obj: any) {
126133
if (obj !== null && typeof obj === 'object' && obj.isSerializedRedirect) {

packages/router-core/src/root.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
/** Stable identifier used for the root route in a route tree. */
12
export const rootRouteId = '__root__'
23
export type RootRouteId = typeof rootRouteId

packages/router-core/src/router.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,10 @@ export interface ViewTransitionOptions {
778778
}
779779

780780
// TODO where is this used? can we remove this?
781+
/**
782+
* Convert an unknown error into a minimal, serializable object.
783+
* Includes name and message (and stack in development).
784+
*/
781785
export function defaultSerializeError(err: unknown) {
782786
if (err instanceof Error) {
783787
const obj = {
@@ -797,6 +801,7 @@ export function defaultSerializeError(err: unknown) {
797801
}
798802
}
799803

804+
/** Options for configuring trailing-slash behavior. */
800805
export const trailingSlashOptions = {
801806
always: 'always',
802807
never: 'never',
@@ -806,6 +811,10 @@ export const trailingSlashOptions = {
806811
export type TrailingSlashOption =
807812
(typeof trailingSlashOptions)[keyof typeof trailingSlashOptions]
808813

814+
/**
815+
* Compute whether path, href or hash changed between previous and current
816+
* resolved locations in router state.
817+
*/
809818
export function getLocationChangeInfo(routerState: {
810819
resolvedLocation?: ParsedLocation
811820
location: ParsedLocation
@@ -2517,8 +2526,10 @@ export class RouterCore<
25172526
}
25182527
}
25192528

2529+
/** Error thrown when search parameter validation fails. */
25202530
export class SearchParamError extends Error {}
25212531

2532+
/** Error thrown when path parameter parsing/validation fails. */
25222533
export class PathParamError extends Error {}
25232534

25242535
const normalize = (str: string) =>
@@ -2527,9 +2538,10 @@ function comparePaths(a: string, b: string) {
25272538
return normalize(a) === normalize(b)
25282539
}
25292540

2530-
// A function that takes an import() argument which is a function and returns a new function that will
2531-
// proxy arguments from the caller to the imported function, retaining all type
2532-
// information along the way
2541+
/**
2542+
* Lazily import a module function and forward arguments to it, retaining
2543+
* parameter and return types for the selected export key.
2544+
*/
25332545
export function lazyFn<
25342546
T extends Record<string, (...args: Array<any>) => any>,
25352547
TKey extends keyof T = 'default',
@@ -2542,6 +2554,7 @@ export function lazyFn<
25422554
}
25432555
}
25442556

2557+
/** Create an initial RouterState from a parsed location. */
25452558
export function getInitialRouterState(
25462559
location: ParsedLocation,
25472560
): RouterState<any> {
@@ -2587,6 +2600,10 @@ function validateSearch(validateSearch: AnyValidator, input: unknown): unknown {
25872600
return {}
25882601
}
25892602

2603+
/**
2604+
* Build the matched route chain and extract params for a pathname.
2605+
* Falls back to the root route if no specific route is found.
2606+
*/
25902607
export function getMatchedRoutes<TRouteLike extends RouteLike>({
25912608
pathname,
25922609
routePathname,

packages/router-core/src/scroll-restoration.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function getSafeSessionStorage() {
3434
}
3535

3636
/** SessionStorage key used to persist scroll restoration state. */
37+
/** SessionStorage key used to store scroll positions across navigations. */
3738
export const storageKey = 'tsr-scroll-restoration-v1_3'
3839

3940
const throttle = (fn: (...args: Array<any>) => void, wait: number) => {
@@ -71,6 +72,7 @@ function createScrollRestorationCache(): ScrollRestorationCache | null {
7172
}
7273
}
7374

75+
/** In-memory handle to the persisted scroll restoration cache. */
7476
/** In-memory handle to the persisted scroll restoration cache. */
7577
export const scrollRestorationCache = createScrollRestorationCache()
7678

@@ -81,6 +83,9 @@ export const scrollRestorationCache = createScrollRestorationCache()
8183
* The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.
8284
*/
8385

86+
/**
87+
* Default scroll restoration cache key: location state key or full href.
88+
*/
8489
/**
8590
* Default scroll restoration cache key: location state key or full href.
8691
*/

packages/router-core/src/searchParams.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { decode, encode } from './qss'
22
import type { AnySchema } from './validators'
33

4+
/** Default `parseSearch` that strips leading '?' and JSON-parses values. */
45
/** Default `parseSearch` that strips leading '?' and JSON-parses values. */
56
export const defaultParseSearch = parseSearchWith(JSON.parse)
67
export const defaultStringifySearch = stringifySearchWith(

packages/router-core/src/ssr/serializer/transformer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export type UnionizeSerializationAdaptersInput<
3232
TAdapters extends ReadonlyArray<AnySerializationAdapter>,
3333
> = TAdapters[number]['~types']['input']
3434

35+
/**
36+
* Create a strongly-typed serialization adapter for SSR hydration.
37+
* Use to register custom types with the router serializer.
38+
*/
3539
export function createSerializationAdapter<
3640
TInput = unknown,
3741
TOutput = unknown,
@@ -154,6 +158,7 @@ export interface SerializationAdapterTypes<
154158

155159
export type AnySerializationAdapter = SerializationAdapter<any, any, any>
156160

161+
/** Create a Seroval plugin for server-side serialization only. */
157162
export function makeSsrSerovalPlugin(
158163
serializationAdapter: AnySerializationAdapter,
159164
options: { didRun: boolean },
@@ -182,6 +187,7 @@ export function makeSsrSerovalPlugin(
182187
})
183188
}
184189

190+
/** Create a Seroval plugin for client/server symmetric (de)serialization. */
185191
export function makeSerovalPlugin(
186192
serializationAdapter: AnySerializationAdapter,
187193
): Plugin<any, SerovalNode> {

0 commit comments

Comments
 (0)