Skip to content

Commit

Permalink
[APM] Revert kbn/typed-react-router-config changes (#120581)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar authored Dec 7, 2021
1 parent 48d1846 commit 7f0a6cf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-typed-react-router-config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ TYPES_DEPS = [
"@npm//query-string",
"@npm//utility-types",
"@npm//@types/jest",
"@npm//@types/history",
"@npm//@types/node",
"@npm//@types/react-router-config",
"@npm//@types/react-router-dom",
"@npm//@types/history",
]

jsts_transpiler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ describe('createRouter', () => {

const matches = router.matchRoutes('/', history.location);

// @ts-expect-error 4.3.5 upgrade - router doesn't seem able to merge properly when two routes match
expect(matches[1]?.match.params).toEqual({
query: {
rangeFrom: 'now-30m',
Expand All @@ -286,7 +285,6 @@ describe('createRouter', () => {

expect(matchedRoutes.length).toEqual(4);

// @ts-expect-error 4.3.5 upgrade - router doesn't seem able to merge properly when two routes match
expect(matchedRoutes[matchedRoutes.length - 1].match).toEqual({
isExact: true,
params: {
Expand Down
14 changes: 7 additions & 7 deletions packages/kbn-typed-react-router-config/src/create_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function toReactRouterPath(path: string) {
return path.replace(/(?:{([^\/]+)})/g, ':$1');
}

export function createRouter<TRoute extends Route>(routes: TRoute[]): Router<TRoute[]> {
export function createRouter<TRoutes extends Route[]>(routes: TRoutes): Router<TRoutes> {
const routesByReactRouterConfig = new Map<ReactRouterConfig, Route>();
const reactRouterConfigsByRoute = new Map<Route, ReactRouterConfig>();

Expand Down Expand Up @@ -181,8 +181,10 @@ export function createRouter<TRoute extends Route>(routes: TRoute[]): Router<TRo
);
};

const router = {
link,
return {
link: (path, ...args) => {
return link(path, ...args);
},
getParams: (...args: any[]) => {
const matches = matchRoutes(...args);
return matches.length
Expand All @@ -195,13 +197,11 @@ export function createRouter<TRoute extends Route>(routes: TRoute[]): Router<TRo
matchRoutes: (...args: any[]) => {
return matchRoutes(...args) as any;
},
getRoutePath: (route: Route) => {
getRoutePath: (route) => {
return reactRouterConfigsByRoute.get(route)!.path as string;
},
getRoutesToMatch: (path: string) => {
return getRoutesToMatch(path) as unknown as FlattenRoutesOf<typeof routes>;
return getRoutesToMatch(path) as unknown as FlattenRoutesOf<TRoutes>;
},
};

return router;
}
20 changes: 9 additions & 11 deletions packages/kbn-typed-react-router-config/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface RouteMatch<TRoute extends Route = Route> {
params: t.Type<any>;
}
? t.TypeOf<TRoute['params']>
: AnyObj;
: {};
};
}

Expand Down Expand Up @@ -160,11 +160,10 @@ interface ReadonlyPlainRoute {
}

export type Route = PlainRoute | ReadonlyPlainRoute;
type AnyObj = Record<string, any>;

interface DefaultOutput {
path: AnyObj;
query: AnyObj;
path: {};
query: {};
}

type OutputOfRouteMatch<TRouteMatch extends RouteMatch> = TRouteMatch extends {
Expand All @@ -191,21 +190,20 @@ type TypeOfRouteMatch<TRouteMatch extends RouteMatch> = TRouteMatch extends {
route: { params: t.Type<any> };
}
? t.TypeOf<TRouteMatch['route']['params']>
: AnyObj;
: {};

type TypeOfMatches<TRouteMatches extends RouteMatch[]> = TRouteMatches extends [RouteMatch]
? TypeOfRouteMatch<TRouteMatches[0]>
: TRouteMatches extends [RouteMatch, ...infer TNextRouteMatches]
? TypeOfRouteMatch<TRouteMatches[0]> &
(TNextRouteMatches extends RouteMatch[] ? TypeOfMatches<TNextRouteMatches> : AnyObj)
: AnyObj;
(TNextRouteMatches extends RouteMatch[] ? TypeOfMatches<TNextRouteMatches> : {})
: {};

export type TypeOf<
TRoutes extends Route[],
TPath extends PathsOf<TRoutes>,
TWithDefaultOutput extends boolean = true
> = TypeOfMatches<Match<TRoutes, TPath>> &
(TWithDefaultOutput extends true ? DefaultOutput : AnyObj);
> = TypeOfMatches<Match<TRoutes, TPath>> & (TWithDefaultOutput extends true ? DefaultOutput : {});

export type TypeAsArgs<TObject> = keyof TObject extends never
? []
Expand Down Expand Up @@ -278,7 +276,7 @@ type MapRoute<TRoute extends Route, TParents extends Route[] = []> = MaybeUnion<
>;
}
>
: AnyObj
: {}
>;

type MapRoutes<TRoutes, TParents extends Route[] = []> = TRoutes extends [Route]
Expand Down Expand Up @@ -343,7 +341,7 @@ type MapRoutes<TRoutes, TParents extends Route[] = []> = TRoutes extends [Route]
MapRoute<TRoutes[8], TParents> &
MapRoute<TRoutes[7], TParents> &
MapRoute<TRoutes[9], TParents>
: AnyObj;
: {};

// const element = null as any;

Expand Down

0 comments on commit 7f0a6cf

Please sign in to comment.