UNPKG

8.38 kBTypeScriptView Raw
1
2import { Location } from "../router/history.js";
3import { DataStrategyResult, MiddlewareNextFunction } from "../router/utils.js";
4import { LinkDescriptor } from "../router/links.js";
5import { MetaDescriptor } from "../dom/ssr/routeModules.js";
6import { Func, Pretty } from "./utils.js";
7import { RouteModule } from "./route-module.js";
8import { ClientDataFunctionArgs, GetLoaderData, ServerDataFrom, ServerDataFunctionArgs } from "./route-data.js";
9
10//#region lib/types/route-module-annotations.d.ts
11type MaybePromise<T> = T | Promise<T>;
12type Props = {
13 params: unknown;
14 loaderData: unknown;
15 actionData: unknown;
16};
17type MatchInfo = {
18 id: string;
19 module: RouteModule;
20};
21type MatchInfoWithLoaderData = {
22 id: string;
23 module?: never;
24 loaderData: unknown;
25};
26type AnyMatchInfo = MatchInfo | MatchInfoWithLoaderData;
27type RouteInfo = Props & {
28 module: RouteModule;
29 matches: Array<AnyMatchInfo>;
30};
31type MatchData<T extends AnyMatchInfo> = T extends MatchInfo ? GetLoaderData<T["module"]> : T extends MatchInfoWithLoaderData ? T["loaderData"] : never;
32type MetaMatch<T extends AnyMatchInfo> = Pretty<{
33 id: T["id"];
34 params: Record<string, string | undefined>;
35 pathname: string;
36 meta: MetaDescriptor[];
37 loaderData: MatchData<T>;
38 handle?: unknown;
39 error?: unknown;
40}>;
41type MetaMatches<T extends Array<AnyMatchInfo>> = T extends [infer F extends AnyMatchInfo, ...infer R extends Array<AnyMatchInfo>] ? [MetaMatch<F>, ...MetaMatches<R>] : T extends [] ? [] : Array<MetaMatch<AnyMatchInfo> | undefined>;
42type HasErrorBoundary<T extends RouteInfo> = T["module"] extends {
43 ErrorBoundary: Func;
44} ? true : false;
45type CreateMetaArgs<T extends RouteInfo> = {
46 /** This is the current router `Location` object. This is useful for generating tags for routes at specific paths or query parameters. */location: Location; /** {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route. */
47 params: T["params"]; /** The return value for this route's server loader function */
48 loaderData: T["loaderData"] | (HasErrorBoundary<T> extends true ? undefined : never); /** Thrown errors that trigger error boundaries will be passed to the meta function. This is useful for generating metadata for error pages. */
49 error?: unknown; /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react-router.UIMatch.html route matches}, including parent route matches. */
50 matches: MetaMatches<T["matches"]>;
51};
52type MetaDescriptors = MetaDescriptor[];
53type HeadersArgs = {
54 loaderHeaders: Headers;
55 parentHeaders: Headers;
56 actionHeaders: Headers;
57 errorHeaders: Headers | undefined;
58};
59type CreateServerMiddlewareFunction<T extends RouteInfo> = (args: ServerDataFunctionArgs<T["params"]>, next: MiddlewareNextFunction<Response>) => MaybePromise<Response | void>;
60type CreateClientMiddlewareFunction<T extends RouteInfo> = (args: ClientDataFunctionArgs<T["params"]>, next: MiddlewareNextFunction<Record<string, DataStrategyResult>>) => MaybePromise<Record<string, DataStrategyResult> | void>;
61type CreateServerLoaderArgs<T extends RouteInfo> = ServerDataFunctionArgs<T["params"]>;
62type CreateClientLoaderArgs<T extends RouteInfo> = ClientDataFunctionArgs<T["params"]> & {
63 /** This is an asynchronous function to get the data from the server loader for this route. On client-side navigations, this will make a {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API fetch} call to the React Router server loader. If you opt-into running your clientLoader on hydration, then this function will return the data that was already loaded on the server (via Promise.resolve). */serverLoader: () => Promise<ServerDataFrom<T["module"]["loader"]>>;
64};
65type CreateServerActionArgs<T extends RouteInfo> = ServerDataFunctionArgs<T["params"]>;
66type CreateClientActionArgs<T extends RouteInfo> = ClientDataFunctionArgs<T["params"]> & {
67 /** This is an asynchronous function that makes the {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API fetch} call to the React Router server action for this route. */serverAction: () => Promise<ServerDataFrom<T["module"]["action"]>>;
68};
69type CreateHydrateFallbackProps<T extends RouteInfo, RSCEnabled extends boolean> = {
70 params: T["params"];
71} & (RSCEnabled extends true ? {
72 /** The data returned from the `loader` */loaderData?: ServerDataFrom<T["module"]["loader"]>; /** The data returned from the `action` following an action submission. */
73 actionData?: ServerDataFrom<T["module"]["action"]>;
74} : {
75 /** The data returned from the `loader` or `clientLoader` */loaderData?: T["loaderData"]; /** The data returned from the `action` or `clientAction` following an action submission. */
76 actionData?: T["actionData"];
77});
78type Match<T extends AnyMatchInfo> = Pretty<{
79 id: T["id"];
80 params: Record<string, string | undefined>;
81 pathname: string;
82 loaderData: MatchData<T>;
83 handle: unknown;
84}>;
85type Matches<T extends Array<AnyMatchInfo>> = T extends [infer F extends AnyMatchInfo, ...infer R extends Array<AnyMatchInfo>] ? [Match<F>, ...Matches<R>] : Array<Match<AnyMatchInfo> | undefined>;
86type CreateComponentProps<T extends RouteInfo, RSCEnabled extends boolean> = {
87 /**
88 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
89 * @example
90 * // app/routes.ts
91 * route("teams/:teamId", "./team.tsx"),
92 *
93 * // app/team.tsx
94 * export default function Component({
95 * params,
96 * }: Route.ComponentProps) {
97 * params.teamId;
98 * // ^ string
99 * }
100 **/
101 params: T["params"]; /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react-router.UIMatch.html route matches}, including parent route matches. */
102 matches: Matches<T["matches"]>;
103} & (RSCEnabled extends true ? {
104 /** The data returned from the `loader` */loaderData: ServerDataFrom<T["module"]["loader"]>; /** The data returned from the `action` following an action submission. */
105 actionData?: ServerDataFrom<T["module"]["action"]>;
106} : {
107 /** The data returned from the `loader` or `clientLoader` */loaderData: T["loaderData"]; /** The data returned from the `action` or `clientAction` following an action submission. */
108 actionData?: T["actionData"];
109});
110type CreateErrorBoundaryProps<T extends RouteInfo, RSCEnabled extends boolean> = {
111 /**
112 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
113 * @example
114 * // app/routes.ts
115 * route("teams/:teamId", "./team.tsx"),
116 *
117 * // app/team.tsx
118 * export function ErrorBoundary({
119 * params,
120 * }: Route.ErrorBoundaryProps) {
121 * params.teamId;
122 * // ^ string
123 * }
124 **/
125 params: T["params"];
126 error: unknown;
127} & (RSCEnabled extends true ? {
128 /** The data returned from the `loader` */loaderData?: ServerDataFrom<T["module"]["loader"]>; /** The data returned from the `action` following an action submission. */
129 actionData?: ServerDataFrom<T["module"]["action"]>;
130} : {
131 /** The data returned from the `loader` or `clientLoader` */loaderData?: T["loaderData"]; /** The data returned from the `action` or `clientAction` following an action submission. */
132 actionData?: T["actionData"];
133});
134type GetAnnotations<Info extends RouteInfo> = {
135 LinkDescriptors: LinkDescriptor[];
136 LinksFunction: () => LinkDescriptor[];
137 MetaArgs: CreateMetaArgs<Info>;
138 MetaDescriptors: MetaDescriptors;
139 MetaFunction: (args: CreateMetaArgs<Info>) => MetaDescriptors;
140 HeadersArgs: HeadersArgs;
141 HeadersFunction: (args: HeadersArgs) => Headers | HeadersInit;
142 MiddlewareFunction: CreateServerMiddlewareFunction<Info>;
143 ClientMiddlewareFunction: CreateClientMiddlewareFunction<Info>;
144 LoaderArgs: CreateServerLoaderArgs<Info>;
145 ClientLoaderArgs: CreateClientLoaderArgs<Info>;
146 ActionArgs: CreateServerActionArgs<Info>;
147 ClientActionArgs: CreateClientActionArgs<Info>;
148 HydrateFallbackProps: CreateHydrateFallbackProps<Info, false>;
149 ServerHydrateFallbackProps: CreateHydrateFallbackProps<Info, true>;
150 ComponentProps: CreateComponentProps<Info, false>;
151 ServerComponentProps: CreateComponentProps<Info, true>;
152 ErrorBoundaryProps: CreateErrorBoundaryProps<Info, false>;
153 ServerErrorBoundaryProps: CreateErrorBoundaryProps<Info, true>;
154};
155//#endregion
156export { GetAnnotations };
\No newline at end of file