Skip to content

feat(react-router): update LoaderFunctionArgs and friends when middleware is enabled #13381

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

Merged
merged 4 commits into from
Apr 8, 2025
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
5 changes: 5 additions & 0 deletions .changeset/violet-carrots-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

UNSTABLE: Update context type for `LoaderFunctionArgs`/`ActionFunctionArgs` when middleware is enabled
17 changes: 11 additions & 6 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MiddlewareEnabled } from "../types/future";
import type { Equal, Expect } from "../types/utils";
import type { Location, Path, To } from "./history";
import { invariant, parsePath, warning } from "./history";
Expand Down Expand Up @@ -172,6 +173,10 @@ export class unstable_RouterContextProvider {
}
}

type DefaultContext = MiddlewareEnabled extends true
? unstable_RouterContextProvider
: any;

/**
* @private
* Arguments passed to route loader/action functions. Same for now but we keep
Expand Down Expand Up @@ -225,13 +230,13 @@ export type unstable_MiddlewareFunction<Result = unknown> = (
/**
* Arguments passed to loader functions
*/
export interface LoaderFunctionArgs<Context = any>
export interface LoaderFunctionArgs<Context = DefaultContext>
extends DataFunctionArgs<Context> {}

/**
* Arguments passed to action functions
*/
export interface ActionFunctionArgs<Context = any>
export interface ActionFunctionArgs<Context = DefaultContext>
extends DataFunctionArgs<Context> {}

/**
Expand All @@ -244,7 +249,7 @@ type DataFunctionReturnValue = MaybePromise<DataFunctionValue>;
/**
* Route loader function signature
*/
export type LoaderFunction<Context = any> = {
export type LoaderFunction<Context = DefaultContext> = {
(
args: LoaderFunctionArgs<Context>,
handlerCtx?: unknown
Expand All @@ -254,7 +259,7 @@ export type LoaderFunction<Context = any> = {
/**
* Route action function signature
*/
export interface ActionFunction<Context = any> {
export interface ActionFunction<Context = DefaultContext> {
(
args: ActionFunctionArgs<Context>,
handlerCtx?: unknown
Expand Down Expand Up @@ -339,7 +344,7 @@ export interface DataStrategyMatch
) => Promise<DataStrategyResult>;
}

export interface DataStrategyFunctionArgs<Context = any>
export interface DataStrategyFunctionArgs<Context = DefaultContext>
extends DataFunctionArgs<Context> {
matches: DataStrategyMatch[];
fetcherKey: string | null;
Expand All @@ -353,7 +358,7 @@ export interface DataStrategyResult {
result: unknown; // data, Error, Response, DeferredData, DataWithResponseInit
}

export interface DataStrategyFunction<Context = any> {
export interface DataStrategyFunction<Context = DefaultContext> {
(args: DataStrategyFunctionArgs<Context>): Promise<
Record<string, DataStrategyResult>
>;
Expand Down