Skip to content

Commit 423a339

Browse files
committed
Dedup Loader/Action function types and remove AppData
1 parent 4a84f79 commit 423a339

File tree

6 files changed

+24
-132
lines changed

6 files changed

+24
-132
lines changed

packages/react-router/lib/dom/ssr/data.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import "../global";
22

3-
/**
4-
* Data for a route that was returned from a `loader()`.
5-
*/
6-
export type AppData = unknown;
7-
83
export async function createRequestInit(
94
request: Request
105
): Promise<RequestInit> {

packages/react-router/lib/dom/ssr/routeModules.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import type { ComponentType, ReactElement } from "react";
22
import type { Location } from "../../router/history";
33
import type {
4-
ActionFunction as RRActionFunction,
5-
ActionFunctionArgs as RRActionFunctionArgs,
6-
LoaderFunction as RRLoaderFunction,
7-
LoaderFunctionArgs as RRLoaderFunctionArgs,
4+
ActionFunction,
5+
ActionFunctionArgs,
6+
LoaderFunction,
7+
LoaderFunctionArgs,
88
Params,
99
ShouldRevalidateFunction,
10-
LoaderFunctionArgs,
1110
} from "../../router/utils";
1211

1312
import type { SerializeFrom } from "./components";
14-
import type { AppData } from "./data";
1513
import type { EntryRoute } from "./routes";
1614
import type { DataRouteMatch } from "../../context";
1715
import type { LinkDescriptor } from "../../router/links";
@@ -38,29 +36,29 @@ export interface RouteModule {
3836
*/
3937
export type ClientActionFunction = (
4038
args: ClientActionFunctionArgs
41-
) => ReturnType<RRActionFunction>;
39+
) => ReturnType<ActionFunction>;
4240

4341
/**
4442
* Arguments passed to a route `clientAction` function
4543
*/
46-
export type ClientActionFunctionArgs = RRActionFunctionArgs<undefined> & {
47-
serverAction: <T = AppData>() => Promise<SerializeFrom<T>>;
44+
export type ClientActionFunctionArgs = ActionFunctionArgs<undefined> & {
45+
serverAction: <T = unknown>() => Promise<SerializeFrom<T>>;
4846
};
4947

5048
/**
5149
* A function that loads data for a route on the client
5250
*/
5351
export type ClientLoaderFunction = ((
5452
args: ClientLoaderFunctionArgs
55-
) => ReturnType<RRLoaderFunction>) & {
53+
) => ReturnType<LoaderFunction>) & {
5654
hydrate?: boolean;
5755
};
5856

5957
/**
6058
* Arguments passed to a route `clientLoader` function
6159
*/
62-
export type ClientLoaderFunctionArgs = RRLoaderFunctionArgs<undefined> & {
63-
serverLoader: <T = AppData>() => Promise<SerializeFrom<T>>;
60+
export type ClientLoaderFunctionArgs = LoaderFunctionArgs<undefined> & {
61+
serverLoader: <T = unknown>() => Promise<SerializeFrom<T>>;
6462
};
6563

6664
/**
@@ -96,19 +94,6 @@ export interface LinksFunction {
9694
(): LinkDescriptor[];
9795
}
9896

99-
// Loose copy from @react-router/server-runtime to avoid circular imports
100-
type LoaderFunction = (
101-
args: LoaderFunctionArgs & {
102-
// Context is always provided in Remix, and typed for module augmentation support.
103-
context: unknown;
104-
// TODO: (v7) Make this non-optional once single-fetch is the default
105-
response?: {
106-
status: number | undefined;
107-
headers: Headers;
108-
};
109-
}
110-
) => ReturnType<RRLoaderFunction>;
111-
11297
export interface MetaMatch<
11398
RouteId extends string = string,
11499
Loader extends LoaderFunction | unknown = unknown
@@ -144,7 +129,7 @@ export interface MetaArgs<
144129
>
145130
> {
146131
data:
147-
| (Loader extends LoaderFunction ? SerializeFrom<Loader> : AppData)
132+
| (Loader extends LoaderFunction ? SerializeFrom<Loader> : unknown)
148133
| undefined;
149134
params: Params;
150135
location: Location;

packages/react-router/lib/server-runtime/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ActionFunctionArgs, LoaderFunctionArgs } from "./routeModules";
1+
import type { ActionFunctionArgs, LoaderFunctionArgs } from "../router/utils";
22
import type {
33
AssetsManifest,
44
EntryContext,

packages/react-router/lib/server-runtime/data.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { isDataWithResponseInit, isRedirectStatusCode } from "../router/router";
21
import type {
3-
ActionFunction,
4-
ActionFunctionArgs,
52
LoaderFunction,
3+
ActionFunction,
64
LoaderFunctionArgs,
7-
} from "./routeModules";
5+
ActionFunctionArgs,
6+
} from "../router/utils";
7+
import { isDataWithResponseInit, isRedirectStatusCode } from "../router/router";
88

99
/**
1010
* An object of unknown type for route loaders and actions provided by the
@@ -16,11 +16,6 @@ export interface AppLoadContext {
1616
[key: string]: unknown;
1717
}
1818

19-
/**
20-
* Data for a route that was returned from a `loader()`.
21-
*/
22-
export type AppData = unknown;
23-
2419
// Need to use RR's version here to permit the optional context even
2520
// though we know it'll always be provided in remix
2621
export async function callRouteHandler(

packages/react-router/lib/server-runtime/routeModules.ts

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,15 @@
1-
import type { Location } from "../router/history";
1+
import type { ActionFunction, LoaderFunction } from "../router/utils";
22
import type {
3-
ActionFunction as RRActionFunction,
4-
ActionFunctionArgs as RRActionFunctionArgs,
5-
AgnosticRouteMatch,
6-
LoaderFunction as RRLoaderFunction,
7-
LoaderFunctionArgs as RRLoaderFunctionArgs,
8-
Params,
9-
} from "../router/utils";
10-
import type { AppData, AppLoadContext } from "./data";
11-
import type { SerializeFrom } from "../dom/ssr/components";
12-
import type { LinkDescriptor } from "../router/links";
3+
ClientActionFunction,
4+
ClientLoaderFunction,
5+
LinksFunction,
6+
MetaFunction,
7+
} from "../dom/ssr/routeModules";
138

149
export interface RouteModules<RouteModule> {
1510
[routeId: string]: RouteModule | undefined;
1611
}
1712

18-
/**
19-
* @deprecated Use `LoaderFunctionArgs`/`ActionFunctionArgs` instead
20-
*/
21-
export type DataFunctionArgs = RRActionFunctionArgs<AppLoadContext> &
22-
RRLoaderFunctionArgs<AppLoadContext> & {
23-
// Context is always provided in Remix, and typed for module augmentation support.
24-
// RR also doesn't export DataFunctionArgs, so we extend the two interfaces here
25-
// even tough they're identical under the hood
26-
context: AppLoadContext;
27-
};
28-
29-
/**
30-
* A function that handles data mutations for a route on the server
31-
*/
32-
export type ActionFunction = (
33-
args: ActionFunctionArgs
34-
) => ReturnType<RRActionFunction>;
35-
36-
/**
37-
* Arguments passed to a route `action` function
38-
*/
39-
export type ActionFunctionArgs = RRActionFunctionArgs<AppLoadContext> & {
40-
// Context is always provided in Remix, and typed for module augmentation support.
41-
context: AppLoadContext;
42-
};
43-
44-
/**
45-
* A function that handles data mutations for a route on the client
46-
* @private Public API is exported from @react-router/react
47-
*/
48-
type ClientActionFunction = (
49-
args: ClientActionFunctionArgs
50-
) => ReturnType<RRActionFunction>;
51-
52-
/**
53-
* Arguments passed to a route `clientAction` function
54-
* @private Public API is exported from @react-router/react
55-
*/
56-
export type ClientActionFunctionArgs = RRActionFunctionArgs<undefined> & {
57-
serverAction: <T = AppData>() => Promise<SerializeFrom<T>>;
58-
};
59-
60-
/**
61-
* A function that loads data for a route on the server
62-
*/
63-
export type LoaderFunction = (
64-
args: LoaderFunctionArgs
65-
) => ReturnType<RRLoaderFunction>;
66-
67-
/**
68-
* Arguments passed to a route `loader` function
69-
*/
70-
export type LoaderFunctionArgs = RRLoaderFunctionArgs<AppLoadContext> & {
71-
// Context is always provided in Remix, and typed for module augmentation support.
72-
context: AppLoadContext;
73-
};
74-
75-
/**
76-
* A function that loads data for a route on the client
77-
* @private Public API is exported from @react-router/react
78-
*/
79-
type ClientLoaderFunction = ((
80-
args: ClientLoaderFunctionArgs
81-
) => ReturnType<RRLoaderFunction>) & {
82-
hydrate?: boolean;
83-
};
84-
85-
/**
86-
* Arguments passed to a route `clientLoader` function
87-
* @private Public API is exported from @react-router/react
88-
*/
89-
export type ClientLoaderFunctionArgs = RRLoaderFunctionArgs<undefined> & {
90-
serverLoader: <T = AppData>() => Promise<SerializeFrom<T>>;
91-
};
92-
9313
export type HeadersArgs = {
9414
loaderHeaders: Headers;
9515
parentHeaders: Headers;

packages/react-router/lib/server-runtime/routes.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,13 @@ export function createStaticHandlerDataRoutes(
100100
invariant("data" in result, "Unable to process prerendered data");
101101
return result.data;
102102
}
103-
let val = await callRouteHandler(
104-
route.module.loader!,
105-
args as LoaderFunctionArgs
106-
);
103+
let val = await callRouteHandler(route.module.loader!, args);
107104
return val;
108105
}
109106
: undefined,
110107
action: route.module.action
111108
? (args: RRActionFunctionArgs) =>
112-
callRouteHandler(route.module.action!, args as ActionFunctionArgs)
109+
callRouteHandler(route.module.action!, args)
113110
: undefined,
114111
handle: route.module.handle,
115112
};

0 commit comments

Comments
 (0)