UNPKG

1.78 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2018,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 {Application, config, CoreBindings, inject} from '@loopback/core';
7import {BootBindings} from '../keys';
8import {ArtifactOptions, booter} from '../types';
9import {BaseArtifactBooter} from './base-artifact.booter';
10
11/**
12 * A class that extends BaseArtifactBooter to boot the 'Controller' artifact type.
13 * Discovered controllers are bound using `app.controller()`.
14 *
15 * Supported phases: configure, discover, load
16 *
17 * @param app - Application instance
18 * @param projectRoot - Root of User Project relative to which all paths are resolved
19 * @param bootConfig - Controller Artifact Options Object
20 */
21@booter('controllers')
22export class ControllerBooter extends BaseArtifactBooter {
23 constructor(
24 @inject(CoreBindings.APPLICATION_INSTANCE) public app: Application,
25 @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
26 @config()
27 public controllerConfig: ArtifactOptions = {},
28 ) {
29 super(
30 projectRoot,
31 // Set Controller Booter Options if passed in via bootConfig
32 Object.assign({}, ControllerDefaults, controllerConfig),
33 );
34 }
35
36 /**
37 * Uses super method to get a list of Artifact classes. Boot each class by
38 * binding it to the application using `app.controller(controller);`.
39 */
40 async load() {
41 await super.load();
42 this.classes.forEach(cls => {
43 this.app.controller(cls);
44 });
45 }
46}
47
48/**
49 * Default ArtifactOptions for ControllerBooter.
50 */
51export const ControllerDefaults: ArtifactOptions = {
52 dirs: ['controllers'],
53 extensions: ['.controller.js'],
54 nested: true,
55};