8000 fix(router): add customRouteProcessor wip by eneajaho · Pull Request #53312 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

fix(router): add customRouteProcessor wip #53312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {Params} from './shared';
import {StateManager} from './statemanager/state_manager';
import {UrlHandlingStrategy} from './url_handling_strategy';
import {containsTree, IsActiveMatchOptions, isUrlTree, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
import {standardizeConfig, validateConfig} from './utils/config';
import {CUSTOM_ROUTE_PROCESSOR, standardizeConfig, validateConfig} from './utils/config';
import {afterNextNavigation} from './utils/navigations';


Expand Down Expand Up @@ -81,6 +81,7 @@ export class Router {
private readonly console = inject(Console);
private readonly stateManager = inject(StateManager);
private readonly options = inject(ROUTER_CONFIGURATION, {optional: true}) || {};
private customRouteProcessor = inject(CUSTOM_ROUTE_PROCESSOR, {optional: true}) ?? undefined;
private readonly pendingTasks = inject(InitialRenderPendingTasks);
private readonly urlUpdateStrategy = this.options.urlUpdateStrategy || 'deferred';
private readonly navigationTransitions = inject(NavigationTransitions);
Expand Down Expand Up @@ -334,7 +335,7 @@ export class Router {
*/
resetConfig(config: Routes): void {
(typeof ngDevMode === 'undefined' || ngDevMode) && validateConfig(config);
this.config = config.map(standardizeConfig);
this.config = config.map(x => standardizeConfig(x, this.customRouteProcessor));
this.navigated = false;
}

Expand Down
12 changes: 6 additions & 6 deletions packages/router/src/router_config_loader.ts
8000
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Compiler, EnvironmentInjector, inject, Injectable, InjectFlags, InjectionToken, Injector, NgModuleFactory, Type} from '@angular/core';
import {Compiler, EnvironmentInjector, inject, Injectable, InjectionToken, Injector, NgModuleFactory, Type} from '@angular/core';
import {ConnectableObservable, from, Observable, of, Subject} from 'rxjs';
import {finalize, map, mergeMap, refCount, tap} from 'rxjs/operators';

import {DefaultExport, LoadChildren, LoadChildrenCallback, LoadedRouterConfig, Route, Routes} from './models';
import {DefaultExport, LoadedRouterConfig, Route, Routes} from './models';
import {wrapIntoObservable} from './utils/collection';
import {assertStandalone, standardizeConfig, validateConfig} from './utils/config';


import {assertStandalone, CUSTOM_ROUTE_PROCESSOR, standardizeConfig, validateConfig} from './utils/config';

/**
* The [DI token](guide/glossary/#di-token) for a router configuration.
Expand Down Expand Up @@ -136,7 +134,9 @@ export function loadChildren(
// for it's parent module instead.
rawRoutes = injector.get(ROUTES, [], {optional: true, self: true}).flat();
}
const routes = rawRoutes.map(standardizeConfig);
const customRouteProcessor =
injector?.get(CUSTOM_ROUTE_PROCESSOR, undefined, {optional: true}) ?? undefined;
const routes = rawRoutes.map(x => standardizeConfig(x, customRouteProcessor));
(typeof ngDevMode === 'undefined' || ngDevMode) &&
validateConfig(routes, route.path, requireStandaloneComponents);
return {routes, injector};
Expand Down
26 changes: 22 additions & 4 deletions packages/router/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {createEnvironmentInjector, EnvironmentInjector, isStandalone, Type, ɵisNgModule as isNgModule, ɵRuntimeError as RuntimeError} from '@angular/core';
import {createEnvironmentInjector, EnvironmentInjector, InjectionToken, isStandalone, Type, ɵisNgModule as isNgModule, ɵRuntimeError as RuntimeError} from '@angular/core';

import {EmptyOutletComponent} from '../components/empty_outlet';
import {RuntimeErrorCode} from '../errors';
Expand Down Expand Up @@ -190,15 +190,33 @@ function getFullPath(parentPath: string, currentRoute: Route): string {
}

/**
* Makes a copy of the config and adds any default required properties.
* The [DI token](guide/glossary/#di-token) for processing the routes when being standardized.
*
* `CUSTOM_ROUTE_PROCESSOR` is a low level API for processing routes everytime they change.
*
* @publicApi
*/
export function standardizeConfig(r: Route): Route {
const children = r.children && r.children.map(standardizeConfig);
export const CUSTOM_ROUTE_PROCESSOR = new InjectionToken<(r: Route) => Route>(
'CUSTOM_ROUTE_PROCESSOR',
);


/**
* Makes a copy of the config and adds any default required properties and apply the custom route
* processor.
*/
export function standardizeConfig(r: Route, customRouteProcessor?: (r: Route) => Route): Route {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can extract this type to use in both places:

type CustomRouteProcessorFn = (route: Route) => Route;

const children = r.children && r.children.map(x => standardizeConfig(x, customRouteProcessor));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we can use Optional chaining for this one.

Suggested change
const children = r.children && r.children.map(x => standardizeConfig(x, customRouteProcessor));
const children = r.children?.map(x => standardizeConfig(x, customRouteProcessor));

const c = children ? {...r, children} : {...r};
if ((!c.component && !c.loadComponent) && (children || c.loadChildren) &&
(c.outlet && c.outlet !== PRIMARY_OUTLET)) {
c.component = EmptyOutletComponent;
}

if (customRouteProcessor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can check if this variable is a function instead

if (typeof customRouteProcessor === 'function') {}

return customRouteProcessor(c);
}

return c;
}

Expand Down
0