Skip to content

Commit b34c719

Browse files
atscottalan-agius4
authored andcommitted
fixup! feat(@schematics/angular): Add schematics for generating functional router guards and resolvers
1 parent 6c39a16 commit b34c719

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

packages/schematics/angular/guard/index.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ import { generateFromFiles } from '../utility/generate-from-files';
1313
import { Implement as GuardInterface, Schema as GuardOptions } from './schema';
1414

1515
export default function (options: GuardOptions): Rule {
16-
if (options.implements && options.implements.length > 0 && options.guardType) {
17-
throw new SchematicsException('Options "implements" and "guardType" cannot be used together.');
16+
if (!options.implements) {
17+
throw new SchematicsException('Option "implements" is required.');
18+
}
19+
if (options.implements.length > 1 && options.functional) {
20+
throw new SchematicsException(
21+
'Can only specify one value for implements when generating a functional guard.',
22+
);
1823
}
1924

20-
if (options.guardType) {
21-
const guardType = options.guardType.replace(/^can/, 'Can') + 'Fn';
25+
if (options.functional) {
26+
const guardType = options.implements[0] + 'Fn';
2227

2328
return generateFromFiles({ ...options, templateFilesDirectory: './type-files' }, { guardType });
2429
} else {
25-
if (!options.implements || options.implements.length < 1) {
26-
options.implements = [GuardInterface.CanActivate];
27-
}
28-
2930
const implementations = options.implements
3031
.map((implement) => (implement === 'CanDeactivate' ? 'CanDeactivate<unknown>' : implement))
3132
.join(', ');

packages/schematics/angular/guard/index_spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ describe('Guard Schematic', () => {
9292
expect(fileString).not.toContain('canLoad');
9393
});
9494

95-
it('should respect the guardType value', async () => {
96-
const options = { ...defaultOptions, guardType: 'canActivate' };
95+
it('should respect the functional guard value', async () => {
96+
const options = { ...defaultOptions, implements: ['CanActivate'], functional: true };
9797
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
9898
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
9999
expect(fileString).toContain('export const fooGuard: CanActivateFn = (route, state) => {');
@@ -104,7 +104,7 @@ describe('Guard Schematic', () => {
104104
});
105105

106106
it('should generate a helper function to execute the guard in a test', async () => {
107-
const options = { ...defaultOptions, guardType: 'canActivate' };
107+
const options = { ...defaultOptions, implements: ['CanActivate'], functional: true };
108108
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
109109
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.spec.ts');
110110
expect(fileString).toContain('const executeGuard: CanActivateFn = (...guardParameters) => ');
@@ -113,8 +113,8 @@ describe('Guard Schematic', () => {
113113
);
114114
});
115115

116-
it('should generate CanDeactivateFn with unknown guardType', async () => {
117-
const options = { ...defaultOptions, guardType: 'canDeactivate' };
116+
it('should generate CanDeactivateFn with unknown functional guard', async () => {
117+
const options = { ...defaultOptions, implements: ['CanDeactivate'], functional: true };
118118
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
119119
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
120120
expect(fileString).toContain(
@@ -157,8 +157,8 @@ describe('Guard Schematic', () => {
157157
expect(fileString).toContain(expectedImports);
158158
});
159159

160-
it('should add correct imports based on canLoad guardType', async () => {
161-
const options = { ...defaultOptions, guardType: 'canLoad' };
160+
it('should add correct imports based on canLoad functional guard', async () => {
161+
const options = { ...defaultOptions, implements: ['CanLoad'], functional: true };
162162
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
163163
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
164164
const expectedImports = `import { CanLoadFn } from '@angular/router';`;
@@ -176,8 +176,8 @@ describe('Guard Schematic', () => {
176176
expect(fileString).toContain(expectedImports);
177177
});
178178

179-
it('should add correct imports based on canActivate guardType', async () => {
180-
const options = { ...defaultOptions, guardType: 'canActivate' };
179+
it('should add correct imports based on canActivate functional guard', async () => {
180+
const options = { ...defaultOptions, implements: ['CanActivate'], functional: true };
181181
const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise();
182182
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
183183
const expectedImports = `import { CanActivateFn } from '@angular/router';`;

packages/schematics/angular/guard/schema.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,22 @@
4141
"$source": "projectName"
4242
}
4343
},
44+
"functional": {
45+
"type": "boolean",
46+
"description": "Specifies whether to generate a guard as a function.",
47+
"default": false
48+
},
4449
"implements": {
4550
"type": "array",
46-
"description": "Specifies which interfaces to implement.",
51+
"description": "Specifies which type of guard to create.",
4752
"uniqueItems": true,
4853
"minItems": 1,
4954
"items": {
5055
"enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad", "CanMatch"],
5156
"type": "string"
52-
}
53-
},
54-
"guardType": {
55-
"type": "string",
56-
"description": "Specifies type of guard to generate.",
57-
"enum": ["canActivate", "canActivateChild", "canDeactivate", "canLoad", "canMatch"]
57+
},
58+
"default": ["CanActivate"],
59+
"x-prompt": "Which type of guard would you like to create?"
5860
}
5961
},
6062
"required": ["name", "project"]

0 commit comments

Comments
 (0)