-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(context): add
@bind
to decorate classes with more information
- Loading branch information
1 parent
1def521
commit b8f9792
Showing
12 changed files
with
980 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/context | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {ClassDecoratorFactory} from '@loopback/metadata'; | ||
import { | ||
asBindingTemplate, | ||
asProvider, | ||
BindingMetadata, | ||
BindingSpec, | ||
BINDING_METADATA_KEY, | ||
isProviderClass, | ||
asClassOrProvider, | ||
removeNameAndKeyTags, | ||
} from './binding-inspector'; | ||
import {Provider} from './provider'; | ||
import {Constructor} from './value-promise'; | ||
|
||
/** | ||
* Decorator factory for `@bind` | ||
*/ | ||
class BindDecoratorFactory extends ClassDecoratorFactory<BindingMetadata> { | ||
mergeWithInherited(inherited: BindingMetadata, target: Function) { | ||
if (inherited) { | ||
return { | ||
templates: [ | ||
...inherited.templates, | ||
removeNameAndKeyTags, | ||
...this.spec.templates, | ||
], | ||
target: this.spec.target, | ||
}; | ||
} else { | ||
this.withTarget(this.spec, target); | ||
return this.spec; | ||
} | ||
} | ||
|
||
withTarget(spec: BindingMetadata, target: Function) { | ||
spec.target = target as Constructor<unknown>; | ||
return spec; | ||
} | ||
} | ||
|
||
/** | ||
* Decorate a class with binding configuration | ||
* | ||
* @example | ||
* ```ts | ||
* @bind((binding) => {binding.inScope(BindingScope.SINGLETON).tag('controller')} | ||
* ) | ||
* export class MyController { | ||
* } | ||
* ``` | ||
* | ||
* @param specs A list of binding scope/tags or template functions to | ||
* configure the binding | ||
*/ | ||
export function bind(...specs: BindingSpec[]): ClassDecorator { | ||
const templateFunctions = specs.map(t => { | ||
if (typeof t === 'function') { | ||
return t; | ||
} else { | ||
return asBindingTemplate(t); | ||
} | ||
}); | ||
|
||
return (target: Function) => { | ||
const cls = target as Constructor<unknown>; | ||
const spec: BindingMetadata = { | ||
templates: [asClassOrProvider(cls), ...templateFunctions], | ||
target: cls, | ||
}; | ||
|
||
const decorator = BindDecoratorFactory.createDecorator( | ||
BINDING_METADATA_KEY, | ||
spec, | ||
); | ||
decorator(target); | ||
}; | ||
} | ||
|
||
export namespace bind { | ||
/** | ||
* `@bind.provider` to denote a provider class | ||
* | ||
* A list of binding scope/tags or template functions to configure the binding | ||
*/ | ||
export function provider( | ||
...specs: BindingSpec[] | ||
): ((target: Constructor<Provider<unknown>>) => void) { | ||
return (target: Constructor<Provider<unknown>>) => { | ||
if (!isProviderClass(target)) { | ||
throw new Error(`Target ${target} is not a Provider`); | ||
} | ||
bind( | ||
// Set up the default for providers | ||
asProvider(target), | ||
// Call other template functions | ||
...specs, | ||
)(target); | ||
}; | ||
} | ||
} |
Oops, something went wrong.