UNPKG

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