Skip to content

Commit a708973

Browse files
Refactor: Improve documentation and types for defer, redirect, and search middleware
Co-authored-by: tannerlinsley <tannerlinsley@gmail.com>
1 parent 1ed3c92 commit a708973

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

packages/router-core/src/defer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ export type DeferredPromise<T> = Promise<T> & {
2222
[TSR_DEFERRED_PROMISE]: DeferredPromiseState<T>
2323
}
2424

25+
/**
26+
* Wrap a promise with a deferred state for use with `<Await>` and `useAwaited`.
27+
*
28+
* The returned promise is augmented with internal state (status/data/error)
29+
* so UI can read progress or suspend until it settles.
30+
*
31+
* @param _promise The promise to wrap.
32+
* @param options Optional config. Provide `serializeError` to customize how
33+
* errors are serialized for transfer.
34+
* @returns The same promise with attached deferred metadata.
35+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/deferFunction
36+
*/
2537
export function defer<T>(
2638
_promise: Promise<T>,
2739
options?: {

packages/router-core/src/redirect.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ export type ResolvedRedirect<
5757
/**
5858
* Create a redirect Response understood by TanStack Router.
5959
*
60-
* Use inside loaders/actions/server functions. If `throw: true` is provided,
61-
* the redirect Response is thrown instead of returned. When an absolute `href`
62-
* is passed and `reloadDocument` is not set, a full-document navigation is
63-
* inferred.
60+
* Use from route `loader`/`beforeLoad` or server functions to trigger a
61+
* navigation. If `throw: true` is set, the redirect is thrown instead of
62+
* returned. When an absolute `href` is supplied and `reloadDocument` is not
63+
* set, a full-document navigation is inferred.
6464
*
65-
* @param opts Options for the redirect, including `href`, `statusCode`,
66-
* `headers`, and standard navigation options (e.g. `to`, `params`, `search`,
67-
* `reloadDocument`).
65+
* @param opts Options for the redirect. Common fields:
66+
* - `href`: absolute URL for external redirects; infers `reloadDocument`.
67+
* - `statusCode`: HTTP status code to use (defaults to 307).
68+
* - `headers`: additional headers to include on the Response.
69+
* - Standard navigation options like `to`, `params`, `search`, `replace`,
70+
* and `reloadDocument` for internal redirects.
6871
* @returns A Response augmented with router navigation options.
6972
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/redirectFunction
7073
*/
@@ -106,6 +109,7 @@ export function redirect<
106109
return response as Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
107110
}
108111

112+
/** Check whether a value is a TanStack Router redirect Response. */
109113
export function isRedirect(obj: any): obj is AnyRedirect {
110114
return obj instanceof Response && !!(obj as any).options
111115
}

packages/router-core/src/searchMiddleware.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import type { SearchMiddleware } from './route'
44
import type { IsRequiredParams } from './link'
55

66
/**
7-
* Search middleware to retain specified search params across links.
7+
* Retain specified search params across navigations.
88
*
9-
* If `keys` is `true`, all existing params are retained. Otherwise, missing
10-
* keys from the current search are merged into the next value produced by
11-
* subsequent middlewares.
9+
* If `keys` is `true`, retain all current params. Otherwise, copy only the
10+
* listed keys from the current search into the next search.
11+
*
12+
* @param keys `true` to retain all, or a list of keys to retain.
13+
* @returns A search middleware suitable for route `search.middlewares`.
14+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/retainSearchParamsFunction
1215
*/
1316
export function retainSearchParams<TSearchSchema extends object>(
1417
keys: Array<keyof TSearchSchema> | true,
@@ -29,11 +32,14 @@ export function retainSearchParams<TSearchSchema extends object>(
2932
}
3033

3134
/**
32-
* Search middleware to remove optional search params from links.
35+
* Remove optional or default-valued search params from navigations.
36+
*
37+
* - Pass `true` (only if there are no required search params) to strip all.
38+
* - Pass an array to always remove those optional keys.
39+
* - Pass an object of default values; keys equal (deeply) to the defaults are removed.
3340
*
34-
* Accepts either a list of keys or an object map of default values. Keys with
35-
* values matching the provided defaults are removed from the final search.
36-
* Passing `true` removes all params.
41+
* @returns A search middleware suitable for route `search.middlewares`.
42+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/stripSearchParamsFunction
3743
*/
3844
export function stripSearchParams<
3945
TSearchSchema,

packages/router-core/src/searchParams.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const defaultStringifySearch = stringifySearchWith(
1515
*
1616
* @param parser Function to parse a string value (e.g. `JSON.parse`).
1717
* @returns A `parseSearch` function compatible with `Router` options.
18-
* @link https://tanstack.com/router/latest/docs/router/framework/react/guide/custom-search-param-serialization
18+
* @link https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization
1919
*/
2020
export function parseSearchWith(parser: (str: string) => any) {
2121
return (searchStr: string): AnySchema => {
@@ -51,7 +51,7 @@ export function parseSearchWith(parser: (str: string) => any) {
5151
* @param stringify Function to serialize a value (e.g. `JSON.stringify`).
5252
* @param parser Optional parser to detect parseable strings.
5353
* @returns A `stringifySearch` function compatible with `Router` options.
54-
* @link https://tanstack.com/router/latest/docs/router/framework/react/guide/custom-search-param-serialization
54+
* @link https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization
5555
*/
5656
export function stringifySearchWith(
5757
stringify: (search: any) => string,

0 commit comments

Comments
 (0)