-
Notifications
You must be signed in to change notification settings - Fork 0
/
getConfigObjectResult.ts
54 lines (53 loc) · 2.06 KB
/
getConfigObjectResult.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
import {Err, Ok, Result} from '@luolapeikko/result-option';
import {IConfigLoader, IConfigParser} from './interfaces';
import {ConfigOptions} from './ConfigOptions';
import {FormatParameters} from './lib/formatUtils';
import {getConfigObject} from './getConfigObject';
import {Loadable} from './types/Loadable';
/**
* Wrapper around getConfigObject that returns a Result
* @param rootKey root key of config object
* @param loaders array of loaders
* @param parser parser for value
* @param defaultValueLoadable optional default value
* @param params optional format parameters
* @example
* // from "@avanio/variable-util-node"
* const fileEnv = new FileConfigLoader({fileName: './settings.json', type: 'json'}).getLoader;
* const portConfig: Result<{type: string | undefined; value: string}> = await getConfigObject('PORT', [env(), fileEnv()], stringParser, '8080', {showValue: true});
* if ( portConfig.isOk() ) {
* const {value, type} = portConfig.unwrap();
* } else {
* // handle error
* }
*/
export async function getConfigObjectResult<Output>(
rootKey: string,
loaders: IConfigLoader[],
parser: IConfigParser<Output, unknown>,
defaultValueLoadable: Loadable<Output>,
params?: FormatParameters,
options?: ConfigOptions,
): Promise<Result<{type: string | undefined; value: Output}>>;
export async function getConfigObjectResult<Output>(
rootKey: string,
loaders: IConfigLoader[],
parser: IConfigParser<Output, unknown>,
defaultValueLoadable?: Loadable<Output> | undefined,
params?: FormatParameters,
options?: ConfigOptions,
): Promise<Result<{type: string | undefined; value: Output | undefined}>>;
export async function getConfigObjectResult<Output>(
rootKey: string,
loaders: IConfigLoader[],
parser: IConfigParser<Output, unknown>,
defaultValueLoadable?: Loadable<Output> | undefined,
params?: FormatParameters,
options?: ConfigOptions,
): Promise<Result<{type: string | undefined; value: Output | undefined}>> {
try {
return Ok(await getConfigObject(rootKey, loaders, parser, defaultValueLoadable, params, options));
} catch (err) {
return Err(err);
}
}