import { Location } from "../router/history.js"; import { DataStrategyResult, MiddlewareNextFunction } from "../router/utils.js"; import { LinkDescriptor } from "../router/links.js"; import { Func, Pretty } from "./utils.js"; import { RouteModule } from "./route-module.js"; import { ClientDataFunctionArgs, GetLoaderData, ServerDataFrom, ServerDataFunctionArgs } from "./route-data.js"; import { MetaDescriptor } from "../dom/ssr/routeModules.js"; //#region lib/types/route-module-annotations.d.ts type MaybePromise = T | Promise; type Props = { params: unknown; loaderData: unknown; actionData: unknown; }; type RouteInfo = Props & { module: RouteModule; matches: Array; }; type MatchInfo = { id: string; module: RouteModule; }; type MetaMatch = Pretty<{ id: T["id"]; params: Record; pathname: string; meta: MetaDescriptor[]; loaderData: GetLoaderData; handle?: unknown; error?: unknown; }>; type MetaMatches> = T extends [infer F extends MatchInfo, ...infer R extends Array] ? [MetaMatch, ...MetaMatches] : Array | undefined>; type HasErrorBoundary = T["module"] extends { ErrorBoundary: Func; } ? true : false; type CreateMetaArgs = { /** 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. */ params: T["params"]; /** The return value for this route's server loader function */ loaderData: T["loaderData"] | (HasErrorBoundary 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. */ error?: unknown; /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react-router.UIMatch.html route matches}, including parent route matches. */ matches: MetaMatches; }; type MetaDescriptors = MetaDescriptor[]; type HeadersArgs = { loaderHeaders: Headers; parentHeaders: Headers; actionHeaders: Headers; errorHeaders: Headers | undefined; }; type CreateServerMiddlewareFunction = (args: ServerDataFunctionArgs, next: MiddlewareNextFunction) => MaybePromise; type CreateClientMiddlewareFunction = (args: ClientDataFunctionArgs, next: MiddlewareNextFunction>) => MaybePromise | void>; type CreateServerLoaderArgs = ServerDataFunctionArgs; type CreateClientLoaderArgs = ClientDataFunctionArgs & { /** 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>; }; type CreateServerActionArgs = ServerDataFunctionArgs; type CreateClientActionArgs = ClientDataFunctionArgs & { /** 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>; }; type CreateHydrateFallbackProps = { params: T["params"]; } & (RSCEnabled extends true ? { /** The data returned from the `loader` */loaderData?: ServerDataFrom; /** The data returned from the `action` following an action submission. */ actionData?: ServerDataFrom; } : { /** The data returned from the `loader` or `clientLoader` */loaderData?: T["loaderData"]; /** The data returned from the `action` or `clientAction` following an action submission. */ actionData?: T["actionData"]; }); type Match = Pretty<{ id: T["id"]; params: Record; pathname: string; loaderData: GetLoaderData; handle: unknown; }>; type Matches> = T extends [infer F extends MatchInfo, ...infer R extends Array] ? [Match, ...Matches] : Array | undefined>; type CreateComponentProps = { /** * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route. * @example * // app/routes.ts * route("teams/:teamId", "./team.tsx"), * * // app/team.tsx * export default function Component({ * params, * }: Route.ComponentProps) { * params.teamId; * // ^ string * } **/ 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. */ matches: Matches; } & (RSCEnabled extends true ? { /** The data returned from the `loader` */loaderData: ServerDataFrom; /** The data returned from the `action` following an action submission. */ actionData?: ServerDataFrom; } : { /** The data returned from the `loader` or `clientLoader` */loaderData: T["loaderData"]; /** The data returned from the `action` or `clientAction` following an action submission. */ actionData?: T["actionData"]; }); type CreateErrorBoundaryProps = { /** * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route. * @example * // app/routes.ts * route("teams/:teamId", "./team.tsx"), * * // app/team.tsx * export function ErrorBoundary({ * params, * }: Route.ErrorBoundaryProps) { * params.teamId; * // ^ string * } **/ params: T["params"]; error: unknown; } & (RSCEnabled extends true ? { /** The data returned from the `loader` */loaderData?: ServerDataFrom; /** The data returned from the `action` following an action submission. */ actionData?: ServerDataFrom; } : { /** The data returned from the `loader` or `clientLoader` */loaderData?: T["loaderData"]; /** The data returned from the `action` or `clientAction` following an action submission. */ actionData?: T["actionData"]; }); type GetAnnotations = { LinkDescriptors: LinkDescriptor[]; LinksFunction: () => LinkDescriptor[]; MetaArgs: CreateMetaArgs; MetaDescriptors: MetaDescriptors; MetaFunction: (args: CreateMetaArgs) => MetaDescriptors; HeadersArgs: HeadersArgs; HeadersFunction: (args: HeadersArgs) => Headers | HeadersInit; MiddlewareFunction: CreateServerMiddlewareFunction; ClientMiddlewareFunction: CreateClientMiddlewareFunction; LoaderArgs: CreateServerLoaderArgs; ClientLoaderArgs: CreateClientLoaderArgs; ActionArgs: CreateServerActionArgs; ClientActionArgs: CreateClientActionArgs; HydrateFallbackProps: CreateHydrateFallbackProps; ServerHydrateFallbackProps: CreateHydrateFallbackProps; ComponentProps: CreateComponentProps; ServerComponentProps: CreateComponentProps; ErrorBoundaryProps: CreateErrorBoundaryProps; ServerErrorBoundaryProps: CreateErrorBoundaryProps; }; //#endregion export { GetAnnotations };