Skip to content

Commit 9299dea

Browse files
cexbrayatangular-robot[bot]
authored andcommittedNov 28, 2022
feat(@schematics/angular): generate functional interceptors
Adds a `--functional` flag to the interceptor schematic to generate a functional interceptor (as we can now do for guards and resolvers)
1 parent 534921c commit 9299dea

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { <%= camelize(name) %>Interceptor } from './<%= dasherize(name) %>.interceptor';
4+
5+
describe('<%= camelize(name) %>Interceptor', () => {
6+
const interceptor: HttpInterceptorFn = (req, next) =>
7+
TestBed.runInInjectionContext(() => <%= camelize(name) %>Interceptor(req, next));
8+
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({});
11+
});
12+
13+
it('should be created', () => {
14+
expect(interceptor).toBeTruthy();
15+
});
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { HttpInterceptorFn } from '@angular/common/http';
2+
3+
export const <%= camelize(name) %>Interceptor: HttpInterceptorFn = (req, next) => {
4+
return next(req);
5+
}

‎packages/schematics/angular/interceptor/index.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ export default function (options: InterceptorOptions): Rule {
1414
// This schematic uses an older method to implement the flat option
1515
const flat = options.flat;
1616
options.flat = true;
17-
18-
return generateFromFiles(options, {
17+
const extraTemplateValues = {
1918
'if-flat': (s: string) => (flat ? '' : s),
20-
});
19+
};
20+
21+
return options.functional
22+
? generateFromFiles(
23+
{ ...options, templateFilesDirectory: './functional-files' },
24+
extraTemplateValues,
25+
)
26+
: generateFromFiles(
27+
{ ...options, templateFilesDirectory: './class-files' },
28+
extraTemplateValues,
29+
);
2130
}

‎packages/schematics/angular/interceptor/index_spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,23 @@ describe('Interceptor Schematic', () => {
7474
.toPromise();
7575
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.interceptor.ts');
7676
});
77+
78+
it('should create a functional interceptor', async () => {
79+
const tree = await schematicRunner
80+
.runSchematicAsync('interceptor', { ...defaultOptions, functional: true }, appTree)
81+
.toPromise();
82+
const fileString = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.ts');
83+
expect(fileString).toContain(
84+
'export const fooInterceptor: HttpInterceptorFn = (req, next) => {',
85+
);
86+
});
87+
88+
it('should create a helper function to run a functional interceptor in a test', async () => {
89+
const tree = await schematicRunner
90+
.runSchematicAsync('interceptor', { ...defaultOptions, functional: true }, appTree)
91+
.toPromise();
92+
const fileString = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.spec.ts');
93+
expect(fileString).toContain('const interceptor: HttpInterceptorFn = (req, next) => ');
94+
expect(fileString).toContain('TestBed.runInInjectionContext(() => fooInterceptor(req, next));');
95+
});
7796
});

‎packages/schematics/angular/interceptor/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
"type": "boolean",
4141
"description": "Do not create \"spec.ts\" test files for the new interceptor.",
4242
"default": false
43+
},
44+
"functional": {
45+
"type": "boolean",
46+
"description": "Creates the interceptor as a `HttpInterceptorFn`.",
47+
"default": false
4348
}
4449
},
4550
"required": ["name", "project"]

0 commit comments

Comments
 (0)
Please sign in to comment.