Skip to content

Commit

Permalink
Route registry (#6600)
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh authored Jan 17, 2025
1 parent 5dc98e1 commit 5c71a35
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/coresandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ const applyConfig = (config: ConfigType) => {
predicates: [ContentTypeCondition(['Document']), RouteCondition('/hello')],
});

config.registerRoute({
type: 'route',
path: '/hello',
file: 'src/components/Views/NewsAndEvents/asd.tsx',
options: {
id: 'hello',
index: true,
},
});

return config;
};

Expand Down
1 change: 1 addition & 0 deletions packages/registry/news/6600.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added route registry. @sneridagh
16 changes: 16 additions & 0 deletions packages/registry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
UtilitiesConfig,
ViewsConfig,
WidgetsConfig,
ReactRouterRouteEntry,
} from '@plone/types';

export type ConfigData = {
Expand All @@ -22,6 +23,7 @@ export type ConfigData = {
widgets: WidgetsConfig | Record<string, never>;
addonReducers?: AddonReducersConfig;
addonRoutes?: AddonRoutesConfig;
routes?: Array<ReactRouterRouteEntry>;
slots: SlotsConfig | Record<string, never>;
components: ComponentsConfig | Record<string, never>;
utilities: UtilitiesConfig | Record<string, never>;
Expand Down Expand Up @@ -126,6 +128,14 @@ class Config {
this._data.addonRoutes = addonRoutes;
}

get routes() {
return this._data.routes;
}

set routes(routes) {
this._data.routes = routes;
}

get slots() {
return this._data.slots;
}
Expand Down Expand Up @@ -494,6 +504,12 @@ class Config {

return utilities;
}

registerRoute(options: ReactRouterRouteEntry) {
const route = this._data.routes || [];
route.push(options);
this._data.routes = route;
}
}

const instance = new Config();
Expand Down
97 changes: 97 additions & 0 deletions packages/registry/src/registry.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,100 @@ describe('Utilities registry', () => {
).toEqual([]);
});
});

describe('Routes registry', () => {
afterEach(() => {
config.set('routes', []);
});

it('registers a simple route', () => {
config.registerRoute({
type: 'route',
path: '/login',
file: 'login.tsx',
});

expect(config.routes).toEqual([
{
type: 'route',
path: '/login',
file: 'login.tsx',
},
]);
});

it('registers a simple route with options', () => {
config.registerRoute({
type: 'route',
path: '/login',
file: 'login.tsx',
options: { id: 'login', caseSensitive: true },
});

expect(config.routes).toEqual([
{
type: 'route',
path: '/login',
file: 'login.tsx',
options: { id: 'login', caseSensitive: true },
},
]);
});

it('registers a nested route', () => {
config.registerRoute({
type: 'route',
path: '/login',
file: 'login.tsx',
children: [
{
type: 'route',
path: '/login/ok',
file: 'ok.tsx',
},
],
});

expect(config.routes).toEqual([
{
type: 'route',
path: '/login',
file: 'login.tsx',
children: [
{
type: 'route',
path: '/login/ok',
file: 'ok.tsx',
},
],
},
]);
});

it('registers a couple of routes', () => {
config.registerRoute({
type: 'route',
path: '/login',
file: 'login.tsx',
});

config.registerRoute({
type: 'route',
path: '/logout',
file: 'logout.tsx',
});

expect(config.routes).toEqual([
{
type: 'route',
path: '/login',
file: 'login.tsx',
},
{
type: 'route',
path: '/logout',
file: 'logout.tsx',
},
]);
});
});
1 change: 1 addition & 0 deletions packages/types/news/6600.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added typings for the route registry. @sneridagh
18 changes: 18 additions & 0 deletions packages/types/src/config/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ export type AddonRoutesConfig = {
component: React.ComponentType;
}[];

export type AddonRoutesEntry = {
path: string;
exact: boolean;
component: React.ComponentType;
};

export type ReactRouterRouteEntry = {
type: 'route' | 'index' | 'layout' | 'prefix';
path: string;
file: string;
options?: {
id?: string;
index?: boolean;
caseSensitive?: boolean;
};
children?: ReactRouterRouteEntry[];
};

export type ComponentsConfig = Record<
string,
{ component: React.ComponentType }
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
* Get the type of the elements in an array
*/
export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;

export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
T,
Exclude<keyof T, Keys>
> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
}[Keys];

0 comments on commit 5c71a35

Please sign in to comment.