-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
types.ts
66 lines (60 loc) · 1.82 KB
/
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
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-greeter-extension
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
BindingScope,
BindingTemplate,
filterByTag,
inject,
} from '@loopback/context';
/**
* Typically an extension point defines an interface as the contract for
* extensions to implement
*/
export interface Greeter {
language: string;
greet(name: string): string;
}
/**
* A factory function to create binding template for extensions of the given
* extension point
* @param extensionPoint Name/id of the extension point
*/
export function extensionFor(extensionPoint: string): BindingTemplate {
return binding =>
binding.inScope(BindingScope.SINGLETON).tag({extensionPoint});
}
/**
* A binding template for greeter extensions
*/
export const asGreeter: BindingTemplate = extensionFor('greeter');
/**
* Shortcut to inject extensions for the given extension point. To be promoted
* as `@extensions` in `@loopback/core` module.
*
* @param extensionPoint Name/id of the extension point
*/
export function extensions(extensionPoint: string) {
return inject.getter(filterByTag({extensionPoint}));
}
/**
* Shortcut to inject configuration for the target binding. To be promoted
* as `@inject.config` in `@loopback/context` module.
*/
export function configuration() {
return inject(
'',
{decorator: '@inject.config', optional: true},
(ctx, injection, session?) => {
if (!session) return undefined;
// Find the key of the target binding
if (!session.currentBinding) return undefined;
const key = session.currentBinding!.key;
return ctx.get(`${key}.options`, {
session,
optional: injection.metadata && injection.metadata.optional,
});
},
);
}