1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {ClassDecoratorFactory} from '@loopback/metadata';
|
7 | import {
|
8 | asBindingTemplate,
|
9 | asClassOrProvider,
|
10 | asProvider,
|
11 | BindingMetadata,
|
12 | BindingSpec,
|
13 | BINDING_METADATA_KEY,
|
14 | isProviderClass,
|
15 | removeNameAndKeyTags,
|
16 | } from './binding-inspector';
|
17 | import {Constructor} from './value-promise';
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class InjectableDecoratorFactory extends ClassDecoratorFactory<BindingMetadata> {
|
23 | mergeWithInherited(inherited: BindingMetadata, target: Function) {
|
24 | if (inherited) {
|
25 | return {
|
26 | templates: [
|
27 | ...inherited.templates,
|
28 | removeNameAndKeyTags,
|
29 | ...this.spec.templates,
|
30 | ],
|
31 | target: this.spec.target,
|
32 | };
|
33 | } else {
|
34 | this.withTarget(this.spec, target);
|
35 | return this.spec;
|
36 | }
|
37 | }
|
38 |
|
39 | mergeWithOwn(ownMetadata: BindingMetadata) {
|
40 | return {
|
41 | templates: [...ownMetadata.templates, ...this.spec.templates],
|
42 | target: this.spec.target,
|
43 | };
|
44 | }
|
45 |
|
46 | withTarget(spec: BindingMetadata, target: Function) {
|
47 | spec.target = target as Constructor<unknown>;
|
48 | return spec;
|
49 | }
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | export function injectable(...specs: BindingSpec[]): ClassDecorator {
|
68 | const templateFunctions = specs.map(t => {
|
69 | if (typeof t === 'function') {
|
70 | return t;
|
71 | } else {
|
72 | return asBindingTemplate(t);
|
73 | }
|
74 | });
|
75 |
|
76 | return (target: Function) => {
|
77 | const cls = target as Constructor<unknown>;
|
78 | const spec: BindingMetadata = {
|
79 | templates: [asClassOrProvider(cls), ...templateFunctions],
|
80 | target: cls,
|
81 | };
|
82 |
|
83 | const decorator = InjectableDecoratorFactory.createDecorator(
|
84 | BINDING_METADATA_KEY,
|
85 | spec,
|
86 | {decoratorName: '@injectable'},
|
87 | );
|
88 | decorator(target);
|
89 | };
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | export namespace injectable {
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 | export function provider(
|
102 | ...specs: BindingSpec[]
|
103 | ): (target: Constructor<unknown>) => void {
|
104 | return (target: Constructor<unknown>) => {
|
105 | if (!isProviderClass(target)) {
|
106 | throw new Error(`Target ${target} is not a Provider`);
|
107 | }
|
108 | injectable(
|
109 |
|
110 | asProvider(target),
|
111 |
|
112 | ...specs,
|
113 | )(target);
|
114 | };
|
115 | }
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export function bind(...specs: BindingSpec[]): ClassDecorator {
|
123 | return injectable(...specs);
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | export namespace bind {
|
132 | |
133 |
|
134 |
|
135 | export const provider = injectable.provider;
|
136 | }
|