UNPKG

6.89 kBTypeScriptView Raw
1import { R as RouteModule, L as LinkDescriptor, a as Location, P as Pretty, M as MetaDescriptor, G as GetLoaderData, S as ServerDataFunctionArgs, u as unstable_MiddlewareNextFunction, C as ClientDataFunctionArgs, b as ServerDataFrom, N as Normalize, c as GetActionData } from '../../routeModules-BR2FO0ix.js';
2import { R as RouteFiles, P as Pages } from '../../register-DiOIlEq5.js';
3import 'react';
4
5type MaybePromise<T> = T | Promise<T>;
6type Props = {
7 params: unknown;
8 loaderData: unknown;
9 actionData: unknown;
10};
11type RouteInfo = Props & {
12 module: RouteModule;
13 matches: Array<MatchInfo>;
14};
15type MatchInfo = {
16 id: string;
17 module: RouteModule;
18};
19type MetaMatch<T extends MatchInfo> = Pretty<{
20 id: T["id"];
21 params: Record<string, string | undefined>;
22 pathname: string;
23 meta: MetaDescriptor[];
24 data: GetLoaderData<T["module"]>;
25 handle?: unknown;
26 error?: unknown;
27}>;
28type MetaMatches<T extends Array<MatchInfo>> = T extends [infer F extends MatchInfo, ...infer R extends Array<MatchInfo>] ? [MetaMatch<F>, ...MetaMatches<R>] : Array<MetaMatch<MatchInfo> | undefined>;
29type CreateMetaArgs<T extends RouteInfo> = {
30 /** This is the current router `Location` object. This is useful for generating tags for routes at specific paths or query parameters. */
31 location: Location;
32 /** {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route. */
33 params: T["params"];
34 /** The return value for this route's server loader function */
35 data: T["loaderData"] | undefined;
36 /** Thrown errors that trigger error boundaries will be passed to the meta function. This is useful for generating metadata for error pages. */
37 error?: unknown;
38 /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react_router.UIMatch.html route matches}, including parent route matches. */
39 matches: MetaMatches<T["matches"]>;
40};
41type MetaDescriptors = MetaDescriptor[];
42type HeadersArgs = {
43 loaderHeaders: Headers;
44 parentHeaders: Headers;
45 actionHeaders: Headers;
46 errorHeaders: Headers | undefined;
47};
48type CreateServerMiddlewareFunction<T extends RouteInfo> = (args: ServerDataFunctionArgs<T["params"]>, next: unstable_MiddlewareNextFunction<Response>) => MaybePromise<Response | void>;
49type CreateClientMiddlewareFunction<T extends RouteInfo> = (args: ClientDataFunctionArgs<T["params"]>, next: unstable_MiddlewareNextFunction<undefined>) => MaybePromise<void>;
50type CreateServerLoaderArgs<T extends RouteInfo> = ServerDataFunctionArgs<T["params"]>;
51type CreateClientLoaderArgs<T extends RouteInfo> = ClientDataFunctionArgs<T["params"]> & {
52 /** 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). */
53 serverLoader: () => Promise<ServerDataFrom<T["module"]["loader"]>>;
54};
55type CreateServerActionArgs<T extends RouteInfo> = ServerDataFunctionArgs<T["params"]>;
56type CreateClientActionArgs<T extends RouteInfo> = ClientDataFunctionArgs<T["params"]> & {
57 /** 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. */
58 serverAction: () => Promise<ServerDataFrom<T["module"]["action"]>>;
59};
60type CreateHydrateFallbackProps<T extends RouteInfo> = {
61 params: T["params"];
62 loaderData?: T["loaderData"];
63 actionData?: T["actionData"];
64};
65type Match<T extends MatchInfo> = Pretty<{
66 id: T["id"];
67 params: Record<string, string | undefined>;
68 pathname: string;
69 data: GetLoaderData<T["module"]>;
70 handle: unknown;
71}>;
72type Matches<T extends Array<MatchInfo>> = T extends [infer F extends MatchInfo, ...infer R extends Array<MatchInfo>] ? [Match<F>, ...Matches<R>] : Array<Match<MatchInfo> | undefined>;
73type CreateComponentProps<T extends RouteInfo> = {
74 /**
75 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
76 * @example
77 * // app/routes.ts
78 * route("teams/:teamId", "./team.tsx"),
79 *
80 * // app/team.tsx
81 * export default function Component({
82 * params,
83 * }: Route.ComponentProps) {
84 * params.teamId;
85 * // ^ string
86 * }
87 **/
88 params: T["params"];
89 /** The data returned from the `loader` or `clientLoader` */
90 loaderData: T["loaderData"];
91 /** The data returned from the `action` or `clientAction` following an action submission. */
92 actionData?: T["actionData"];
93 /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react_router.UIMatch.html route matches}, including parent route matches. */
94 matches: Matches<T["matches"]>;
95};
96type CreateErrorBoundaryProps<T extends RouteInfo> = {
97 /**
98 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
99 * @example
100 * // app/routes.ts
101 * route("teams/:teamId", "./team.tsx"),
102 *
103 * // app/team.tsx
104 * export function ErrorBoundary({
105 * params,
106 * }: Route.ErrorBoundaryProps) {
107 * params.teamId;
108 * // ^ string
109 * }
110 **/
111 params: T["params"];
112 error: unknown;
113 loaderData?: T["loaderData"];
114 actionData?: T["actionData"];
115};
116type GetAnnotations<Info extends RouteInfo> = {
117 LinkDescriptors: LinkDescriptor[];
118 LinksFunction: () => LinkDescriptor[];
119 MetaArgs: CreateMetaArgs<Info>;
120 MetaDescriptors: MetaDescriptors;
121 MetaFunction: (args: CreateMetaArgs<Info>) => MetaDescriptors;
122 HeadersArgs: HeadersArgs;
123 HeadersFunction: (args: HeadersArgs) => Headers | HeadersInit;
124 unstable_MiddlewareFunction: CreateServerMiddlewareFunction<Info>;
125 unstable_ClientMiddlewareFunction: CreateClientMiddlewareFunction<Info>;
126 LoaderArgs: CreateServerLoaderArgs<Info>;
127 ClientLoaderArgs: CreateClientLoaderArgs<Info>;
128 ActionArgs: CreateServerActionArgs<Info>;
129 ClientActionArgs: CreateClientActionArgs<Info>;
130 HydrateFallbackProps: CreateHydrateFallbackProps<Info>;
131 ComponentProps: CreateComponentProps<Info>;
132 ErrorBoundaryProps: CreateErrorBoundaryProps<Info>;
133};
134
135type Params<RouteFile extends keyof RouteFiles> = Normalize<Pages[RouteFiles[RouteFile]["page"]]["params"]>;
136
137type GetInfo<T extends {
138 file: keyof RouteFiles;
139 module: RouteModule;
140}> = {
141 params: Params<T["file"]>;
142 loaderData: GetLoaderData<T["module"]>;
143 actionData: GetActionData<T["module"]>;
144};
145
146export type { GetAnnotations, GetInfo };