-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@schematics/angular): Add schematics for generating functional r…
…outer guards and resolvers Functional guards and resolvers were introduced in the Angular router in v14.2. This commit adds the ability to generate functional router guards by specifying `--guardType` instead of `--implements`. These guards are also accompanied by a test that includes a helper function for executing the guard in the `TestBed` environment so that any `inject` calls in the guard will work properly. Functional resolvers are generated by adding the `--functional` flag.
- Loading branch information
1 parent
827fecc
commit 6c39a16
Showing
16 changed files
with
186 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...iles/__name@dasherize__.guard.ts.template → ...iles/__name@dasherize__.guard.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/schematics/angular/guard/type-files/__name@dasherize__.guard.spec.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { EnvironmentInjector } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { <%= guardType %> } from '@angular/router'; | ||
|
||
import { <%= camelize(name) %>Guard } from './<%= dasherize(name) %>.guard'; | ||
|
||
describe('<%= camelize(name) %>Guard', () => { | ||
const executeGuard: <%= guardType %> = (...guardParameters) => | ||
TestBed.inject(EnvironmentInjector).runInContext(() => <%= camelize(name) %>Guard(...guardParameters)); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(<%= camelize(name) %>Guard).toBeTruthy(); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/schematics/angular/guard/type-files/__name@dasherize__.guard.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { <%= guardType %> } from '@angular/router'; | ||
|
||
export const <%= camelize(name) %>Guard: <%= guardType %><% if (guardType === 'CanDeactivateFn') { %><unknown><% } %> = <% | ||
if (guardType === 'CanMatchFn' || guardType === 'CanLoadFn') { %>(route, segments)<% } | ||
%><% if (guardType === 'CanActivateFn') { %>(route, state)<% } | ||
%><% if (guardType === 'CanActivateChildFn') { %>(childRoute, state)<% } | ||
%><% if (guardType === 'CanDeactivateFn') { %>(component, currentRoute, currentState, nextState)<% } %> => { | ||
return true; | ||
} |
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
...schematics/angular/resolver/functional-files/__name@dasherize__.resolver.spec.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { ResolveFn } from '@angular/router'; | ||
|
||
import { <%= camelize(name) %>Resolver } from './<%= dasherize(name) %>.resolver'; | ||
|
||
describe('<%= camelize(name) %>Resolver', () => { | ||
const executeResolver: ResolveFn<boolean> = (...resolverParameters) => | ||
TestBed.inject(EnvironmentInjector).runInContext(() => <%= camelize(name) %>Resolver(...resolverParameters)); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(resolver).toBeTruthy(); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
...ages/schematics/angular/resolver/functional-files/__name@dasherize__.resolver.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ResolveFn } from '@angular/router'; | ||
|
||
export const <%= camelize(name) %>Resolver: ResolveFn<boolean> = (route, state) => { | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters