From dfec50f52115e71cd8f86c7981b0b334915c3e39 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Wed, 21 Aug 2024 20:27:05 +0200 Subject: [PATCH] fix(react-router): restore previous `matchRoutes` signature fixes #2164 --- packages/react-router/src/router.ts | 47 +++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts index 702adc475c..565e51de8e 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -42,6 +42,7 @@ import type { AnyContext, AnyRoute, AnyRouteWithContext, + AnySearchSchema, BeforeLoadContextOptions, ErrorRouteComponent, LoaderFnContext, @@ -588,6 +589,8 @@ export function createRouter< >(options) } +type MatchRoutesOpts = { preload?: boolean; throwOnError?: boolean } + export class Router< in out TRouteTree extends AnyRoute, in out TTrailingSlashOption extends TrailingSlashOption, @@ -950,10 +953,50 @@ export class Router< return this.routesById as Record } - matchRoutes = ( + /** + @deprecated use the following signature instead + ```ts + matchRoutes ( + next: ParsedLocation, + opts?: { preload?: boolean; throwOnError?: boolean }, + ): Array; + ``` +*/ + public matchRoutes( + pathname: string, + locationSearch: AnySearchSchema, + opts?: MatchRoutesOpts, + ): Array + public matchRoutes( + next: ParsedLocation, + opts?: MatchRoutesOpts, + ): Array + + public matchRoutes( + pathnameOrNext: string | ParsedLocation, + locationSearchOrOpts?: + | AnySearchSchema + | { preload?: boolean; throwOnError?: boolean }, + opts?: { preload?: boolean; throwOnError?: boolean }, + ) { + if (typeof pathnameOrNext === 'string') { + return this.matchRoutesInternal( + { + pathname: pathnameOrNext, + search: locationSearchOrOpts, + } as ParsedLocation, + opts, + ) + } + else { + return this.matchRoutesInternal(pathnameOrNext, locationSearchOrOpts) + } + } + + private matchRoutesInternal( next: ParsedLocation, opts?: { preload?: boolean; throwOnError?: boolean }, - ): Array => { + ): Array { let routeParams: Record = {} const foundRoute = this.flatRoutes.find((route) => {