UNPKG

3.36 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2017,2019. All Rights Reserved.
2// Node module: @loopback/core
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {
7 Binding,
8 BoundValue,
9 Constructor,
10 createBindingFromClass,
11 Provider,
12} from '@loopback/context';
13import {
14 Application,
15 ControllerClass,
16 ServiceOrProviderClass,
17} from './application';
18import {LifeCycleObserver} from './lifecycle';
19import {Server} from './server';
20
21/**
22 * A map of provider classes to be bound to a context
23 */
24export interface ProviderMap {
25 [key: string]: Constructor<Provider<BoundValue>>;
26}
27
28/**
29 * A map of classes to be bound to a context
30 */
31export interface ClassMap {
32 [key: string]: Constructor<BoundValue>;
33}
34
35/**
36 * A component declares a set of artifacts so that they can be contributed to
37 * an application as a group
38 */
39export interface Component {
40 /**
41 * An array of controller classes
42 */
43 controllers?: ControllerClass[];
44
45 /**
46 * A map of providers to be bound to the application context
47 *
48 * @example
49 * ```ts
50 * {
51 * 'authentication.strategies.ldap': LdapStrategyProvider
52 * }
53 * ```
54 */
55 providers?: ProviderMap;
56
57 /**
58 * A map of classes to be bound to the application context.
59 *
60 * @example
61 * ```ts
62 * {
63 * 'rest.body-parsers.xml': XmlBodyParser
64 * }
65 * ```
66 */
67 classes?: ClassMap;
68
69 /**
70 * A map of name/class pairs for servers
71 */
72 servers?: {
73 [name: string]: Constructor<Server>;
74 };
75
76 lifeCycleObservers?: Constructor<LifeCycleObserver>[];
77
78 /**
79 * An array of service or provider classes
80 */
81 services?: ServiceOrProviderClass[];
82
83 /**
84 * An array of bindings to be aded to the application context.
85 *
86 * @example
87 * ```ts
88 * const bindingX = Binding.bind('x').to('Value X');
89 * this.bindings = [bindingX]
90 * ```
91 */
92 bindings?: Binding[];
93
94 /**
95 * Other properties
96 */
97 // eslint-disable-next-line @typescript-eslint/no-explicit-any
98 [prop: string]: any;
99}
100
101/**
102 * Mount a component to an Application.
103 *
104 * @param app - Application
105 * @param component - Component instance
106 */
107export 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}