UNPKG

2.29 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
2// Node module: @loopback/boot
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {
7 Application,
8 config,
9 Constructor,
10 CoreBindings,
11 inject,
12 Interceptor,
13 Provider,
14} from '@loopback/core';
15import debugFactory from 'debug';
16import {BootBindings} from '../keys';
17import {ArtifactOptions, booter} from '../types';
18import {BaseArtifactBooter} from './base-artifact.booter';
19
20const debug = debugFactory('loopback:boot:interceptor-booter');
21
22type InterceptorProviderClass = Constructor<Provider<Interceptor>>;
23
24/**
25 * A class that extends BaseArtifactBooter to boot the 'InterceptorProvider' artifact type.
26 *
27 * Supported phases: configure, discover, load
28 *
29 * @param app - Application instance
30 * @param projectRoot - Root of User Project relative to which all paths are resolved
31 * @param bootConfig - InterceptorProvider Artifact Options Object
32 */
33@booter('interceptors')
34export 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 // Set InterceptorProvider Booter Options if passed in via bootConfig
47 Object.assign({}, InterceptorProviderDefaults, interceptorConfig),
48 );
49 }
50
51 /**
52 * Uses super method to get a list of Artifact classes. Boot each file by
53 * creating a DataSourceConstructor and binding it to the application class.
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 * Default ArtifactOptions for InterceptorProviderBooter.
69 */
70export const InterceptorProviderDefaults: ArtifactOptions = {
71 dirs: ['interceptors'],
72 extensions: ['.interceptor.js'],
73 nested: true,
74};