Skip to content

Commit bcbd8f7

Browse files
committed
Update getLoadContext types for middleware context
1 parent 687bb87 commit bcbd8f7

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

packages/react-router-architect/server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { AppLoadContext, ServerBuild } from "react-router";
1+
import type {
2+
AppLoadContext,
3+
UNSAFE_MiddlewareEnabled as MiddlewareEnabled,
4+
ServerBuild,
5+
unstable_InitialContext,
6+
} from "react-router";
27
import { createRequestHandler as createReactRouterRequestHandler } from "react-router";
38
import { readableStreamToString } from "@react-router/node";
49
import type {
@@ -10,6 +15,8 @@ import type {
1015

1116
import { isBinaryType } from "./binaryTypes";
1217

18+
type MaybePromise<T> = T | Promise<T>;
19+
1320
/**
1421
* A function that returns the value to use as `context` in route `loader` and
1522
* `action` functions.
@@ -19,7 +26,9 @@ import { isBinaryType } from "./binaryTypes";
1926
*/
2027
export type GetLoadContextFunction = (
2128
event: APIGatewayProxyEventV2
22-
) => Promise<AppLoadContext> | AppLoadContext;
29+
) => MiddlewareEnabled extends true
30+
? MaybePromise<unstable_InitialContext>
31+
: MaybePromise<AppLoadContext>;
2332

2433
export type RequestHandler = APIGatewayProxyHandlerV2;
2534

packages/react-router-cloudflare/worker.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import type { AppLoadContext, ServerBuild } from "react-router";
1+
import type {
2+
AppLoadContext,
3+
UNSAFE_MiddlewareEnabled as MiddlewareEnabled,
4+
ServerBuild,
5+
unstable_InitialContext,
6+
} from "react-router";
27
import { createRequestHandler as createReactRouterRequestHandler } from "react-router";
38
import { type CacheStorage } from "@cloudflare/workers-types";
49

10+
type MaybePromise<T> = T | Promise<T>;
11+
512
/**
613
* A function that returns the value to use as `context` in route `loader` and
714
* `action` functions.
@@ -29,7 +36,9 @@ export type GetLoadContextFunction<
2936
caches: CacheStorage;
3037
};
3138
};
32-
}) => AppLoadContext | Promise<AppLoadContext>;
39+
}) => MiddlewareEnabled extends true
40+
? MaybePromise<unstable_InitialContext>
41+
: MaybePromise<AppLoadContext>;
3342

3443
export type RequestHandler<Env = any> = PagesFunction<Env>;
3544

packages/react-router-express/server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
/// <reference lib="DOM.Iterable" />
44

55
import type * as express from "express";
6-
import type { AppLoadContext, ServerBuild } from "react-router";
6+
import type {
7+
AppLoadContext,
8+
ServerBuild,
9+
UNSAFE_MiddlewareEnabled as MiddlewareEnabled,
10+
unstable_InitialContext,
11+
} from "react-router";
712
import { createRequestHandler as createRemixRequestHandler } from "react-router";
813
import {
914
createReadableStreamFromReadable,
1015
writeReadableStreamToWritable,
1116
} from "@react-router/node";
1217

18+
type MaybePromise<T> = T | Promise<T>;
19+
1320
/**
1421
* A function that returns the value to use as `context` in route `loader` and
1522
* `action` functions.
@@ -21,7 +28,9 @@ import {
2128
export type GetLoadContextFunction = (
2229
req: express.Request,
2330
res: express.Response
24-
) => Promise<AppLoadContext> | AppLoadContext;
31+
) => MiddlewareEnabled extends true
32+
? MaybePromise<unstable_InitialContext>
33+
: MaybePromise<AppLoadContext>;
2534

2635
export type RequestHandler = (
2736
req: express.Request,

packages/react-router/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ export type {
269269
FlashSessionData,
270270
} from "./lib/server-runtime/sessions";
271271

272-
export type { Future } from "./lib/types/future.ts";
272+
export type {
273+
Future,
274+
MiddlewareEnabled as UNSAFE_MiddlewareEnabled,
275+
} from "./lib/types/future.ts";
273276
export type { unstable_SerializesTo } from "./lib/types/serializes-to.ts";
274277
export type { Register } from "./lib/types/register";
275278
export { href } from "./lib/href";

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StaticHandler, StaticHandlerContext } from "../router/router";
2-
import type { ErrorResponse } from "../router/utils";
2+
import type { ErrorResponse, unstable_InitialContext } from "../router/utils";
33
import { unstable_RouterContextProvider } from "../router/utils";
44
import {
55
isRouteErrorResponse,
@@ -90,11 +90,12 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
9090
return async function requestHandler(request, _loadContext) {
9191
_build = typeof build === "function" ? await build() : build;
9292

93-
// TODO: Accept `initialContext` from `getLoadContext` when the flag is enabled
9493
let loadContext: AppLoadContext = _build.future.unstable_middleware
9594
? // @ts-expect-error This type changes when middleware is enabled
96-
(new unstable_RouterContextProvider() as AppLoadContext)
97-
: _loadContext ?? {};
95+
(new unstable_RouterContextProvider(
96+
_loadContext as unknown as unstable_InitialContext
97+
) as AppLoadContext)
98+
: _loadContext || {};
9899

99100
if (typeof build === "function") {
100101
let derived = derive(_build, mode);

0 commit comments

Comments
 (0)