29 lines
1010 B
TypeScript
29 lines
1010 B
TypeScript
import {type Static, type TObject, type TProperties} from '@sinclair/typebox'
|
|
import {Value} from '@sinclair/typebox/value'
|
|
import logger from 'log'
|
|
|
|
import {read_object} from './src/read_type'
|
|
|
|
const log = logger('config')
|
|
export {Type, type Static, type StaticDecode} from '@sinclair/typebox' // Re-export Type so users can describe config
|
|
export {Value} from '@sinclair/typebox/value'
|
|
|
|
export async function parse<T extends TProperties>(scheme: TObject<T>): Promise<Static<TObject<T>> | null> {
|
|
log.info("Parse configuration from env")
|
|
|
|
log.debug("Read configuration from env")
|
|
const config = await read_object(scheme)
|
|
if (!config.ok) return null
|
|
|
|
// This check is kind of a duplicate, is it useful ?
|
|
log.debug("Validate config against scheme")
|
|
const config_parsed = Value.Check(scheme, config.data)
|
|
if (config_parsed) {
|
|
log.trace("Config is valid")
|
|
return config.data
|
|
} else {
|
|
log.warn("Invalid config, check failed")
|
|
return null
|
|
}
|
|
}
|