|
| 1 | +import _ from 'lodash'; |
| 2 | + |
| 3 | +import { MissingOptionError, UnknownKeysError } from './errors'; |
| 4 | +import { buildLazyObject, forceParsing } from './lazy'; |
| 5 | +import initLocator from './locator'; |
| 6 | + |
| 7 | +import type { LazyObject } from './types/lazy'; |
| 8 | +import type { RootParsedConfig } from './types/common'; |
| 9 | +import type { MapParser } from './types/map'; |
| 10 | +import type { OptionParser, OptionParserConfig } from './types/option'; |
| 11 | +import type { RootParser, RootPrefixes, ConfigParser } from './types/root'; |
| 12 | +import type { SectionParser, SectionProperties } from './types/section'; |
| 13 | +import type { DeepPartial } from './types/utils'; |
| 14 | +import type { Locator } from './types/locator'; |
| 15 | + |
| 16 | +type Parser<T, R = any> = OptionParser<T, R> | SectionParser<T, R> | MapParser<T, R>; |
| 17 | + |
| 18 | +/** |
| 19 | + * Single option |
| 20 | + */ |
| 21 | +export function option<T, S = T, R = any>({ |
| 22 | + defaultValue, |
| 23 | + parseCli = _.identity, |
| 24 | + parseEnv = _.identity, |
| 25 | + validate = _.noop, |
| 26 | + map: mapFunc = _.identity |
| 27 | +}: OptionParserConfig<T, S, R>): OptionParser<S, R> { |
| 28 | + const validateFunc: typeof validate = validate; |
| 29 | + |
| 30 | + return (locator, parsed) => { |
| 31 | + const config = parsed.root; |
| 32 | + const currNode = locator.parent ? _.get(parsed, locator.parent) : config; |
| 33 | + |
| 34 | + let value: unknown; |
| 35 | + if (locator.cliOption !== undefined) { |
| 36 | + value = parseCli(locator.cliOption); |
| 37 | + } else if (locator.envVar !== undefined) { |
| 38 | + value = parseEnv(locator.envVar); |
| 39 | + } else if (locator.option !== undefined) { |
| 40 | + value = locator.option; |
| 41 | + } else if (defaultValue !== undefined) { |
| 42 | + value = _.isFunction(defaultValue) |
| 43 | + ? defaultValue(config, currNode) |
| 44 | + : defaultValue; |
| 45 | + } else { |
| 46 | + throw new MissingOptionError(locator.name); |
| 47 | + } |
| 48 | + |
| 49 | + validateFunc(value, config, currNode); |
| 50 | + |
| 51 | + return mapFunc(value, config, currNode); |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Object with fixed properties. |
| 57 | + * Any unknown property will be reported as error. |
| 58 | + */ |
| 59 | +export function section<T, R = any>(properties: SectionProperties<T, R>): SectionParser<T, R> { |
| 60 | + const expectedKeys = _.keys(properties) as Array<keyof T>; |
| 61 | + |
| 62 | + return (locator, config) => { |
| 63 | + const unknownKeys = _.difference( |
| 64 | + _.keys(locator.option), |
| 65 | + expectedKeys as Array<string> |
| 66 | + ); |
| 67 | + |
| 68 | + if (unknownKeys.length > 0) { |
| 69 | + throw new UnknownKeysError( |
| 70 | + unknownKeys.map((key) => `${locator.name}.${key}`) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + const lazyResult = buildLazyObject(expectedKeys, (key) => { |
| 75 | + const parser = properties[key]; |
| 76 | + |
| 77 | + return () => parser(locator.nested(key) as Locator<DeepPartial<T[keyof T]>>, config); |
| 78 | + }); |
| 79 | + |
| 80 | + _.set(config, locator.name, lazyResult); |
| 81 | + |
| 82 | + return lazyResult; |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Object with user-specified keys and values, |
| 88 | + * parsed by valueParser. |
| 89 | + */ |
| 90 | +export function map<T extends Record<string, any>, V extends T[string] = T[string], R = any>( |
| 91 | + valueParser: Parser<V, R>, |
| 92 | + defaultValue: DeepPartial<Record<string, V>> |
| 93 | +): MapParser<Record<string, V>, R> { |
| 94 | + return (locator, config) => { |
| 95 | + if (locator.option === undefined) { |
| 96 | + if (!defaultValue) { |
| 97 | + return {} as LazyObject<T>; |
| 98 | + } |
| 99 | + locator = locator.resetOption(defaultValue); |
| 100 | + } |
| 101 | + |
| 102 | + const optionsToParse = Object.keys(locator.option as Record<string, V>); |
| 103 | + const lazyResult = buildLazyObject<Record<string, V>>(optionsToParse, (key) => { |
| 104 | + return () => valueParser(locator.nested(key) as Locator<DeepPartial<T[keyof T]>>, config); |
| 105 | + }); |
| 106 | + _.set(config, locator.name, lazyResult); |
| 107 | + |
| 108 | + return lazyResult; |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +export function root<T>(rootParser: RootParser<T>, {envPrefix, cliPrefix}: RootPrefixes): ConfigParser<T> { |
| 113 | + return ({options, env, argv}) => { |
| 114 | + const rootLocator = initLocator({options, env, argv, envPrefix, cliPrefix}); |
| 115 | + const parsed = rootParser(rootLocator, {} as RootParsedConfig<T>); |
| 116 | + |
| 117 | + return forceParsing(parsed); |
| 118 | + }; |
| 119 | +} |
0 commit comments