UNPKG

8.69 kBTypeScriptView Raw
1
2import { Location } from "../router/history.js";
3import { ActionFunction, LoaderFunction, Params, RouterContextProvider, ShouldRevalidateFunction } from "../router/utils.js";
4import { ClientActionFunction, ClientLoaderFunction, HeadersFunction, LinksFunction, MetaFunction } from "../dom/ssr/routeModules.js";
5import * as React$1 from "react";
6import { ReactFormState } from "react-dom/client";
7
8//#region lib/rsc/server.rsc.d.ts
9declare function getRequest(): Request;
10type RSCRouteConfigEntryBase = {
11 action?: ActionFunction;
12 clientAction?: ClientActionFunction;
13 clientLoader?: ClientLoaderFunction;
14 ErrorBoundary?: React$1.ComponentType<any>;
15 handle?: any;
16 headers?: HeadersFunction;
17 HydrateFallback?: React$1.ComponentType<any>;
18 Layout?: React$1.ComponentType<any>;
19 links?: LinksFunction;
20 loader?: LoaderFunction;
21 meta?: MetaFunction;
22 shouldRevalidate?: ShouldRevalidateFunction;
23};
24type RSCRouteConfigEntry = RSCRouteConfigEntryBase & {
25 id: string;
26 path?: string;
27 Component?: React$1.ComponentType<any>;
28 lazy?: () => Promise<RSCRouteConfigEntryBase & ({
29 default?: React$1.ComponentType<any>;
30 Component?: never;
31 } | {
32 default?: never;
33 Component?: React$1.ComponentType<any>;
34 })>;
35} & ({
36 index: true;
37} | {
38 children?: RSCRouteConfigEntry[];
39});
40type RSCRouteConfig = Array<RSCRouteConfigEntry>;
41type RSCRouteManifest = {
42 clientAction?: ClientActionFunction;
43 clientLoader?: ClientLoaderFunction;
44 element?: React$1.ReactElement | false;
45 errorElement?: React$1.ReactElement;
46 handle?: any;
47 hasAction: boolean;
48 hasComponent: boolean;
49 hasLoader: boolean;
50 hydrateFallbackElement?: React$1.ReactElement;
51 id: string;
52 index?: boolean;
53 links?: LinksFunction;
54 meta?: MetaFunction;
55 parentId?: string;
56 path?: string;
57 shouldRevalidate?: ShouldRevalidateFunction;
58};
59type RSCRouteMatch = RSCRouteManifest & {
60 params: Params;
61 pathname: string;
62 pathnameBase: string;
63};
64type RSCRenderPayload = {
65 type: "render";
66 actionData: Record<string, any> | null;
67 basename: string | undefined;
68 clientVersion?: string;
69 errors: Record<string, any> | null;
70 loaderData: Record<string, any>;
71 location: Location;
72 routeDiscovery: RouteDiscovery;
73 matches: RSCRouteMatch[];
74 patches?: Promise<RSCRouteManifest[]>;
75 formState?: ReactFormState;
76};
77type RSCManifestPayload = {
78 type: "manifest";
79 patches: Promise<RSCRouteManifest[]>;
80};
81type RSCActionPayload = {
82 type: "action";
83 actionResult: Promise<unknown>;
84 rerender?: Promise<RSCRenderPayload | RSCRedirectPayload>;
85};
86type RSCRedirectPayload = {
87 type: "redirect";
88 status: number;
89 location: string;
90 replace: boolean;
91 reload: boolean;
92 actionResult?: Promise<unknown>;
93};
94type RSCPayload = RSCRenderPayload | RSCManifestPayload | RSCActionPayload | RSCRedirectPayload;
95type RSCMatch = {
96 statusCode: number;
97 headers: Headers;
98 payload: RSCPayload;
99};
100type DecodeActionFunction = (formData: FormData) => Promise<() => Promise<unknown>>;
101type DecodeFormStateFunction = (result: unknown, formData: FormData) => Promise<ReactFormState | undefined>;
102type DecodeReplyFunction = (reply: FormData | string, options: {
103 temporaryReferences: unknown;
104}) => Promise<unknown[]>;
105type LoadServerActionFunction = (id: string) => Promise<Function>;
106type RouteDiscovery = {
107 mode: "lazy";
108 manifestPath?: string | undefined;
109} | {
110 mode: "initial";
111};
112/**
113 * Matches the given routes to a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
114 * and returns an [RSC](https://react.dev/reference/rsc/server-components)
115 * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
116 * encoding an {@link unstable_RSCPayload} for consumption by an [RSC](https://react.dev/reference/rsc/server-components)
117 * enabled client router.
118 *
119 * Treat every React Server Function as a public endpoint. Server Functions are
120 * not inherently associated with a route. Any route middleware that runs is
121 * selected from the URL used to call a Server Function, but the client controls
122 * both the Server Function identifier and the URL. Perform all access control
123 * checks within each Server Function, or use a route action for
124 * middleware-driven access control.
125 *
126 * @example
127 * import {
128 * createTemporaryReferenceSet,
129 * decodeAction,
130 * decodeReply,
131 * loadServerAction,
132 * renderToReadableStream,
133 * } from "@vitejs/plugin-rsc/rsc";
134 * import { unstable_matchRSCServerRequest as matchRSCServerRequest } from "react-router";
135 *
136 * matchRSCServerRequest({
137 * createTemporaryReferenceSet,
138 * decodeAction,
139 * decodeFormState,
140 * decodeReply,
141 * loadServerAction,
142 * request,
143 * routes: routes(),
144 * generateResponse(match) {
145 * return new Response(
146 * renderToReadableStream(match.payload),
147 * {
148 * status: match.statusCode,
149 * headers: match.headers,
150 * }
151 * );
152 * },
153 * });
154 *
155 * @name unstable_matchRSCServerRequest
156 * @public
157 * @category RSC
158 * @mode data
159 * @param opts Options
160 * @param opts.allowedActionOrigins Origin patterns that are allowed to execute actions.
161 * @param opts.basename The basename to use when matching the request.
162 * @param opts.createTemporaryReferenceSet A function that returns a temporary
163 * reference set for the request, used to track temporary references in the [RSC](https://react.dev/reference/rsc/server-components)
164 * stream.
165 * @param opts.decodeAction Your `react-server-dom-xyz/server`'s `decodeAction`
166 * function, responsible for loading a server action.
167 * @param opts.decodeFormState A function responsible for decoding form state for
168 * progressively enhanceable forms with React's [`useActionState`](https://react.dev/reference/react/useActionState)
169 * using your `react-server-dom-xyz/server`'s `decodeFormState`.
170 * @param opts.decodeReply Your `react-server-dom-xyz/server`'s `decodeReply`
171 * function, used to decode the server function's arguments and bind them to the
172 * implementation for invocation by the router.
173 * @param opts.generateResponse A function responsible for using your
174 * `renderToReadableStream` to generate a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
175 * encoding the {@link unstable_RSCPayload}.
176 * @param opts.loadServerAction Your `react-server-dom-xyz/server`'s
177 * `loadServerAction` function, used to load a server action by ID.
178 * @param opts.clientVersion A version derived from the client build output used
179 * to detect stale clients during lazy route discovery.
180 * @param opts.onError An optional error handler that will be called with any
181 * errors that occur during the request processing.
182 * @param opts.request The [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
183 * to match against.
184 * @param opts.requestContext An instance of {@link RouterContextProvider}
185 * that should be created per request, to be passed to [`action`](../../start/data/route-object#action)s,
186 * [`loader`](../../start/data/route-object#loader)s and [middleware](../../how-to/middleware).
187 * @param opts.routeDiscovery The route discovery configuration, used to determine how the router should discover new routes during navigations.
188 * @param opts.routes Your {@link unstable_RSCRouteConfigEntry | route definitions}.
189 * @returns A [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
190 * that contains the [RSC](https://react.dev/reference/rsc/server-components)
191 * data for hydration.
192 */
193declare function matchRSCServerRequest({
194 allowedActionOrigins,
195 createTemporaryReferenceSet,
196 basename,
197 decodeReply,
198 requestContext,
199 routeDiscovery,
200 loadServerAction,
201 decodeAction,
202 decodeFormState,
203 clientVersion,
204 onError,
205 request,
206 routes,
207 generateResponse
208}: {
209 allowedActionOrigins?: string[];
210 createTemporaryReferenceSet: () => unknown;
211 basename?: string;
212 decodeReply?: DecodeReplyFunction;
213 decodeAction?: DecodeActionFunction;
214 decodeFormState?: DecodeFormStateFunction;
215 requestContext?: RouterContextProvider;
216 loadServerAction?: LoadServerActionFunction;
217 clientVersion?: string;
218 onError?: (error: unknown) => void;
219 request: Request;
220 routes: RSCRouteConfigEntry[];
221 routeDiscovery?: RouteDiscovery;
222 generateResponse: (match: RSCMatch, {
223 onError,
224 temporaryReferences
225 }: {
226 onError(error: unknown): string | undefined;
227 temporaryReferences: unknown;
228 }) => Response;
229}): Promise<Response>;
230//#endregion
231export { DecodeActionFunction, DecodeFormStateFunction, DecodeReplyFunction, LoadServerActionFunction, RSCManifestPayload, RSCMatch, RSCPayload, RSCRenderPayload, RSCRouteConfig, RSCRouteConfigEntry, RSCRouteManifest, RSCRouteMatch, getRequest, matchRSCServerRequest };
\No newline at end of file