Skip to content

Commit

Permalink
Fix typings for response in LoaderFunctonArgs/ActionFunctionArgs (#9254)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Apr 18, 2024
1 parent 2d7e704 commit b75afdf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-beds-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

[REMOVE] Fix typings for response in LoaderFunctonArgs/ActionFunctionArgs
3 changes: 3 additions & 0 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type ResponseStubOperation = [
export type ResponseStub = {
status: number | undefined;
headers: Headers;
};

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

Expand Down
13 changes: 6 additions & 7 deletions packages/remix-server-runtime/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type {

import { callRouteAction, callRouteLoader } from "./data";
import type { FutureConfig } from "./entry";
import type { ResponseStub, ServerRouteModule } from "./routeModules";
import type { ServerRouteModule } from "./routeModules";
import type { DataStrategyCtx } from "./single-fetch";

export interface RouteManifest<Route> {
[routeId: string]: Route;
Expand Down Expand Up @@ -100,9 +101,8 @@ export function createStaticHandlerDataRoutes(
loader: route.module.loader!,
routeId: route.id,
singleFetch: future.unstable_singleFetch === true,
response: (
dataStrategyCtx as unknown as { response: ResponseStub }
)?.response,
response: (dataStrategyCtx as DataStrategyCtx | undefined)
?.response,
})
: undefined,
action: route.module.action
Expand All @@ -114,9 +114,8 @@ export function createStaticHandlerDataRoutes(
action: route.module.action!,
routeId: route.id,
singleFetch: future.unstable_singleFetch === true,
response: (
dataStrategyCtx as unknown as { response: ResponseStub }
)?.response,
response: (dataStrategyCtx as DataStrategyCtx | undefined)
?.response,
})
: undefined,
handle: route.module.handle,
Expand Down
22 changes: 16 additions & 6 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { encode } from "turbo-stream";
import type { AppLoadContext } from "./data";
import { sanitizeError, sanitizeErrors } from "./errors";
import { ServerMode } from "./mode";
import type { ResponseStub, ResponseStubOperation } from "./routeModules";
import type {
ResponseStub,
ResponseStubImpl,
ResponseStubOperation,
} from "./routeModules";
import { ResponseStubOperationsSymbol } from "./routeModules";
import { isDeferredData, isRedirectStatusCode, isResponse } from "./responses";

Expand All @@ -35,6 +39,10 @@ export type SingleFetchResults = {
[SingleFetchRedirectSymbol]?: SingleFetchRedirectResult;
};

export type DataStrategyCtx = {
response: ResponseStub;
};

export function getSingleFetchDataStrategy(
responseStubs: ReturnType<typeof getResponseStubs>,
{
Expand All @@ -54,19 +62,21 @@ export function getSingleFetchDataStrategy(

let results = await Promise.all(
matches.map(async (match) => {
let responseStub: ResponseStub | undefined;
let responseStub: ResponseStubImpl | undefined;
if (request.method !== "GET") {
responseStub = responseStubs[ResponseStubActionSymbol];
} else {
responseStub = responseStubs[match.route.id];
}

let result = await match.resolve(async (handler) => {
// Cast `ResponseStubImpl -> ResponseStub` to hide the symbol in userland
let ctx: DataStrategyCtx = { response: responseStub as ResponseStub };
// Only run opt-in loaders when fine-grained revalidation is enabled
let data =
loadRouteIds && !loadRouteIds.includes(match.route.id)
? null
: await handler({ response: responseStub });
: await handler(ctx);
return { type: "data", result: data };
});

Expand Down Expand Up @@ -282,7 +292,7 @@ export async function singleFetchLoaders(
}
}

export function isResponseStub(value: any): value is ResponseStub {
export function isResponseStub(value: any): value is ResponseStubImpl {
return (
value && typeof value === "object" && ResponseStubOperationsSymbol in value
);
Expand Down Expand Up @@ -310,7 +320,7 @@ function getResponseStub(status?: number) {
}

export function getResponseStubs() {
return new Proxy({} as Record<string | symbol, ResponseStub>, {
return new Proxy({} as Record<string | symbol, ResponseStubImpl>, {
get(responseStubCache, prop) {
let cached = responseStubCache[prop];
if (!cached) {
Expand All @@ -324,7 +334,7 @@ export function getResponseStubs() {
function proxyResponseToResponseStub(
status: number | undefined,
headers: Headers,
responseStub: ResponseStub
responseStub: ResponseStubImpl
) {
if (status != null && responseStub.status == null) {
responseStub.status = status;
Expand Down

0 comments on commit b75afdf

Please sign in to comment.