UNPKG

8.08 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 errors: Record<string, any> | null;
69 loaderData: Record<string, any>;
70 location: Location;
71 routeDiscovery: RouteDiscovery;
72 matches: RSCRouteMatch[];
73 patches?: Promise<RSCRouteManifest[]>;
74 nonce?: string;
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 * @example
120 * import {
121 * createTemporaryReferenceSet,
122 * decodeAction,
123 * decodeReply,
124 * loadServerAction,
125 * renderToReadableStream,
126 * } from "@vitejs/plugin-rsc/rsc";
127 * import { unstable_matchRSCServerRequest as matchRSCServerRequest } from "react-router";
128 *
129 * matchRSCServerRequest({
130 * createTemporaryReferenceSet,
131 * decodeAction,
132 * decodeFormState,
133 * decodeReply,
134 * loadServerAction,
135 * request,
136 * routes: routes(),
137 * generateResponse(match) {
138 * return new Response(
139 * renderToReadableStream(match.payload),
140 * {
141 * status: match.statusCode,
142 * headers: match.headers,
143 * }
144 * );
145 * },
146 * });
147 *
148 * @name unstable_matchRSCServerRequest
149 * @public
150 * @category RSC
151 * @mode data
152 * @param opts Options
153 * @param opts.allowedActionOrigins Origin patterns that are allowed to execute actions.
154 * @param opts.basename The basename to use when matching the request.
155 * @param opts.createTemporaryReferenceSet A function that returns a temporary
156 * reference set for the request, used to track temporary references in the [RSC](https://react.dev/reference/rsc/server-components)
157 * stream.
158 * @param opts.decodeAction Your `react-server-dom-xyz/server`'s `decodeAction`
159 * function, responsible for loading a server action.
160 * @param opts.decodeFormState A function responsible for decoding form state for
161 * progressively enhanceable forms with React's [`useActionState`](https://react.dev/reference/react/useActionState)
162 * using your `react-server-dom-xyz/server`'s `decodeFormState`.
163 * @param opts.decodeReply Your `react-server-dom-xyz/server`'s `decodeReply`
164 * function, used to decode the server function's arguments and bind them to the
165 * implementation for invocation by the router.
166 * @param opts.generateResponse A function responsible for using your
167 * `renderToReadableStream` to generate a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
168 * encoding the {@link unstable_RSCPayload}.
169 * @param opts.loadServerAction Your `react-server-dom-xyz/server`'s
170 * `loadServerAction` function, used to load a server action by ID.
171 * @param opts.onError An optional error handler that will be called with any
172 * errors that occur during the request processing.
173 * @param opts.request The [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
174 * to match against.
175 * @param opts.requestContext An instance of {@link RouterContextProvider}
176 * that should be created per request, to be passed to [`action`](../../start/data/route-object#action)s,
177 * [`loader`](../../start/data/route-object#loader)s and [middleware](../../how-to/middleware).
178 * @param opts.routeDiscovery The route discovery configuration, used to determine how the router should discover new routes during navigations.
179 * @param opts.routes Your {@link unstable_RSCRouteConfigEntry | route definitions}.
180 * @returns A [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
181 * that contains the [RSC](https://react.dev/reference/rsc/server-components)
182 * data for hydration.
183 */
184declare function matchRSCServerRequest({
185 allowedActionOrigins,
186 createTemporaryReferenceSet,
187 basename,
188 decodeReply,
189 requestContext,
190 routeDiscovery,
191 loadServerAction,
192 decodeAction,
193 decodeFormState,
194 onError,
195 request,
196 routes,
197 generateResponse
198}: {
199 allowedActionOrigins?: string[];
200 createTemporaryReferenceSet: () => unknown;
201 basename?: string;
202 decodeReply?: DecodeReplyFunction;
203 decodeAction?: DecodeActionFunction;
204 decodeFormState?: DecodeFormStateFunction;
205 requestContext?: RouterContextProvider;
206 loadServerAction?: LoadServerActionFunction;
207 onError?: (error: unknown) => void;
208 request: Request;
209 routes: RSCRouteConfigEntry[];
210 routeDiscovery?: RouteDiscovery;
211 generateResponse: (match: RSCMatch, {
212 onError,
213 temporaryReferences
214 }: {
215 onError(error: unknown): string | undefined;
216 temporaryReferences: unknown;
217 }) => Response;
218}): Promise<Response>;
219//#endregion
220export { DecodeActionFunction, DecodeFormStateFunction, DecodeReplyFunction, LoadServerActionFunction, RSCManifestPayload, RSCMatch, RSCPayload, RSCRenderPayload, RSCRouteConfig, RSCRouteConfigEntry, RSCRouteManifest, RSCRouteMatch, getRequest, matchRSCServerRequest };
\No newline at end of file