UNPKG

3.37 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/boot
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.ServiceDefaults = exports.ServiceBooter = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const debug_1 = tslib_1.__importDefault(require("debug"));
11const keys_1 = require("../keys");
12const types_1 = require("../types");
13const base_artifact_booter_1 = require("./base-artifact.booter");
14const debug = (0, debug_1.default)('loopback:boot:service-booter');
15/**
16 * A class that extends BaseArtifactBooter to boot the 'Service' artifact type.
17 * Discovered services are bound using `app.service()`.
18 *
19 * Supported phases: configure, discover, load
20 *
21 * @param app - Application instance
22 * @param projectRoot - Root of User Project relative to which all paths are resolved
23 * @param bootConfig - Service Artifact Options Object
24 */
25let ServiceBooter = class ServiceBooter extends base_artifact_booter_1.BaseArtifactBooter {
26 constructor(app, projectRoot, serviceConfig = {}) {
27 super(projectRoot,
28 // Set Service Booter Options if passed in via bootConfig
29 Object.assign({}, exports.ServiceDefaults, serviceConfig));
30 this.app = app;
31 this.serviceConfig = serviceConfig;
32 }
33 /**
34 * Uses super method to get a list of Artifact classes. Boot each file by
35 * creating a DataSourceConstructor and binding it to the application class.
36 */
37 async load() {
38 await super.load();
39 for (const cls of this.classes) {
40 if (!isBindableClass(cls))
41 continue;
42 debug('Bind class: %s', cls.name);
43 const binding = this.app.service(cls);
44 debug('Binding created for class: %j', binding);
45 }
46 }
47};
48ServiceBooter = tslib_1.__decorate([
49 (0, types_1.booter)('services'),
50 tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
51 tslib_1.__param(1, (0, core_1.inject)(keys_1.BootBindings.PROJECT_ROOT)),
52 tslib_1.__param(2, (0, core_1.config)()),
53 tslib_1.__metadata("design:paramtypes", [Object, String, Object])
54], ServiceBooter);
55exports.ServiceBooter = ServiceBooter;
56/**
57 * Default ArtifactOptions for DataSourceBooter.
58 */
59exports.ServiceDefaults = {
60 dirs: ['services'],
61 extensions: ['.service.js'],
62 nested: true,
63};
64function isServiceProvider(cls) {
65 const hasSupportedName = cls.name.endsWith('Provider');
66 const hasValueMethod = typeof cls.prototype.value === 'function';
67 return hasSupportedName && hasValueMethod;
68}
69function isBindableClass(cls) {
70 if (core_1.MetadataInspector.getClassMetadata(core_1.BINDING_METADATA_KEY, cls)) {
71 return true;
72 }
73 if ((0, core_1.hasInjections)(cls)) {
74 return true;
75 }
76 if (isServiceProvider(cls)) {
77 debug('Provider class found: %s', cls.name);
78 return true;
79 }
80 if ((0, core_1.isDynamicValueProviderClass)(cls)) {
81 debug('Dynamic value provider class found: %s', cls.name);
82 return true;
83 }
84 debug('Skip class not decorated with @injectable: %s', cls.name);
85 return false;
86}
87//# sourceMappingURL=service.booter.js.map
\No newline at end of file