Skip to content

Commit

Permalink
Simplify router types
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Jan 23, 2022
1 parent 5f8e186 commit f6d7da1
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 713 deletions.
17 changes: 10 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 @@ -15,25 +15,28 @@ import {
} from 'react-router-config';
import qs from 'query-string';
import { findLastIndex, merge, compact } from 'lodash';
import { deepExactRt, mergeRt } from '@kbn/io-ts-utils';
import { FlattenRoutesOf, Route, Router } from './types';
import { mergeRt, deepExactRt } from '@kbn/io-ts-utils';
import { FlattenRoutesOf, Route, RouteWithPath, Router, RouteMap } from './types';

function toReactRouterPath(path: string) {
return path.replace(/(?:{([^\/]+)})/g, ':$1');
}

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

const reactRouterConfigs = routes.map((route) => toReactRouterConfigRoute(route));
const reactRouterConfigs = Object.entries(routes).map(([path, route]) =>
toReactRouterConfigRoute({ ...route, path })
);

function toReactRouterConfigRoute(route: Route): ReactRouterConfig {
function toReactRouterConfigRoute(route: RouteWithPath): ReactRouterConfig {
const reactRouterConfig: ReactRouterConfig = {
component: () => route.element,
routes:
(route.children as Route[] | undefined)?.map((child) => toReactRouterConfigRoute(child)) ??
[],
Object.entries((route.children as RouteMap | undefined) ?? {})?.map(([path, child]) =>
toReactRouterConfigRoute({ ...child, path })
) ?? [],
exact: !route.children?.length,
path: toReactRouterPath(route.path),
};
Expand Down
2 changes: 0 additions & 2 deletions packages/kbn-typed-react-router-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
export * from './create_router';
export * from './types';
export * from './outlet';
export * from './route';
export * from './route_renderer';
export * from './router_provider';
export * from './unconst';
export * from './use_current_route';
export * from './use_match_routes';
export * from './use_params';
Expand Down
15 changes: 0 additions & 15 deletions packages/kbn-typed-react-router-config/src/route.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import { History } from 'history';
import React from 'react';
import { Router as ReactRouter } from 'react-router-dom';
import { Route, Router } from './types';
import { RouteMap, Router } from './types';
import { RouterContextProvider } from './use_router';

export function RouterProvider({
children,
router,
history,
}: {
router: Router<Route[]>;
router: Router<RouteMap>;
history: History;
children: React.ReactNode;
}) {
Expand Down
Loading

0 comments on commit f6d7da1

Please sign in to comment.