11import type { Type } from './schema'
2- import { A } from 'vitest/dist/chunks/environment.d.Dmw5ulng.js'
32import { assert } from '../assert'
4- import { fromCamelCase , toCamelCase } from '../data/camelcase'
3+ import { fromCamelCase } from '../data/camelcase'
54import { valueToBoolean , valueToBooleanNotFalse , valueToInteger } from '../data/convert'
6- import { objectFilter , objectMap } from '../data/object'
5+ import { objectMap } from '../data/object'
76import { isSchemaObjectFlat } from './utils'
87
98declare module './schema' {
@@ -13,10 +12,10 @@ declare module './schema' {
1312 }
1413}
1514
16- type EnvType = Record < string , any >
15+ export type SchemaEnvType = Record < string , any >
1716
18- interface SchemaEnvOptions {
19- env ?: EnvType
17+ export interface SchemaEnvOptions {
18+ env ?: SchemaEnvType
2019 prefix ?: string
2120 prefixOptional ?: boolean // if true, prefix is optional
2221}
@@ -25,9 +24,9 @@ export function parseSchemaEnv<T>(schema: Type<T>, opt?: SchemaEnvOptions): T {
2524 assert ( schema ?. _object , 'schema should be of type object' )
2625 assert ( isSchemaObjectFlat ( schema ) , 'schema should be a flat object' )
2726
28- let {
27+ const {
2928 // eslint-disable-next-line node/prefer-global/process
30- env = ( process ?. env ?? { } ) as EnvType ,
29+ env = ( process ?. env ?? { } ) as SchemaEnvType ,
3130 prefix = '' ,
3231 prefixOptional = false ,
3332 } = opt ?? { }
@@ -59,34 +58,6 @@ export function parseSchemaEnv<T>(schema: Type<T>, opt?: SchemaEnvOptions): T {
5958 }
6059 return schema . parse ( value )
6160 } ) as T
62-
63- if ( pl > 0 )
64- env = objectFilter ( env , key => key . startsWith ( prefix ) )
65- env = objectMap ( env , ( key , value ) => {
66- if ( pl > 0 )
67- key = key . substring ( pl )
68- return [ toCamelCase ( key ) , value ]
69- } )
70- const result = objectMap ( schema . _object ! , ( key , schema ) => {
71- let value = env [ toCamelCase ( key ) ]
72- if ( schema . type === 'number' ) {
73- value = valueToInteger ( value , schema . _default )
74- }
75- else if ( schema . type === 'boolean' ) {
76- if ( schema . _default === true )
77- value = valueToBooleanNotFalse ( value )
78- else
79- value = valueToBoolean ( value , false )
80- }
81- return schema . parse ( value )
82- } ) as T
83-
84- if ( prefixOptional !== true || pl <= 0 ) {
85- return result
86- }
87-
88- const resultWithoutPrefix = parseSchemaEnv ( schema , { ...opt , prefix : '' , prefixOptional : false } )
89- return { ...resultWithoutPrefix , ...result }
9061}
9162
9263export function stringFromSchemaEnv < T > ( schema : Type < T > , prefix = '' , commentOut = false , showPrivate = false ) : string {
0 commit comments