1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {
|
7 | Binding,
|
8 | BoundValue,
|
9 | Constructor,
|
10 | createBindingFromClass,
|
11 | Provider,
|
12 | } from '@loopback/context';
|
13 | import {
|
14 | Application,
|
15 | ControllerClass,
|
16 | ServiceOrProviderClass,
|
17 | } from './application';
|
18 | import {LifeCycleObserver} from './lifecycle';
|
19 | import {Server} from './server';
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export interface ProviderMap {
|
25 | [key: string]: Constructor<Provider<BoundValue>>;
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export interface ClassMap {
|
32 | [key: string]: Constructor<BoundValue>;
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export interface Component {
|
40 | |
41 |
|
42 |
|
43 | controllers?: ControllerClass[];
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | providers?: ProviderMap;
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | classes?: ClassMap;
|
68 |
|
69 | |
70 |
|
71 |
|
72 | servers?: {
|
73 | [name: string]: Constructor<Server>;
|
74 | };
|
75 |
|
76 | lifeCycleObservers?: Constructor<LifeCycleObserver>[];
|
77 |
|
78 | |
79 |
|
80 |
|
81 | services?: ServiceOrProviderClass[];
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | bindings?: Binding[];
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 | [prop: string]: any;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | export function mountComponent(app: Application, component: Component) {
|
108 | if (component.classes) {
|
109 | for (const classKey in component.classes) {
|
110 | const binding = createBindingFromClass(component.classes[classKey], {
|
111 | key: classKey,
|
112 | });
|
113 | app.add(binding);
|
114 | }
|
115 | }
|
116 |
|
117 | if (component.providers) {
|
118 | for (const providerKey in component.providers) {
|
119 | const binding = createBindingFromClass(component.providers[providerKey], {
|
120 | key: providerKey,
|
121 | });
|
122 | app.add(binding);
|
123 | }
|
124 | }
|
125 |
|
126 | if (component.bindings) {
|
127 | for (const binding of component.bindings) {
|
128 | app.add(binding);
|
129 | }
|
130 | }
|
131 |
|
132 | if (component.controllers) {
|
133 | for (const controllerCtor of component.controllers) {
|
134 | app.controller(controllerCtor);
|
135 | }
|
136 | }
|
137 |
|
138 | if (component.servers) {
|
139 | for (const serverKey in component.servers) {
|
140 | app.server(component.servers[serverKey], serverKey);
|
141 | }
|
142 | }
|
143 |
|
144 | if (component.lifeCycleObservers) {
|
145 | for (const observer of component.lifeCycleObservers) {
|
146 | app.lifeCycleObserver(observer);
|
147 | }
|
148 | }
|
149 |
|
150 | if (component.services) {
|
151 | for (const service of component.services) {
|
152 | app.service(service);
|
153 | }
|
154 | }
|
155 | }
|