Move parsing function to file
This commit is contained in:
+1
-26
@@ -1,27 +1,2 @@
|
||||
import logger from 'log'
|
||||
import * as yup from 'yup'
|
||||
|
||||
import * as parsing from './parsing'
|
||||
import type {Ok} from './helpers'
|
||||
|
||||
export * as yup from 'yup'
|
||||
|
||||
const log = logger('config')
|
||||
|
||||
export async function parse<S extends yup.Maybe<yup.AnyObject>>(schema: yup.ObjectSchema<S>): Promise<Ok<S>> {
|
||||
log.info('Parse from env')
|
||||
|
||||
log.trace('Start parsing')
|
||||
const config = await parsing.object(schema)
|
||||
if (!config.ok) return config
|
||||
|
||||
log.trace('double-check')
|
||||
const res = await schema.isValid(config.data, {strict: true})
|
||||
if (!res) {
|
||||
log.error('Double-check failed')
|
||||
log.debug('Config', config.data)
|
||||
return {ok: false}
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
export {parse} from './parsing'
|
||||
|
||||
@@ -6,6 +6,24 @@ import type {Ok} from './helpers'
|
||||
|
||||
const log = logger('config:parsing')
|
||||
|
||||
export async function parse<S extends yup.Maybe<yup.AnyObject>>(schema: yup.ObjectSchema<S>): Promise<Ok<S>> {
|
||||
log.info('Parse from env')
|
||||
|
||||
log.trace('Start parsing')
|
||||
const config = await object(schema)
|
||||
if (!config.ok) return config
|
||||
|
||||
log.trace('double-check')
|
||||
const res = await schema.isValid(config.data, {strict: true})
|
||||
if (!res) {
|
||||
log.error('Double-check failed')
|
||||
log.debug('Config', config.data)
|
||||
return {ok: false}
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
export async function object<S extends yup.Maybe<yup.AnyObject>>(
|
||||
schema: yup.ObjectSchema<S>,
|
||||
base_name: string = ''
|
||||
|
||||
Reference in New Issue
Block a user