-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(core): improve component bindings #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
db9bd6d
199a094
8075c52
cc96229
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,24 @@ | |
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Constructor, Provider, BoundValue} from '@loopback/context'; | ||
| import {Constructor, Provider, BoundValue, Binding} from '@loopback/context'; | ||
| import {Server} from './server'; | ||
| import {Application, ControllerClass} from './application'; | ||
|
|
||
| /** | ||
| * A map of name/class pairs for binding providers | ||
| * A map of provider classes to be bound to a context | ||
| */ | ||
| export interface ProviderMap { | ||
| [key: string]: Constructor<Provider<BoundValue>>; | ||
| } | ||
|
|
||
| /** | ||
| * A map of classes to be bound to a context | ||
| */ | ||
| export interface ClassMap { | ||
| [key: string]: Constructor<BoundValue>; | ||
| } | ||
|
|
||
| /** | ||
| * A component declares a set of artifacts so that they cane be contributed to | ||
| * an application as a group | ||
|
|
@@ -23,17 +30,46 @@ export interface Component { | |
| * An array of controller classes | ||
| */ | ||
| controllers?: ControllerClass[]; | ||
|
|
||
| /** | ||
| * A map of name/class pairs for binding providers | ||
| * A map of providers to be bound to the application context | ||
| * * For example: | ||
| * ```ts | ||
| * { | ||
| * 'authentication.strategies.ldap': LdapStrategyProvider | ||
| * } | ||
| * ``` | ||
| */ | ||
| providers?: ProviderMap; | ||
|
|
||
| /** | ||
| * A map of classes to be bound to the application context. | ||
| * | ||
| * For example: | ||
| * ```ts | ||
| * { | ||
| * 'rest.body-parsers.xml': XmlBodyParser | ||
| * } | ||
| * ``` | ||
| */ | ||
| classes?: ClassMap; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this name confusing. What does it mean for a component to export a class? Some classes are exported via JavaScript/TypeScript exports, e.g. Personally, I prefer to remove this API entirely, keep If you feel it's important to keep this shortcut, then let's find a better name please. For example:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added jsdocs to make it clear.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am afraid jsdocs are not good enough. Consider the following code snippet from your pull request: class MyComponentWithClasses implements Component {
classes = {'my-class': MyClass};
}The person reading the code (e.g. while reviewing a pull request on GitHub) does not see tscode comments. |
||
|
|
||
| /** | ||
| * A map of name/class pairs for servers | ||
| */ | ||
| servers?: { | ||
| [name: string]: Constructor<Server>; | ||
| }; | ||
|
|
||
| /** | ||
| * An array of bindings to be aded to the application context. For example, | ||
| * ```ts | ||
| * const bindingX = Binding.bind('x').to('Value X'); | ||
| * this.bindings = [bindingX] | ||
| * ``` | ||
| */ | ||
| bindings?: Binding[]; | ||
|
|
||
| /** | ||
| * Other properties | ||
| */ | ||
|
|
@@ -49,9 +85,9 @@ export interface Component { | |
| * @param {Component} component | ||
| */ | ||
| export function mountComponent(app: Application, component: Component) { | ||
| if (component.controllers) { | ||
| for (const controllerCtor of component.controllers) { | ||
| app.controller(controllerCtor); | ||
| if (component.classes) { | ||
| for (const classKey in component.classes) { | ||
| app.bind(classKey).toClass(component.classes[classKey]); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -61,6 +97,18 @@ export function mountComponent(app: Application, component: Component) { | |
| } | ||
| } | ||
|
|
||
| if (component.bindings) { | ||
| for (const binding of component.bindings) { | ||
| app.add(binding); | ||
| } | ||
| } | ||
|
|
||
| if (component.controllers) { | ||
| for (const controllerCtor of component.controllers) { | ||
| app.controller(controllerCtor); | ||
| } | ||
| } | ||
|
|
||
| if (component.servers) { | ||
| for (const serverKey in component.servers) { | ||
| app.server(component.servers[serverKey], serverKey); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.