Skip to content

Commit

Permalink
fix: allow beforeLoad to return never` (#954)
Browse files Browse the repository at this point in the history
this can for example happen if `beforeLoad` only throws redirects.
  • Loading branch information
schiller-manuel authored Jan 8, 2024
1 parent 039b25e commit 9cd61c7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
10 changes: 9 additions & 1 deletion packages/react-router/src/fileRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ export class FileRoute<
TParentRoute['types']['allParams'],
TParams
>,
TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
TRouteContextReturn extends
RouteConstraints['TRouteContext'] = RouteContext,
TRouteContext extends RouteConstraints['TRouteContext'] = [
TRouteContextReturn,
] extends [never]
? RouteContext
: TRouteContextReturn,
TContext extends Expand<
Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
> = Expand<
Expand All @@ -149,6 +155,7 @@ export class FileRoute<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TContext,
TLoaderDeps,
Expand All @@ -170,6 +177,7 @@ export class FileRoute<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TContext,
TRouterContext,
Expand Down
82 changes: 48 additions & 34 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type RouteOptions<
TFullSearchSchema extends Record<string, any> = TSearchSchema,
TParams extends AnyPathParams = AnyPathParams,
TAllParams extends AnyPathParams = TParams,
TRouteContextReturn extends RouteContext = RouteContext,
TRouteContext extends RouteContext = RouteContext,
TAllContext extends Record<string, any> = AnyContext,
TLoaderDeps extends Record<string, any> = {},
Expand All @@ -82,6 +83,7 @@ export type RouteOptions<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TAllContext,
TLoaderDeps,
Expand All @@ -105,6 +107,7 @@ export type BaseRouteOptions<
TFullSearchSchema extends Record<string, any> = TSearchSchema,
TParams extends AnyPathParams = {},
TAllParams = ParamsFallback<TPath, TParams>,
TRouteContextReturn extends RouteContext = RouteContext,
TRouteContext extends RouteContext = RouteContext,
TAllContext extends Record<string, any> = AnyContext,
TLoaderDeps extends Record<string, any> = {},
Expand All @@ -122,36 +125,27 @@ export type BaseRouteOptions<
TRouteContext
>,
) => any)
} & (keyof PickRequired<RouteContext> extends never
? // This async function is called before a route is loaded.
// If an error is thrown here, the route's loader will not be called.
// If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
// If thrown during a preload event, the error will be logged to the console.
{
beforeLoad?: BeforeLoadFn<
TFullSearchSchema,
TParentRoute,
TAllParams,
TRouteContext
>
}
: {
beforeLoad: BeforeLoadFn<
TFullSearchSchema,
TParentRoute,
TAllParams,
TRouteContext
>
}) & {
loaderDeps?: (opts: { search: TFullSearchSchema }) => TLoaderDeps
loader?: RouteLoaderFn<
TAllParams,
NoInfer<TLoaderDeps>,
NoInfer<TAllContext>,
NoInfer<TRouteContext>,
TLoaderData
>
} & (
} & {
// This async function is called before a route is loaded.
// If an error is thrown here, the route's loader will not be called.
// If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.
// If thrown during a preload event, the error will be logged to the console.
beforeLoad?: BeforeLoadFn<
TFullSearchSchema,
TParentRoute,
TAllParams,
TRouteContextReturn
>
} & {
loaderDeps?: (opts: { search: TFullSearchSchema }) => TLoaderDeps
loader?: RouteLoaderFn<
TAllParams,
NoInfer<TLoaderDeps>,
NoInfer<TAllContext>,
NoInfer<TRouteContext>,
TLoaderData
>
} & (
| {
// Both or none
parseParams?: (
Expand All @@ -173,7 +167,7 @@ type BeforeLoadFn<
TFullSearchSchema extends Record<string, any>,
TParentRoute extends AnyRoute,
TAllParams,
TRouteContext,
TRouteContextReturn extends RouteContext,
> = (opts: {
search: TFullSearchSchema
abortController: AbortController
Expand All @@ -184,7 +178,7 @@ type BeforeLoadFn<
navigate: NavigateFn<AnyRoute>
buildLocation: BuildLocationFn<TParentRoute>
cause: 'preload' | 'enter' | 'stay'
}) => Promise<TRouteContext> | TRouteContext | void
}) => Promise<TRouteContextReturn> | TRouteContextReturn | void

export type UpdatableRouteOptions<
TFullSearchSchema extends Record<string, any>,
Expand Down Expand Up @@ -343,6 +337,7 @@ export interface AnyRoute
any,
any,
any,
any,
any
> {}

Expand Down Expand Up @@ -471,7 +466,12 @@ export class Route<
TParentRoute,
TParams
>,
TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
TRouteContextReturn extends RouteConstraints['TRouteContext'] = RouteContext,
TRouteContext extends RouteConstraints['TRouteContext'] = [
TRouteContextReturn,
] extends [never]
? RouteContext
: TRouteContextReturn,
TAllContext extends Expand<
Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
> = Expand<
Expand All @@ -495,6 +495,7 @@ export class Route<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TAllContext,
TLoaderDeps,
Expand Down Expand Up @@ -531,6 +532,7 @@ export class Route<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TAllContext,
TLoaderDeps,
Expand Down Expand Up @@ -583,6 +585,7 @@ export class Route<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TAllContext,
TLoaderDeps,
Expand Down Expand Up @@ -655,6 +658,7 @@ export class Route<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TAllContext,
TRouterContext,
Expand Down Expand Up @@ -690,6 +694,7 @@ export class Route<
TFullSearchSchema,
TParams,
TAllParams,
TRouteContextReturn,
TRouteContext,
TAllContext,
TRouterContext,
Expand Down Expand Up @@ -746,13 +751,14 @@ export class Route<
}
}

export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any>
export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any, any>

export function rootRouteWithContext<TRouterContext extends {}>() {
return <
TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
TSearchSchema extends Record<string, any> = RootSearchSchema,
TSearchSchemaUsed extends Record<string, any> = RootSearchSchema,
TRouteContextReturn extends RouteContext = RouteContext,
TRouteContext extends RouteContext = RouteContext,
TLoaderDeps extends Record<string, any> = {},
TLoaderData extends any = unknown,
Expand All @@ -769,6 +775,7 @@ export function rootRouteWithContext<TRouterContext extends {}>() {
TSearchSchema, // TFullSearchSchema
{}, // TParams
{}, // TAllParams
TRouteContextReturn, // TRouteContextReturn
TRouteContext, // TRouteContext
Assign<TRouterContext, TRouteContext>, // TAllContext
TLoaderDeps,
Expand All @@ -785,6 +792,7 @@ export function rootRouteWithContext<TRouterContext extends {}>() {
TSearchSchemaInput,
TSearchSchema,
TSearchSchemaUsed,
TRouteContextReturn,
TRouteContext,
TRouterContext
> => {
Expand All @@ -800,6 +808,7 @@ export class RootRoute<
TSearchSchemaInput extends Record<string, any> = RootSearchSchema,
TSearchSchema extends Record<string, any> = RootSearchSchema,
TSearchSchemaUsed extends Record<string, any> = RootSearchSchema,
TRouteContextReturn extends RouteContext = RouteContext,
TRouteContext extends RouteContext = RouteContext,
TRouterContext extends {} = {},
TLoaderDeps extends Record<string, any> = {},
Expand All @@ -817,6 +826,7 @@ export class RootRoute<
TSearchSchema, // TFullSearchSchema
{}, // TParams
{}, // TAllParams
TRouteContextReturn, // TRouteContextReturn
TRouteContext, // TRouteContext
Expand<Assign<TRouterContext, TRouteContext>>, // TAllContext
TRouterContext, // TRouterContext
Expand All @@ -838,6 +848,7 @@ export class RootRoute<
TSearchSchema, // TFullSearchSchema
{}, // TParams
{}, // TAllParams
TRouteContextReturn, // TRouteContextReturn
TRouteContext, // TRouteContext
Assign<TRouterContext, TRouteContext>, // TAllContext
TLoaderDeps,
Expand Down Expand Up @@ -957,6 +968,7 @@ export class NotFoundRoute<
> = Expand<
Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
>,
TRouteContextReturn extends RouteConstraints['TRouterContext'] = AnyContext,
TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
TLoaderDeps extends Record<string, any> = {},
TLoaderData extends any = unknown,
Expand All @@ -975,6 +987,7 @@ export class NotFoundRoute<
TFullSearchSchema,
{},
{},
TRouteContextReturn,
TRouteContext,
TAllContext,
TRouterContext,
Expand All @@ -996,6 +1009,7 @@ export class NotFoundRoute<
TFullSearchSchema,
{},
{},
TRouteContextReturn,
TRouteContext,
TAllContext,
TLoaderDeps,
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/src/routeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type ParseRouteChildren<TRouteTree extends AnyRoute> =
any,
any,
any,
any,
infer TChildren,
any
>
Expand Down

0 comments on commit 9cd61c7

Please sign in to comment.