Skip to content
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

feat(router): helper functions to convert class guards to functional #48709

Closed
wants to merge 3 commits 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
25 changes: 25 additions & 0 deletions goldens/public-api/router/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,31 @@ export type LoadChildren = LoadChildrenCallback;
// @public
export type LoadChildrenCallback = () => Type<any> | NgModuleFactory<any> | Routes | Observable<Type<any> | Routes | DefaultExport<Type<any>> | DefaultExport<Routes>> | Promise<NgModuleFactory<any> | Type<any> | Routes | DefaultExport<Type<any>> | DefaultExport<Routes>>;

// @public
export function mapToCanActivate(providers: Array<Type<{
canActivate: CanActivateFn;
}>>): CanActivateFn[];

// @public
export function mapToCanActivateChild(providers: Array<Type<{
canActivateChild: CanActivateChildFn;
}>>): CanActivateChildFn[];

// @public
export function mapToCanDeactivate<T = unknown>(providers: Array<Type<{
canDeactivate: CanDeactivateFn<T>;
}>>): CanDeactivateFn<T>[];

// @public
export function mapToCanMatch(providers: Array<Type<{
canMatch: CanMatchFn;
}>>): CanMatchFn[];

// @public
export function mapToResolve<T>(provider: Type<{
resolve: ResolveFn<T>;
}>): ResolveFn<T>;

// @public
export interface Navigation {
extractedUrl: UrlTree;
Expand Down
3 changes: 0 additions & 3 deletions packages/examples/router/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ ng_module(
deps = [
"//packages/core",
"//packages/platform-browser",
"//packages/platform-browser-dynamic",
"//packages/router",
"//packages/zone.js/lib",
"@npm//rxjs",
],
)

Expand Down
40 changes: 40 additions & 0 deletions packages/examples/router/utils/functional_guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable} from '@angular/core';
import {mapToCanActivate, mapToResolve, Route} from '@angular/router';

// #docregion CanActivate
@Injectable({providedIn: 'root'})
export class AdminGuard {
canActivate() {
return true;
}
}

const route: Route = {
path: 'admin',
canActivate: mapToCanActivate([AdminGuard]),
};
// #enddocregion

// #docregion Resolve
@Injectable({providedIn: 'root'})
export class ResolveUser {
resolve() {
return {name: 'Bob'};
}
}

const userRoute: Route = {
path: 'user',
resolve: {
user: mapToResolve(ResolveUser),
},
};
// #enddocregion
1 change: 1 addition & 0 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot
export {convertToParamMap, defaultUrlMatcher, ParamMap, Params, PRIMARY_OUTLET} from './shared';
export {UrlHandlingStrategy} from './url_handling_strategy';
export {DefaultUrlSerializer, IsActiveMatchOptions, UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
export {mapToCanActivate, mapToCanActivateChild, mapToCanDeactivate, mapToCanMatch, mapToResolve} from './utils/functional_guards';
export {VERSION} from './version';

export * from './private_export';
76 changes: 76 additions & 0 deletions packages/router/src/utils/functional_guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {inject, Type} from '@angular/core';

import {CanActivateChildFn, CanActivateFn, CanDeactivateFn, CanMatchFn, ResolveFn} from '../models';

/**
* Maps an array of injectable classes with canMatch functions to an array of equivalent
* `CanMatchFn` for use in a `Route` definition.
*
* Usage {@example router/utils/functional_guards.ts region='CanActivate'}
*
* @publicApi
* @see Route
*/
export function mapToCanMatch(providers: Array<Type<{canMatch: CanMatchFn}>>): CanMatchFn[] {
return providers.map(provider => (...params) => inject(provider).canMatch(...params));
}

/**
* Maps an array of injectable classes with canActivate functions to an array of equivalent
* `CanActivateFn` for use in a `Route` definition.
*
* Usage {@example router/utils/functional_guards.ts region='CanActivate'}
*
* @publicApi
* @see Route
*/
export function mapToCanActivate(providers: Array<Type<{canActivate: CanActivateFn}>>):
CanActivateFn[] {
return providers.map(provider => (...params) => inject(provider).canActivate(...params));
}
/**
* Maps an array of injectable classes with canActivateChild functions to an array of equivalent
* `CanActivateChildFn` for use in a `Route` definition.
*
* Usage {@example router/utils/functional_guards.ts region='CanActivate'}
*
* @publicApi
* @see Route
*/
export function mapToCanActivateChild(
providers: Array<Type<{canActivateChild: CanActivateChildFn}>>): CanActivateChildFn[] {
return providers.map(provider => (...params) => inject(provider).canActivateChild(...params));
}
/**
* Maps an array of injectable classes with canDeactivate functions to an array of equivalent
* `CanDeactivateFn` for use in a `Route` definition.
*
* Usage {@example router/utils/functional_guards.ts region='CanActivate'}
*
* @publicApi
* @see Route
*/
export function mapToCanDeactivate<T = unknown>(
providers: Array<Type<{canDeactivate: CanDeactivateFn<T>}>>): CanDeactivateFn<T>[] {
return providers.map(provider => (...params) => inject(provider).canDeactivate(...params));
}
/**
* Maps an injectable class with a resolve function to an equivalent `ResolveFn`
* for use in a `Route` definition.
*
* Usage {@example router/utils/functional_guards.ts region='Resolve'}
*
* @publicApi
* @see Route
*/
export function mapToResolve<T>(provider: Type<{resolve: ResolveFn<T>}>): ResolveFn<T> {
return (...params) => inject(provider).resolve(...params);
}