-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
binding-types.ts
104 lines (83 loc) · 2.65 KB
/
binding-types.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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, Provider} from '@loopback/context';
/**
* A strongly-typed binding key for current date
*/
const CURRENT_DATE = BindingKey.create<Date>('currentDate');
/**
* A strongly-typed binding key for `Greeter`
*/
const GREETER = BindingKey.create<Greeter>('greeter');
/**
* A factory function to return the current date
*/
const getCurrentDate = () => new Date();
/**
* A class with dependency injection
*/
class Greeter {
// Constructor based injection
constructor(@inject('currentUser') private userName: string) {}
// Property based injection
@inject(CURRENT_DATE)
private created: Date;
// Property based injection
@inject('requestId')
private requestId: string;
hello() {
return `[${this.created.toISOString()}] (${this.requestId}) Hello, ${
this.userName
}`;
}
}
/**
* A provider is similar as the factory function but it requires dependency
* injection. As a result, it's declared as a class with `@inject.*` decorations
* applied.
*/
class RequestIdProvider implements Provider<string> {
static ids: Map<string, number> = new Map();
// Injection of `url`
constructor(@inject('url') private url: string) {}
// This method returns the resolved value for the binding
value() {
let id = RequestIdProvider.ids.get(this.url);
if (id == null) {
id = 1;
} else {
id++;
}
RequestIdProvider.ids.set(this.url, id);
return `${this.url}#${id}`;
}
}
export async function main() {
const ctx = new Context('request');
// Set the current user to `John` (a constant value)
ctx.bind('currentUser').to('John');
// Set current url
ctx.bind('url').to('/greet');
// Set the current date to a factory function that creates the value
ctx.bind(CURRENT_DATE).toDynamicValue(getCurrentDate);
// Bind `hello` to a class from which the value is instantiated
ctx.bind(GREETER).toClass(Greeter);
// Set `requestId` to a provider class which has a `value()` method to
// create the value. It's a specializd factory declared as a class to allow
// dependency injection
ctx.bind('requestId').toProvider(RequestIdProvider);
// Bind `hello` as an alias to `GREETER`
ctx.bind('hello').toAlias(GREETER);
let greeter = await ctx.get(GREETER);
console.log(greeter.hello());
greeter = await ctx.get<Greeter>('hello');
console.log(greeter.hello());
}
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}