INDIE UNPKG

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