Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate DataFunctionArgs #8089

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/data-function-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@remix-run/server-runtime": minor
"@remix-run/server-node": minor
"@remix-run/server-cloudflare": minor
"@remix-run/server-deno": minor
---

Deprecate `DataFunctionArgs` in favor of `LoaderFunctionArgs`/`ActionFunctionArgs`. This is aimed at keeping the types aligned across server/client loaders/actions now that we have `serverLoader`/`serverAction` parameters which differentiate `ClientLoaderFunctionArgs`/`ClientActionFunctionArgs`.
12 changes: 10 additions & 2 deletions docs/file-conventions/entry.server.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ You can export an optional `handleDataRequest` function that will allow you to m
```tsx
export function handleDataRequest(
response: Response,
{ request, params, context }: DataFunctionArgs
{
request,
params,
context,
}: LoaderFunctionArgs | ActionFunctionArgs
) {
response.headers.set("X-Custom-Header", "value");
return response;
Expand All @@ -30,7 +34,11 @@ By default, Remix will log encountered server-side errors to the console. If you
```tsx
export function handleError(
error: unknown,
{ request, params, context }: DataFunctionArgs
{
request,
params,
context,
}: LoaderFunctionArgs | ActionFunctionArgs
) {
if (!request.signal.aborted) {
sendErrorToErrorReportingService(error);
Expand Down
8 changes: 5 additions & 3 deletions packages/remix-server-runtime/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DataFunctionArgs } from "./routeModules";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "./routeModules";
import type { AssetsManifest, EntryContext, FutureConfig } from "./entry";
import type { ServerRouteManifest } from "./routes";
import type { AppLoadContext } from "./data";
Expand Down Expand Up @@ -29,11 +29,13 @@ export interface HandleDocumentRequestFunction {
}

export interface HandleDataRequestFunction {
(response: Response, args: DataFunctionArgs): Promise<Response> | Response;
(response: Response, args: LoaderFunctionArgs | ActionFunctionArgs):
| Promise<Response>
| Response;
}

export interface HandleErrorFunction {
(error: unknown, args: DataFunctionArgs): void;
(error: unknown, args: LoaderFunctionArgs | ActionFunctionArgs): void;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
} from "./responses";
import type {
ActionFunction,
DataFunctionArgs,
ActionFunctionArgs,
LoaderFunction,
LoaderFunctionArgs,
} from "./routeModules";

/**
Expand All @@ -35,7 +36,7 @@ export async function callRouteActionRR({
}: {
request: Request;
action: ActionFunction;
params: DataFunctionArgs["params"];
params: ActionFunctionArgs["params"];
loadContext: AppLoadContext;
routeId: string;
}) {
Expand Down Expand Up @@ -64,7 +65,7 @@ export async function callRouteLoaderRR({
}: {
request: Request;
loader: LoaderFunction;
params: DataFunctionArgs["params"];
params: LoaderFunctionArgs["params"];
loadContext: AppLoadContext;
routeId: string;
}) {
Expand Down
29 changes: 22 additions & 7 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,46 @@ export interface RouteModules<RouteModule> {
[routeId: string]: RouteModule;
}

// Context is always provided in Remix, and typed for module augmentation support.
// RR also doesn't export DataFunctionArgs, so we extend the two interfaces here
// even tough they're identical under the hood
/**
* @deprecated Use `LoaderFunctionArgs`/`ActionFunctionArgs` instead
*/
export type DataFunctionArgs = RRActionFunctionArgs<AppLoadContext> &
RRLoaderFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
// RR also doesn't export DataFunctionArgs, so we extend the two interfaces here
// even tough they're identical under the hood
context: AppLoadContext;
};

/**
* A function that handles data mutations for a route.
*/
export type ActionFunction = (
args: DataFunctionArgs
args: ActionFunctionArgs
) => ReturnType<RRActionFunction>;

export type ActionFunctionArgs = DataFunctionArgs;
/**
* Arguments passed to a route `action` function
*/
export type ActionFunctionArgs = RRActionFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
context: AppLoadContext;
};

/**
* A function that loads data for a route.
*/
export type LoaderFunction = (
args: DataFunctionArgs
args: LoaderFunctionArgs
) => ReturnType<RRLoaderFunction>;

export type LoaderFunctionArgs = DataFunctionArgs;
/**
* Arguments passed to a route `loader` function
*/
export type LoaderFunctionArgs = RRLoaderFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
context: AppLoadContext;
};

export type HeadersArgs = {
loaderHeaders: Headers;
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-testing/__tests__/stub-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useLoaderData,
useMatches,
} from "@remix-run/react";
import type { DataFunctionArgs } from "@remix-run/node";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";

test("renders a route", () => {
Expand Down Expand Up @@ -128,7 +128,7 @@ test("fetchers work", async () => {
});

test("can pass a predefined loader", () => {
async function loader(_args: DataFunctionArgs) {
async function loader(_args: LoaderFunctionArgs) {
return json({ hi: "there" });
}

Expand Down