-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
interceptor-proxy.ts
117 lines (101 loc) · 2.9 KB
/
interceptor-proxy.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
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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 {
asGlobalInterceptor,
AsyncProxy,
bind,
BindingKey,
BindingScope,
Context,
createBindingFromClass,
inject,
Interceptor,
InvocationContext,
Provider,
ValueOrPromise,
} from '@loopback/context';
type RequestIdGenerator = (requestCtx: Context) => string;
const REQUEST_CONTEXT = BindingKey.create<Context>('request.context');
const REQUEST_ID = BindingKey.create<string>('tracing.requestId');
const REQUEST_ID_GENERATOR = BindingKey.create<RequestIdGenerator>(
'tracing.requestIdGenerator',
);
const TRACING_INTERCEPTOR = BindingKey.create<Interceptor>(
'tracing.interceptor',
);
const CONVERTER = BindingKey.create<Converter>('converter');
const GREETER = BindingKey.create<Greeter>('greeter');
@bind(asGlobalInterceptor('tracing'))
class TracingInterceptor implements Provider<Interceptor> {
constructor(
@inject(REQUEST_ID_GENERATOR) private generator: RequestIdGenerator,
) {}
value() {
return this.intercept.bind(this);
}
async intercept<T>(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<T>,
) {
const reqCtx = invocationCtx.getSync(REQUEST_CONTEXT);
let reqId = await reqCtx.get(REQUEST_ID, {optional: true});
if (!reqId) {
reqId = this.generator(reqCtx);
// We need to set request id to `reqCtx` as `invocationCtx` is transient
reqCtx.bind(REQUEST_ID).to(reqId);
console.log(
'Adding request id at %s: %s',
invocationCtx.targetName,
reqId,
);
} else {
console.log(
'Request id found at %s: %s',
invocationCtx.targetName,
reqId,
);
}
return await next();
}
}
class Converter {
toUpperCase(name: string) {
return name.toUpperCase();
}
}
class Greeter {
@inject(CONVERTER, {asProxyWithInterceptors: true})
private converter: AsyncProxy<Converter>;
async greet(name: string) {
const msg = await this.converter.toUpperCase(name);
return `Hello, ${msg}`;
}
}
export async function main() {
const ctx = new Context('request');
// Bind request context
ctx.bind(REQUEST_CONTEXT).to(ctx);
const binding = createBindingFromClass(TracingInterceptor, {
key: TRACING_INTERCEPTOR,
});
ctx.add(binding);
let count = 1;
const reqUuidGenerator: RequestIdGenerator = context =>
`[${context.name}] ${count++}`;
ctx.bind(REQUEST_ID_GENERATOR).to(reqUuidGenerator);
ctx.bind(GREETER).toClass(Greeter);
ctx
.bind(CONVERTER)
.toClass(Converter)
.tag(BindingScope.SINGLETON);
const greeter = await ctx.get(GREETER, {asProxyWithInterceptors: true});
console.log(await greeter!.greet('John'));
}
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}