-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
custom-configuration-resolver.ts
85 lines (76 loc) · 2.14 KB
/
custom-configuration-resolver.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
// 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 {
BindingAddress,
ConfigurationResolver,
Context,
ContextBindings,
DefaultConfigurationResolver,
inject,
ResolutionOptions,
ValueOrPromise,
} from '@loopback/context';
/**
* A custom configuration resolver that looks up env variables first
*/
class EnvConfigResolver extends DefaultConfigurationResolver
implements ConfigurationResolver {
constructor(@inject.context() public readonly context: Context) {
super(context);
}
/**
* Try to find a matching env variable (case insensitive)
* @param key The binding key
*/
private getFromEnvVars(key: string) {
let val;
let found;
for (const k in process.env) {
if (k.toUpperCase() === key.toUpperCase()) {
val = process.env[k];
found = k;
break;
}
}
if (val == null) return val;
console.log(
'Loading configuration for binding "%s" from env variable "%s"',
key,
found,
);
try {
return JSON.parse(val);
} catch (err) {
return val;
}
}
getConfigAsValueOrPromise<ConfigValueType>(
key: BindingAddress<unknown>,
configPath?: string,
resolutionOptions?: ResolutionOptions,
): ValueOrPromise<ConfigValueType | undefined> {
const val = this.getFromEnvVars(key.toString());
if (val != null) return val;
return super.getConfigAsValueOrPromise(key, configPath, resolutionOptions);
}
}
export async function main() {
const ctx = new Context();
ctx.bind(ContextBindings.CONFIGURATION_RESOLVER).toClass(EnvConfigResolver);
// Configure `foo` with `{bar: 'abc'}`
// To override it with env var, use `foo='{"bar":"abc"}'`.
ctx.configure('foo').to({bar: 'abc'});
const fooConfig = await ctx.getConfig('foo');
console.log(fooConfig);
ctx.configure('bar').to('xyz');
const barConfig = ctx.getConfigSync('bar');
console.log(barConfig);
}
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}