core-api: rename routes to routePaths
Co-authored-by: blam <ben@blam.sh>
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import React, { PropsWithChildren } from 'react';
|
||||
import { routeCollector, routeParentCollector } from './collectors';
|
||||
import { routePathCollector, routeParentCollector } from './collectors';
|
||||
|
||||
import {
|
||||
traverseElementTree,
|
||||
@@ -96,7 +96,7 @@ describe('discovery', () => {
|
||||
root,
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routes: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
});
|
||||
@@ -147,7 +147,7 @@ describe('discovery', () => {
|
||||
root,
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routes: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
});
|
||||
@@ -184,7 +184,7 @@ describe('discovery', () => {
|
||||
),
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routes: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -19,7 +19,7 @@ import { RouteRef } from '../routing/types';
|
||||
import { getComponentData } from '../extensions';
|
||||
import { createCollector } from '../extensions/traversal';
|
||||
|
||||
export const routeCollector = createCollector(
|
||||
export const routePathCollector = createCollector(
|
||||
() => new Map<RouteRef, string>(),
|
||||
(acc, node, parent) => {
|
||||
if (parent.props.element === node) {
|
||||
|
||||
@@ -24,12 +24,16 @@ import {
|
||||
traverseElementTree,
|
||||
} from '../extensions/traversal';
|
||||
import { createPlugin } from '../plugin';
|
||||
import { routeCollector, routeParentCollector } from './collectors';
|
||||
import { routePathCollector, routeParentCollector } from './collectors';
|
||||
import { useRouteRef, RoutingProvider, validateRoutes } from './hooks';
|
||||
import { createRouteRef } from './RouteRef';
|
||||
import { RouteRef } from './types';
|
||||
import { RouteRef, RouteRefConfig } from './types';
|
||||
|
||||
const mockConfig = () => ({ path: '/unused', title: 'Unused' });
|
||||
const mockConfig = (extra?: Partial<RouteRefConfig>) => ({
|
||||
path: '/unused',
|
||||
title: 'Unused',
|
||||
...extra,
|
||||
});
|
||||
const MockComponent = ({ children }: PropsWithChildren<{}>) => <>{children}</>;
|
||||
|
||||
const plugin = createPlugin({ id: 'my-plugin' });
|
||||
@@ -83,17 +87,17 @@ describe('discovery', () => {
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const { routes, routeParents } = traverseElementTree({
|
||||
const { routePaths, routeParents } = traverseElementTree({
|
||||
root,
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routePaths: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
});
|
||||
|
||||
const rendered = render(
|
||||
<RoutingProvider routes={routes} routeParents={routeParents}>
|
||||
<RoutingProvider routePaths={routePaths} routeParents={routeParents}>
|
||||
{root}
|
||||
</RoutingProvider>,
|
||||
);
|
||||
@@ -123,17 +127,17 @@ describe('discovery', () => {
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const { routes, routeParents } = traverseElementTree({
|
||||
const { routePaths, routeParents } = traverseElementTree({
|
||||
root,
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routePaths: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
});
|
||||
|
||||
const rendered = render(
|
||||
<RoutingProvider routes={routes} routeParents={routeParents}>
|
||||
<RoutingProvider routePaths={routePaths} routeParents={routeParents}>
|
||||
{root}
|
||||
</RoutingProvider>,
|
||||
);
|
||||
@@ -146,7 +150,7 @@ describe('discovery', () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should handle relative routing within parameterized routes', () => {
|
||||
it('should handle relative routing within parameterized routePaths', () => {
|
||||
const root = (
|
||||
<MemoryRouter initialEntries={['/foo/blob/bar']}>
|
||||
<Routes>
|
||||
@@ -158,17 +162,17 @@ describe('discovery', () => {
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const { routes, routeParents } = traverseElementTree({
|
||||
const { routePaths, routeParents } = traverseElementTree({
|
||||
root,
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routePaths: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
});
|
||||
|
||||
const rendered = render(
|
||||
<RoutingProvider routes={routes} routeParents={routeParents}>
|
||||
<RoutingProvider routePaths={routePaths} routeParents={routeParents}>
|
||||
{root}
|
||||
</RoutingProvider>,
|
||||
);
|
||||
@@ -178,7 +182,7 @@ describe('discovery', () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should handle relative routing of parameterized routes with duplicate param names', () => {
|
||||
it('should handle relative routing of parameterized routePaths with duplicate param names', () => {
|
||||
const root = (
|
||||
<MemoryRouter>
|
||||
<Routes>
|
||||
@@ -189,16 +193,16 @@ describe('discovery', () => {
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const { routes, routeParents } = traverseElementTree({
|
||||
const { routePaths, routeParents } = traverseElementTree({
|
||||
root,
|
||||
discoverers: [childDiscoverer, routeElementDiscoverer],
|
||||
collectors: {
|
||||
routes: routeCollector,
|
||||
routePaths: routePathCollector,
|
||||
routeParents: routeParentCollector,
|
||||
},
|
||||
});
|
||||
|
||||
expect(() => validateRoutes(routes, routeParents)).toThrow(
|
||||
expect(() => validateRoutes(routePaths, routeParents)).toThrow(
|
||||
'Parameter :id is duplicated in path /foo/:id/bar/:id',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -22,7 +22,7 @@ export type RouteFunc = (params?: Record<string, string>) => string;
|
||||
|
||||
class RouteResolver {
|
||||
constructor(
|
||||
private readonly routes: Map<RouteRef, string>,
|
||||
private readonly routePaths: Map<RouteRef, string>,
|
||||
private readonly routeParents: Map<RouteRef, RouteRef | undefined>,
|
||||
) {}
|
||||
|
||||
@@ -31,7 +31,7 @@ class RouteResolver {
|
||||
let fullPath = '';
|
||||
|
||||
while (currentRouteRef) {
|
||||
const path = this.routes.get(currentRouteRef);
|
||||
const path = this.routePaths.get(currentRouteRef);
|
||||
if (!path) {
|
||||
throw new Error(`No path for ${currentRouteRef}`);
|
||||
}
|
||||
@@ -58,17 +58,17 @@ export function useRouteRef(routeRef: RouteRef): RouteFunc {
|
||||
}
|
||||
|
||||
type ProviderProps = {
|
||||
routes: Map<RouteRef, string>;
|
||||
routePaths: Map<RouteRef, string>;
|
||||
routeParents: Map<RouteRef, RouteRef | undefined>;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
export const RoutingProvider = ({
|
||||
routes,
|
||||
routePaths,
|
||||
routeParents,
|
||||
children,
|
||||
}: ProviderProps) => {
|
||||
const resolver = new RouteResolver(routes, routeParents);
|
||||
const resolver = new RouteResolver(routePaths, routeParents);
|
||||
return (
|
||||
<RoutingContext.Provider value={resolver}>
|
||||
{children}
|
||||
@@ -77,7 +77,7 @@ export const RoutingProvider = ({
|
||||
};
|
||||
|
||||
export function validateRoutes(
|
||||
routes: Map<RouteRef, string>,
|
||||
routePaths: Map<RouteRef, string>,
|
||||
routeParents: Map<RouteRef, RouteRef | undefined>,
|
||||
) {
|
||||
const notLeafRoutes = new Set(routeParents.values());
|
||||
@@ -92,7 +92,7 @@ export function validateRoutes(
|
||||
|
||||
let fullPath = '';
|
||||
while (currentRouteRef) {
|
||||
const path = routes.get(currentRouteRef);
|
||||
const path = routePaths.get(currentRouteRef);
|
||||
if (!path) {
|
||||
throw new Error(`No path for ${currentRouteRef}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user