Skip to content

Commit 86aa08b

Browse files
committed
Change middleware return type from void to undefined
1 parent f3a07bb commit 86aa08b

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

.changeset/unlucky-pumas-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
[UNSTABLE] Update `Route.unstable_MiddlewareFunction` to have a return value of `Response | undefined` instead of `Response | void` becaue you should not return anything if you aren't returning the `Response`

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222
DataStrategyResult,
2323
ImmutableRouteKey,
2424
MapRoutePropertiesFunction,
25+
MaybePromise,
2526
MutationFormMethod,
2627
RedirectResult,
2728
RouteData,
@@ -61,8 +62,6 @@ import {
6162
//#region Types and Constants
6263
////////////////////////////////////////////////////////////////////////////////
6364

64-
type MaybePromise<T> = T | Promise<T>;
65-
6665
/**
6766
* A Router instance manages all navigation and data loading/mutations
6867
*/

packages/react-router/lib/router/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { Equal, Expect } from "../types/utils";
22
import type { Location, Path, To } from "./history";
33
import { invariant, parsePath, warning } from "./history";
44

5+
export type MaybePromise<T> = T | Promise<T>;
6+
57
/**
68
* Map of routeId -> data returned from a loader/action/error
79
*/
@@ -206,7 +208,7 @@ interface DataFunctionArgs<Context> {
206208
* middlewares from the bottom-up
207209
*/
208210
export interface unstable_MiddlewareNextFunction<Result = unknown> {
209-
(): Result | Promise<Result>;
211+
(): MaybePromise<Result>;
210212
}
211213

212214
/**
@@ -218,7 +220,7 @@ export interface unstable_MiddlewareNextFunction<Result = unknown> {
218220
export type unstable_MiddlewareFunction<Result = unknown> = (
219221
args: DataFunctionArgs<unstable_RouterContextProvider>,
220222
next: unstable_MiddlewareNextFunction<Result>
221-
) => Result | Promise<Result>;
223+
) => MaybePromise<Result | undefined>;
222224

223225
/**
224226
* Arguments passed to loader functions
@@ -237,7 +239,7 @@ export interface ActionFunctionArgs<Context = any>
237239
*/
238240
type DataFunctionValue = unknown;
239241

240-
type DataFunctionReturnValue = Promise<DataFunctionValue> | DataFunctionValue;
242+
type DataFunctionReturnValue = MaybePromise<DataFunctionValue>;
241243

242244
/**
243245
* Route loader function signature
@@ -373,7 +375,7 @@ export type AgnosticPatchRoutesOnNavigationFunction<
373375
M extends AgnosticRouteMatch = AgnosticRouteMatch
374376
> = (
375377
opts: AgnosticPatchRoutesOnNavigationFunctionArgs<O, M>
376-
) => void | Promise<void>;
378+
) => MaybePromise<void>;
377379

378380
/**
379381
* Function provided by the framework-aware layers to set any framework-specific

packages/react-router/lib/types/route-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ type ServerDataFunctionArgs<T extends RouteInfo> = {
190190
export type CreateServerMiddlewareFunction<T extends RouteInfo> = (
191191
args: ServerDataFunctionArgs<T>,
192192
next: unstable_MiddlewareNextFunction<Response>
193-
) => MaybePromise<Response | void>;
193+
) => MaybePromise<Response | undefined>;
194194

195195
export type CreateClientMiddlewareFunction<T extends RouteInfo> = (
196196
args: ClientDataFunctionArgs<T>,

0 commit comments

Comments
 (0)