UNPKG

10.5 kBTypeScriptView Raw
1import { aE as LinkDescriptor, aB as MetaDescriptor, aT as ServerDataFrom, aU as ClientDataFrom, aV as Func, aW as unstable_MiddlewareNextFunction, E as Equal, u as unstable_RouterContextProvider, aX as Pretty } from '../../route-data-CGHGzi13.js';
2import { M as MiddlewareEnabled, A as AppLoadContext } from '../../future-ldDp5FKH.js';
3import 'react';
4
5type IsDefined<T> = Equal<T, undefined> extends true ? false : true;
6type MaybePromise<T> = T | Promise<T>;
7type RouteModule = {
8 meta?: Func;
9 links?: Func;
10 headers?: Func;
11 loader?: Func;
12 clientLoader?: Func;
13 action?: Func;
14 clientAction?: Func;
15 HydrateFallback?: unknown;
16 default?: unknown;
17 ErrorBoundary?: unknown;
18 [key: string]: unknown;
19};
20type LinkDescriptors = LinkDescriptor[];
21type RouteInfo = {
22 parents: RouteInfo[];
23 module: RouteModule;
24 id: unknown;
25 file: string;
26 path: string;
27 params: unknown;
28 loaderData: unknown;
29 actionData: unknown;
30};
31type MetaMatch<T extends RouteInfo> = Pretty<Pick<T, "id" | "params"> & {
32 pathname: string;
33 meta: MetaDescriptor[];
34 data: T["loaderData"];
35 handle?: unknown;
36 error?: unknown;
37}>;
38type MetaMatches<T extends RouteInfo[]> = T extends [infer F extends RouteInfo, ...infer R extends RouteInfo[]] ? [MetaMatch<F>, ...MetaMatches<R>] : Array<MetaMatch<RouteInfo> | undefined>;
39type CreateMetaArgs<T extends RouteInfo> = {
40 /** This is the current router `Location` object. This is useful for generating tags for routes at specific paths or query parameters. */
41 location: Location;
42 /** {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route. */
43 params: T["params"];
44 /** The return value for this route's server loader function */
45 data: T["loaderData"];
46 /** Thrown errors that trigger error boundaries will be passed to the meta function. This is useful for generating metadata for error pages. */
47 error?: unknown;
48 /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react_router.UIMatch.html route matches}, including parent route matches. */
49 matches: MetaMatches<[...T["parents"], T]>;
50};
51type MetaDescriptors = MetaDescriptor[];
52type HeadersArgs = {
53 loaderHeaders: Headers;
54 parentHeaders: Headers;
55 actionHeaders: Headers;
56 errorHeaders: Headers | undefined;
57};
58type IsHydrate<ClientLoader> = ClientLoader extends {
59 hydrate: true;
60} ? true : ClientLoader extends {
61 hydrate: false;
62} ? false : false;
63type CreateLoaderData<T extends RouteModule> = _CreateLoaderData<ServerDataFrom<T["loader"]>, ClientDataFrom<T["clientLoader"]>, IsHydrate<T["clientLoader"]>, T extends {
64 HydrateFallback: Func;
65} ? true : false>;
66type _CreateLoaderData<ServerLoaderData, ClientLoaderData, ClientLoaderHydrate extends boolean, HasHydrateFallback> = [
67 HasHydrateFallback,
68 ClientLoaderHydrate
69] extends [true, true] ? IsDefined<ClientLoaderData> extends true ? ClientLoaderData : undefined : [
70 IsDefined<ClientLoaderData>,
71 IsDefined<ServerLoaderData>
72] extends [true, true] ? ServerLoaderData | ClientLoaderData : IsDefined<ClientLoaderData> extends true ? ClientLoaderData : IsDefined<ServerLoaderData> extends true ? ServerLoaderData : undefined;
73type CreateActionData<T extends RouteModule> = _CreateActionData<ServerDataFrom<T["action"]>, ClientDataFrom<T["clientAction"]>>;
74type _CreateActionData<ServerActionData, ClientActionData> = Awaited<[
75 IsDefined<ServerActionData>,
76 IsDefined<ClientActionData>
77] extends [true, true] ? ServerActionData | ClientActionData : IsDefined<ClientActionData> extends true ? ClientActionData : IsDefined<ServerActionData> extends true ? ServerActionData : undefined>;
78type ClientDataFunctionArgs<T extends RouteInfo> = {
79 /**
80 * 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.
81 *
82 * @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.
83 **/
84 request: Request;
85 /**
86 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
87 * @example
88 * // app/routes.ts
89 * route("teams/:teamId", "./team.tsx"),
90 *
91 * // app/team.tsx
92 * export function clientLoader({
93 * params,
94 * }: Route.ClientLoaderArgs) {
95 * params.teamId;
96 * // ^ string
97 * }
98 **/
99 params: T["params"];
100 /**
101 * When `future.unstable_middleware` is not enabled, this is undefined.
102 *
103 * When `future.unstable_middleware` is enabled, this is an instance of
104 * `unstable_RouterContextProvider` and can be used to access context values
105 * from your route middlewares. You may pass in initial context values in your
106 * `<HydratedRouter unstable_getContext>` prop
107 */
108 context: unstable_RouterContextProvider;
109};
110type ServerDataFunctionArgs<T extends RouteInfo> = {
111 /** 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. */
112 request: Request;
113 /**
114 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
115 * @example
116 * // app/routes.ts
117 * route("teams/:teamId", "./team.tsx"),
118 *
119 * // app/team.tsx
120 * export function loader({
121 * params,
122 * }: Route.LoaderArgs) {
123 * params.teamId;
124 * // ^ string
125 * }
126 **/
127 params: T["params"];
128 /**
129 * Without `future.unstable_middleware` enabled, this is the context passed in
130 * to your server adapter's `getLoadContext` function. It's a way to bridge the
131 * gap between the adapter's request/response API with your React Router app.
132 * It is only applicable if you are using a custom server adapter.
133 *
134 * With `future.unstable_middleware` enabled, this is an instance of
135 * `unstable_RouterContextProvider` and can be used for type-safe access to
136 * context value set in your route middlewares. If you are using a custom
137 * server adapter, you may provide an initial set of context values from your
138 * `getLoadContext` function.
139 */
140 context: MiddlewareEnabled extends true ? unstable_RouterContextProvider : AppLoadContext;
141};
142type CreateServerMiddlewareFunction<T extends RouteInfo> = (args: ServerDataFunctionArgs<T>, next: unstable_MiddlewareNextFunction<Response>) => MaybePromise<Response | undefined>;
143type CreateClientMiddlewareFunction<T extends RouteInfo> = (args: ClientDataFunctionArgs<T>, next: unstable_MiddlewareNextFunction<undefined>) => MaybePromise<undefined>;
144type CreateServerLoaderArgs<T extends RouteInfo> = ServerDataFunctionArgs<T>;
145type CreateClientLoaderArgs<T extends RouteInfo> = ClientDataFunctionArgs<T> & {
146 /** 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). */
147 serverLoader: () => Promise<ServerDataFrom<T["module"]["loader"]>>;
148};
149type CreateServerActionArgs<T extends RouteInfo> = ServerDataFunctionArgs<T>;
150type CreateClientActionArgs<T extends RouteInfo> = ClientDataFunctionArgs<T> & {
151 /** 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. */
152 serverAction: () => Promise<ServerDataFrom<T["module"]["action"]>>;
153};
154type CreateHydrateFallbackProps<T extends RouteInfo> = {
155 params: T["params"];
156 loaderData?: T["loaderData"];
157 actionData?: T["actionData"];
158};
159type Match<T extends RouteInfo> = Pretty<Pick<T, "id" | "params"> & {
160 pathname: string;
161 data: T["loaderData"];
162 handle: unknown;
163}>;
164type Matches<T extends RouteInfo[]> = T extends [infer F extends RouteInfo, ...infer R extends RouteInfo[]] ? [Match<F>, ...Matches<R>] : Array<Match<RouteInfo> | undefined>;
165type CreateComponentProps<T extends RouteInfo> = {
166 /**
167 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
168 * @example
169 * // app/routes.ts
170 * route("teams/:teamId", "./team.tsx"),
171 *
172 * // app/team.tsx
173 * export default function Component({
174 * params,
175 * }: Route.ComponentProps) {
176 * params.teamId;
177 * // ^ string
178 * }
179 **/
180 params: T["params"];
181 /** The data returned from the `loader` or `clientLoader` */
182 loaderData: T["loaderData"];
183 /** The data returned from the `action` or `clientAction` following an action submission. */
184 actionData?: T["actionData"];
185 /** An array of the current {@link https://api.reactrouter.com/v7/interfaces/react_router.UIMatch.html route matches}, including parent route matches. */
186 matches: Matches<[...T["parents"], T]>;
187};
188type CreateErrorBoundaryProps<T extends RouteInfo> = {
189 /**
190 * {@link https://reactrouter.com/start/framework/routing#dynamic-segments Dynamic route params} for the current route.
191 * @example
192 * // app/routes.ts
193 * route("teams/:teamId", "./team.tsx"),
194 *
195 * // app/team.tsx
196 * export function ErrorBoundary({
197 * params,
198 * }: Route.ErrorBoundaryProps) {
199 * params.teamId;
200 * // ^ string
201 * }
202 **/
203 params: T["params"];
204 error: unknown;
205 loaderData?: T["loaderData"];
206 actionData?: T["actionData"];
207};
208
209export type { CreateActionData, CreateClientActionArgs, CreateClientLoaderArgs, CreateClientMiddlewareFunction, CreateComponentProps, CreateErrorBoundaryProps, CreateHydrateFallbackProps, CreateLoaderData, CreateMetaArgs, CreateServerActionArgs, CreateServerLoaderArgs, CreateServerMiddlewareFunction, HeadersArgs, LinkDescriptors, MetaDescriptors };