Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bind a meta value provider to a context
* Create a MetaValueProvider interface * Add Binding.toProvider() to bind a MetaValueProvider * return provider.value() asynchronously thru Binding.getValue()
- Loading branch information
1 parent
0185602
commit 214cf8f
Showing
5 changed files
with
162 additions
and
2 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,35 @@ | ||
// Copyright IBM Corp. 2013,2017. All Rights Reserved. | ||
// Node module: loopback | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {ValueOrPromise} from './binding'; | ||
|
||
/** | ||
* @exports Provider<T> : interface definition for a provider of a value of type T | ||
* @summary Providers allow binding of a value provider class instead of the value itself | ||
* @example: | ||
* ```ts | ||
* export class DateProvider implements Provider<Date> { | ||
* constructor(@inject('stringDate') private param: String){} | ||
* value(): Date { | ||
* return new Date(param); | ||
* } | ||
* } | ||
* ``` | ||
* @example: Binding a context | ||
* ```ts | ||
* ctx.bind('provider_key').toProvider(MyProvider); | ||
* ``` | ||
* @example: getting a value dynamically | ||
* ```ts | ||
* ctx.get('provider_key'); | ||
* ctx.getBinding('provider_key').getValue(); | ||
* ``` | ||
*/ | ||
export interface Provider<T> { | ||
/** | ||
* @returns a value or a promise | ||
*/ | ||
value(): ValueOrPromise<T>; | ||
} |
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,30 @@ | ||
// Copyright IBM Corp. 2013,2017. All Rights Reserved. | ||
// Node module: loopback | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {expect} from '@loopback/testlab'; | ||
import {Provider, Context, Binding} from '../../src'; | ||
|
||
describe('Provider', () => { | ||
let provider: Provider<String>; | ||
|
||
beforeEach(givenProvider); | ||
|
||
describe('value()', () => { | ||
it('returns the value of the binding', () => { | ||
expect(provider.value()).to.equal('hello world'); | ||
}); | ||
}); | ||
|
||
function givenProvider() { | ||
provider = new MyProvider('hello'); | ||
} | ||
}); | ||
|
||
class MyProvider implements Provider<String> { | ||
constructor(private _msg: string) {} | ||
value(): String { | ||
return this._msg + ' world'; | ||
} | ||
} |