UNPKG

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