UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/context
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.bind = exports.injectable = void 0;
8const metadata_1 = require("@loopback/metadata");
9const binding_inspector_1 = require("./binding-inspector");
10/**
11 * Decorator factory for `@injectable`
12 */
13class InjectableDecoratorFactory extends metadata_1.ClassDecoratorFactory {
14 mergeWithInherited(inherited, target) {
15 if (inherited) {
16 return {
17 templates: [
18 ...inherited.templates,
19 binding_inspector_1.removeNameAndKeyTags,
20 ...this.spec.templates,
21 ],
22 target: this.spec.target,
23 };
24 }
25 else {
26 this.withTarget(this.spec, target);
27 return this.spec;
28 }
29 }
30 mergeWithOwn(ownMetadata) {
31 return {
32 templates: [...ownMetadata.templates, ...this.spec.templates],
33 target: this.spec.target,
34 };
35 }
36 withTarget(spec, target) {
37 spec.target = target;
38 return spec;
39 }
40}
41/**
42 * Decorate a class with binding configuration
43 *
44 * @example
45 * ```ts
46 * @injectable((binding) => {binding.inScope(BindingScope.SINGLETON).tag('controller')}
47 * )
48 * @injectable({scope: BindingScope.SINGLETON})
49 * export class MyController {
50 * }
51 * ```
52 *
53 * @param specs - A list of binding scope/tags or template functions to
54 * configure the binding
55 */
56function injectable(...specs) {
57 const templateFunctions = specs.map(t => {
58 if (typeof t === 'function') {
59 return t;
60 }
61 else {
62 return (0, binding_inspector_1.asBindingTemplate)(t);
63 }
64 });
65 return (target) => {
66 const cls = target;
67 const spec = {
68 templates: [(0, binding_inspector_1.asClassOrProvider)(cls), ...templateFunctions],
69 target: cls,
70 };
71 const decorator = InjectableDecoratorFactory.createDecorator(binding_inspector_1.BINDING_METADATA_KEY, spec, { decoratorName: '@injectable' });
72 decorator(target);
73 };
74}
75exports.injectable = injectable;
76/**
77 * A namespace to host shortcuts for `@injectable`
78 */
79(function (injectable) {
80 /**
81 * `@injectable.provider` to denote a provider class
82 *
83 * A list of binding scope/tags or template functions to configure the binding
84 */
85 function provider(...specs) {
86 return (target) => {
87 if (!(0, binding_inspector_1.isProviderClass)(target)) {
88 throw new Error(`Target ${target} is not a Provider`);
89 }
90 injectable(
91 // Set up the default for providers
92 (0, binding_inspector_1.asProvider)(target),
93 // Call other template functions
94 ...specs)(target);
95 };
96 }
97 injectable.provider = provider;
98})(injectable = exports.injectable || (exports.injectable = {}));
99/**
100 * `@bind` is now an alias to {@link injectable} for backward compatibility
101 * {@inheritDoc injectable}
102 */
103function bind(...specs) {
104 return injectable(...specs);
105}
106exports.bind = bind;
107/**
108 * Alias namespace `bind` to `injectable` for backward compatibility
109 *
110 * It should have the same members as `bind`.
111 */
112(function (bind) {
113 /**
114 * {@inheritDoc injectable.provider}
115 */
116 bind.provider = injectable.provider;
117})(bind = exports.bind || (exports.bind = {}));
118//# sourceMappingURL=binding-decorator.js.map
\No newline at end of file