diff --git a/src/env.ts b/src/env.ts index 50d4ae5..6edc9e0 100644 --- a/src/env.ts +++ b/src/env.ts @@ -4,35 +4,35 @@ import fs from 'node:fs/promises' import type {Ok} from './helpers' const log = logger('config:env') -const FILE_EXT = "__FILE" +const FILE_EXT = '__FILE' // Read from env or file export async function read_env(key: string): Promise> { - const path = process.env[key + FILE_EXT] - if (path !== undefined) return from_file(path) - else return {ok: true, data: process.env[key]} + const path = process.env[key + FILE_EXT] + if (path !== undefined) return from_file(path) + else return {ok: true, data: process.env[key]} } async function from_file(path: string): Promise> { - log.debug("Read a key from a file") - log.trace("Path :", path) + log.debug('Read a key from a file') + log.trace('Path :', path) - let content = await get_file_content(path) - if (!content.ok) return content + let content = await get_file_content(path) + if (!content.ok) return content - content.data = content.data.trim() + content.data = content.data.trim() - return content + return content } -async function get_file_content(path: string) : Promise> { - log.debug('Read file content') - log.trace('Path :', path) +async function get_file_content(path: string): Promise> { + log.debug('Read file content') + log.trace('Path :', path) - try { - const data = await fs.readFile(path, {encoding: 'utf-8'}) - return {ok: true, data} - } catch (e) { - log.warn('Failed to read file', path) - log.debug('Error :', e) - return {ok: false} - } + try { + const data = await fs.readFile(path, {encoding: 'utf-8'}) + return {ok: true, data} + } catch (e) { + log.warn('Failed to read file', path) + log.debug('Error :', e) + return {ok: false} + } } diff --git a/src/helpers.ts b/src/helpers.ts index 088127f..3404057 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1 +1 @@ -export type Ok = {ok: true, data: T} | {ok?: false} +export type Ok = {ok: true; data: T} | {ok?: false} diff --git a/src/index.ts b/src/index.ts index 7bedb1c..f4257b7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,19 +9,19 @@ export * as yup from 'yup' const log = logger('config') export async function parse>(schema: yup.ObjectSchema): Promise> { - log.info("Parse from env") + log.info('Parse from env') - log.trace("Start parsing") - const config = await parsing.object(schema) - if (!config.ok) return config + 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} - } + 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 + return config } diff --git a/src/parsing.ts b/src/parsing.ts index 50a49d3..7ddb57f 100644 --- a/src/parsing.ts +++ b/src/parsing.ts @@ -6,57 +6,60 @@ import type {Ok} from './helpers' const log = logger('config:parsing') -export async function object>(schema: yup.ObjectSchema, base_name: string = ""): Promise> { - log.debug("Object") - if (base_name.length !== 0) base_name = base_name + "_" +export async function object>( + schema: yup.ObjectSchema, + base_name: string = '' +): Promise> { + log.debug('Object') + if (base_name.length !== 0) base_name = base_name + '_' - // @ts-expect-error Ugly hack with type S - const data: S = {} - - for (const key in schema.fields) { - const sub_key = base_name + key.toUpperCase() + // @ts-expect-error Ugly hack with type S + const data: S = {} - const sub_scheme = schema.fields[key] - let value: Ok + for (const key in schema.fields) { + const sub_key = base_name + key.toUpperCase() - // TODO : If array, add a transform - if (sub_scheme.describe().type === 'object') { - value = await object(sub_scheme, sub_key) - } else { - value = await generic(sub_scheme, sub_key) - } + const sub_scheme = schema.fields[key] + let value: Ok - if (!value.ok) return value - if (value.data === undefined) continue - - data[key] = value.data + // TODO : If array, add a transform + if (sub_scheme.describe().type === 'object') { + value = await object(sub_scheme, sub_key) + } else { + value = await generic(sub_scheme, sub_key) } - return {ok: true, data} + if (!value.ok) return value + if (value.data === undefined) continue + + data[key] = value.data + } + + return {ok: true, data} } export async function generic(scheme: yup.AnySchema, key: string): Promise> { - log.debug('Generic', scheme.describe().type) + log.debug('Generic', scheme.describe().type) - const value = await read_env(key) - if (!value.ok) return value + const value = await read_env(key) + if (!value.ok) return value - if (value.data === undefined) { - const def = scheme.getDefault() - if (def !== undefined) return {ok: true, data: def} - else if (scheme.spec.optional) return {ok: true, data: undefined} - - log.warn('Missing value for', key) - return {} - } + if (value.data === undefined) { + const def = scheme.getDefault() + if (def !== undefined) return {ok: true, data: def} + else if (scheme.spec.optional) return {ok: true, data: undefined} - if (!(await scheme.isValid(value.data))) { - log.warn('Invaid value for', key) - log.debug('Value:', value.data) - return {} - } - - const res = scheme.cast(value.data) + log.warn('Missing value for', key) + return {} + } - return {ok: true, data: res} + if (!(await scheme.isValid(value.data))) { + log.warn('Invaid value for', key) + log.debug('Value:', value.data) + return {} + } + + const res = scheme.cast(value.data) + + return {ok: true, data: res} }