Skip to content

Commit

Permalink
Remove response stub from LoaderFunctionArgs/ActionFunctionArgs (#9398)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed May 9, 2024
1 parent 45c7552 commit a0d4e9b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .changeset/fresh-pigs-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@remix-run/server-runtime": patch
---

Remove `response` stub from `LoaderFunctionArgs`/`ActionFunctionArgs`
- Instead, you will need to use `defineLaoder`/`defineAction` with single fetch to gain type access to the `response` stub
10 changes: 7 additions & 3 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type {
ActionFunctionArgs,
LoaderFunction,
LoaderFunctionArgs,
ResponseStub,
} from "./routeModules";
import type { ResponseStub } from "./single-fetch";

/**
* An object of unknown type for route loaders and actions provided by the
Expand Down Expand Up @@ -49,7 +49,9 @@ export async function callRouteAction({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params,
response,
// Only provided when single fetch is enabled, and made available via
// `defineAction` types, not `ActionFunctionArgs`
...(singleFetch ? { response } : null),
});

if (result === undefined) {
Expand Down Expand Up @@ -88,7 +90,9 @@ export async function callRouteLoader({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params,
response,
// Only provided when single fetch is enabled, and made available via
// `defineLoader` types, not `LoaderFunctionArgs`
...(singleFetch ? { response } : null),
});

if (result === undefined) {
Expand Down
23 changes: 0 additions & 23 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,6 @@ export type DataFunctionArgs = RRActionFunctionArgs<AppLoadContext> &
context: AppLoadContext;
};

export const ResponseStubOperationsSymbol = Symbol("ResponseStubOperations");
export type ResponseStubOperation = [
"set" | "append" | "delete",
string,
string?
];
/**
* A stubbed response to let you set the status/headers of your response from
* loader/action functions
*/
export type ResponseStub = {
status: number | undefined;
headers: Headers;
};

export type ResponseStubImpl = ResponseStub & {
[ResponseStubOperationsSymbol]: ResponseStubOperation[];
};

/**
* A function that handles data mutations for a route on the server
*/
Expand All @@ -59,8 +40,6 @@ export type ActionFunction = (
export type ActionFunctionArgs = RRActionFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
context: AppLoadContext;
// TODO: (v7) Make this non-optional once single-fetch is the default
response?: ResponseStub;
};

/**
Expand Down Expand Up @@ -92,8 +71,6 @@ export type LoaderFunction = (
export type LoaderFunctionArgs = RRLoaderFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
context: AppLoadContext;
// TODO: (v7) Make this non-optional once single-fetch is the default
response?: ResponseStub;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import {
singleFetchAction,
singleFetchLoaders,
SingleFetchRedirectSymbol,
ResponseStubOperationsSymbol,
} from "./single-fetch";
import { resourceRouteJsonWarning } from "./deprecations";
import { ResponseStubOperationsSymbol } from "./routeModules";

export type RequestHandler = (
request: Request,
Expand Down
26 changes: 20 additions & 6 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import { encode } from "turbo-stream";
import type { AppLoadContext } from "./data";
import { sanitizeError, sanitizeErrors } from "./errors";
import { ServerMode } from "./mode";
import type {
ResponseStub,
ResponseStubImpl,
ResponseStubOperation,
} from "./routeModules";
import { ResponseStubOperationsSymbol } from "./routeModules";
import type { TypedDeferredData, TypedResponse } from "./responses";
import { isDeferredData, isRedirectStatusCode, isResponse } from "./responses";
import type { SerializeFrom } from "./serialize";
Expand Down Expand Up @@ -544,6 +538,26 @@ export type Serialize<T extends Loader | ClientLoader | Action | ClientAction> =
Awaited<ReturnType<T>> extends TypedResponse<Record<string, unknown>> ? SerializeFrom<T> :
Awaited<ReturnType<T>>;

export const ResponseStubOperationsSymbol = Symbol("ResponseStubOperations");
export type ResponseStubOperation = [
"set" | "append" | "delete",
string,
string?
];

/**
* A stubbed response to let you set the status/headers of your response from
* loader/action functions
*/
export type ResponseStub = {
status: number | undefined;
headers: Headers;
};

export type ResponseStubImpl = ResponseStub & {
[ResponseStubOperationsSymbol]: ResponseStubOperation[];
};

// loader
type LoaderArgs = RRLoaderArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
Expand Down

0 comments on commit a0d4e9b

Please sign in to comment.