Implement the whole thing
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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} from '@sinclair/typebox' // Re-export Type so users can describe config
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user