1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {
|
7 | Application,
|
8 | config,
|
9 | Constructor,
|
10 | CoreBindings,
|
11 | inject,
|
12 | Interceptor,
|
13 | Provider,
|
14 | } from '@loopback/core';
|
15 | import debugFactory from 'debug';
|
16 | import {BootBindings} from '../keys';
|
17 | import {ArtifactOptions, booter} from '../types';
|
18 | import {BaseArtifactBooter} from './base-artifact.booter';
|
19 |
|
20 | const debug = debugFactory('loopback:boot:interceptor-booter');
|
21 |
|
22 | type InterceptorProviderClass = Constructor<Provider<Interceptor>>;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | @booter('interceptors')
|
34 | export class InterceptorProviderBooter extends BaseArtifactBooter {
|
35 | interceptors: InterceptorProviderClass[];
|
36 |
|
37 | constructor(
|
38 | @inject(CoreBindings.APPLICATION_INSTANCE)
|
39 | public app: Application,
|
40 | @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
|
41 | @config()
|
42 | public interceptorConfig: ArtifactOptions = {},
|
43 | ) {
|
44 | super(
|
45 | projectRoot,
|
46 |
|
47 | Object.assign({}, InterceptorProviderDefaults, interceptorConfig),
|
48 | );
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 | async load() {
|
56 | await super.load();
|
57 |
|
58 | this.interceptors = this.classes as InterceptorProviderClass[];
|
59 | for (const interceptor of this.interceptors) {
|
60 | debug('Bind interceptor: %s', interceptor.name);
|
61 | const binding = this.app.interceptor(interceptor);
|
62 | debug('Binding created for interceptor: %j', binding);
|
63 | }
|
64 | }
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export const InterceptorProviderDefaults: ArtifactOptions = {
|
71 | dirs: ['interceptors'],
|
72 | extensions: ['.interceptor.js'],
|
73 | nested: true,
|
74 | };
|