-
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.
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
30e871f
commit d00fb74
Showing
4 changed files
with
86 additions
and
0 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,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 | ||
|
||
|
||
/** | ||
* Interface definition for a MetaValueProvider | ||
* | ||
* MetaValueProvider provisions injecting values via the constructor and | ||
* returns a computed value (Object or Function) when the value() function is called | ||
* | ||
* @example: | ||
* ```ts | ||
* export class MyProvider implements MetaValueProvider { | ||
* constructor(@inject('value') param){} | ||
* | ||
* static key = 'MY_KEY'; | ||
* | ||
* value() { | ||
* return computed_value; | ||
* } | ||
* } | ||
* | ||
* ``` | ||
*/ | ||
export interface MetaValueProvider { | ||
key: string; | ||
value(): Object | Function; | ||
} |
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,36 @@ | ||
// 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 {MetaValueProvider} from '../../src'; | ||
|
||
describe('MetaValueProvider', () => { | ||
let provider: MetaValueProvider; | ||
beforeEach(givenProvider); | ||
|
||
describe('key', () => { | ||
it('identifies the provider', () => { | ||
expect(provider.key).to.equal('MY_KEY'); | ||
}); | ||
}); | ||
|
||
describe('value()', () => { | ||
it('returns the value synchronously', () => { | ||
expect(provider.value()).to.equal('hello world'); | ||
}); | ||
}); | ||
|
||
function givenProvider() { | ||
provider = new MyProvider('hello'); | ||
} | ||
}); | ||
|
||
class MyProvider implements MetaValueProvider { | ||
constructor(private _msg : string) {} | ||
key = 'MY_KEY'; | ||
value() { | ||
return this._msg + ' world'; | ||
} | ||
} |