import { DataWithResponseInit, RouterContextProvider } from "../router/utils.js"; import { Equal, Func, IsAny } from "./utils.js"; import { RouteModule } from "./route-module.js"; import { unstable_SerializesTo } from "./serializes-to.js"; import { Serializable } from "../server-runtime/single-fetch.js"; import { ClientActionFunctionArgs, ClientLoaderFunctionArgs } from "../dom/ssr/routeModules.js"; //#region lib/types/route-data.d.ts type Serialize = T extends unstable_SerializesTo ? To : T extends Serializable ? T : T extends ((...args: any[]) => unknown) ? undefined : T extends Promise ? Promise> : T extends Map ? Map, Serialize> : T extends ReadonlyMap ? ReadonlyMap, Serialize> : T extends Set ? Set> : T extends ReadonlySet ? ReadonlySet> : T extends [] ? [] : T extends readonly [infer F, ...infer R] ? [Serialize, ...Serialize] : T extends Array ? Array> : T extends readonly unknown[] ? readonly Serialize[] : T extends Record ? { [K in keyof T]: Serialize } : undefined; type VoidToUndefined = Equal extends true ? undefined : T; type DataFrom = IsAny extends true ? undefined : T extends Func ? VoidToUndefined>> : undefined; type ClientData = T extends Response ? never : T extends DataWithResponseInit ? U : T; type ServerData = T extends Response ? never : T extends DataWithResponseInit ? Serialize : Serialize; type ServerDataFrom = ServerData>; type ClientDataFrom = ClientData>; type ClientDataFunctionArgs = { /** * A {@link https://developer.mozilla.org/en-US/docs/Web/API/Request Fetch Request instance} which you can use to read the URL, the method, the "content-type" header, and the request body from the request. * * @note Because client data functions are called before a network request is made, the Request object does not include the headers which the browser automatically adds. React Router infers the "content-type" header from the enc-type of the form that performed the submission. **/ request: Request; /** * A URL instance representing the application location being navigated to or * fetched. * * In Framework mode, this is a normalized URL with React-Router-specific * implementation details removed (`.data` suffixes, `index`/`_routes` search * params). For the raw incoming URL, use `request.url`. */ url: URL; /** * {@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 clientLoader({ * params, * }: Route.ClientLoaderArgs) { * params.teamId; * // ^ string * } **/ params: Params; /** * Matched un-interpolated route pattern for the current path (i.e., /blog/:slug). * Mostly useful as a identifier to aggregate on for logging/tracing/etc. */ pattern: string; /** * An instance of `RouterContextProvider` that can be used to access context * values from your route middlewares. You may pass in initial context values * in your `` prop. */ context: Readonly; }; type ServerDataFunctionArgs = { /** A {@link https://developer.mozilla.org/en-US/docs/Web/API/Request Fetch Request instance} which you can use to read the url, method, headers (such as cookies), and request body from the request. */request: Request; /** * A URL instance representing the application location being navigated to or * fetched. * * In Framework mode, this is a normalized URL with React-Router-specific * implementation details removed (`.data` suffixes, `index`/`_routes` search * params). For the raw incoming URL, use `request.url`. */ url: URL; /** * {@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 loader({ * params, * }: Route.LoaderArgs) { * params.teamId; * // ^ string * } **/ params: Params; /** * Matched un-interpolated route pattern for the current path (i.e., /blog/:slug). * Mostly useful as a identifier to aggregate on for logging/tracing/etc. */ pattern: string; /** * An instance of `RouterContextProvider` that can be used for type-safe * access to context values set in your route middlewares. If you are using * a custom server adapter, you may provide an initial set of context values * from your `getLoadContext` function. */ context: Readonly; }; type SerializeFrom = T extends ((...args: infer Args) => unknown) ? Args extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs | ClientDataFunctionArgs] ? ClientDataFrom : ServerDataFrom : T; type IsDefined = Equal extends true ? false : true; type IsHydrate = ClientLoader extends { hydrate: true; } ? true : ClientLoader extends { hydrate: false; } ? false : false; type GetLoaderData = _DataLoaderData, ClientDataFrom, IsHydrate, T extends { HydrateFallback: Func; } ? true : false>; type _DataLoaderData = [HasHydrateFallback, ClientLoaderHydrate] extends [true, true] ? IsDefined extends true ? ClientLoaderData : undefined : [IsDefined, IsDefined] extends [true, true] ? ServerLoaderData | ClientLoaderData : IsDefined extends true ? ClientLoaderData : IsDefined extends true ? ServerLoaderData : undefined; type GetActionData = _DataActionData, ClientDataFrom>; type _DataActionData = Awaited<[IsDefined, IsDefined] extends [true, true] ? ServerActionData | ClientActionData : IsDefined extends true ? ClientActionData : IsDefined extends true ? ServerActionData : undefined>; //#endregion export { ClientDataFunctionArgs, GetActionData, GetLoaderData, SerializeFrom, ServerDataFrom, ServerDataFunctionArgs };