import { CleanedEnvAccessors } from './types' ; export const strictProxyMiddleware = < T extends object > ( envObj , rawEnv ) => { const inspectables = [ 'length' , 'inspect' , 'hasOwnProperty' , 'toJSON' , Symbol . toStringTag , Symbol . iterator , 'asymmetricMatch' , 'nodeType' , '$$typeof' , 'then' , '__esModule' , ] ; const inspectSymbolStrings = [ 'Symbol(util.inspect.custom)' , 'Symbol(nodejs.util.inspect.custom)' ] ; return new Proxy ( envObj , { get ( target , name ) { if ( inspectables . includes ( name ) || inspectSymbolStrings . includes ( name . toString ( ) ) ) { return target [ name ] ; } const varExists = target . hasOwnProperty ( name ) ; if ( ! varExists ) { if ( typeof rawEnv === 'object' && rawEnv ?. hasOwnProperty ?. ( name ) ) { throw new ReferenceError ( `[envalid] Env var ${ name } was accessed but not validated. This var is set in the environment; please add an envalid validator for it.` ) ; } throw new ReferenceError ( `[envalid] Env var not found: ${ name }` ) ; } return target [ name as keyof T ] ; } , set ( _target , name ) { throw new TypeError ( `[envalid] Attempt to mutate environment value: ${ name }` ) ; } } ) ; } ; export const accessorMiddleware = < T > ( envObj , rawEnv ) => { const computedNodeEnv = envObj . NODE_ENV || rawEnv . NODE_ENV ; const isProd = ! computedNodeEnv || computedNodeEnv === 'production' ; Object . defineProperties ( envObj , { isDevelopment : { value : computedNodeEnv === 'development' } , isDev : { value : computedNodeEnv === 'development' } , isProduction : { value : isProd } , isProd : { value : isProd } , isTest : { value : computedNodeEnv === 'test' } } ) ; return envObj as T & CleanedEnvAccessors ; } ; export const applyDefaultMiddleware = < T > ( cleanedEnv , rawEnv ) => { return strictProxyMiddleware ( accessorMiddleware ( cleanedEnv , rawEnv ) , rawEnv ) ; } ;