-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
injection-without-binding.ts
44 lines (37 loc) · 1.07 KB
/
injection-without-binding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/example-context
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {BindingKey, Context, inject, instantiateClass} from '@loopback/context';
/**
* Strongly-typed binding key for the current user
*/
const CURRENT_USER = BindingKey.create<string>('currentUser');
/**
* A class with dependency injection
*/
class Greeter {
constructor(@inject(CURRENT_USER) private userName: string) {}
hello() {
return `Hello, ${this.userName}`;
}
}
/**
* Create an instance for `Greeter` with the given context and run `hello()`
* @param ctx Context object
*/
async function sayHello(ctx: Context) {
const greeter = await instantiateClass(Greeter, ctx);
console.log(greeter.hello());
}
export async function main() {
const ctx = new Context('invocation-context');
ctx.bind(CURRENT_USER).to('John');
await sayHello(ctx);
}
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}