-
Notifications
You must be signed in to change notification settings - Fork 0
/
getConfigVariableResult.ts
45 lines (44 loc) · 1.63 KB
/
getConfigVariableResult.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
import {Err, Ok, Result} from '@luolapeikko/result-option';
import {IConfigLoader, IConfigParser} from './interfaces';
import {ConfigOptions} from './ConfigOptions';
import {FormatParameters} from './lib/formatUtils';
import {getConfigVariable} from './getConfigVariable';
import {Loadable} from './types/Loadable';
/**
* @example
* // from "@avanio/variable-util-node"
* const portResult: Result<string> = await getConfigVariableResult('PORT', [env(), fileEnv()], stringParser, '8080', {showValue: true});
*
* const value: string = portResult.unwrap(); // get value or throw error
* const value: string | undefined = portResult.ok(); // get value or undefined
*/
export async function getConfigVariableResult<Output>(
rootKey: string,
loaders: IConfigLoader[],
parser: IConfigParser<Output, unknown>,
defaultValueLoadable: Loadable<Output>,
params?: FormatParameters,
options?: ConfigOptions,
): Promise<Result<Output>>;
export async function getConfigVariableResult<Output>(
rootKey: string,
loaders: IConfigLoader[],
parser: IConfigParser<Output, unknown>,
defaultValueLoadable?: Loadable<Output> | undefined,
params?: FormatParameters,
options?: ConfigOptions,
): Promise<Result<Output | undefined>>;
export async function getConfigVariableResult<Output>(
rootKey: string,
loaders: IConfigLoader[],
parser: IConfigParser<Output, unknown>,
defaultValueLoadable?: Loadable<Output> | undefined,
params?: FormatParameters,
options?: ConfigOptions,
): Promise<Result<Output | undefined>> {
try {
return Ok(await getConfigVariable(rootKey, loaders, parser, defaultValueLoadable, params, options));
} catch (err) {
return Err(err);
}
}