import { BindingAddress , ConfigurationResolver , Context , ContextBindings , DefaultConfigurationResolver , inject , ResolutionOptions , ValueOrPromise , } from '@loopback/context' ; /**
 * A custom configuration resolver that looks up env variables first
 */ class EnvConfigResolver extends DefaultConfigurationResolver implements ConfigurationResolver { constructor ( @ inject . context ( ) public readonly context ) { super ( context ) ; } param key private getFromEnvVars ( key ) { let val ; let found ; for ( const k in process . env ) { if ( k . toUpperCase ( ) === key . toUpperCase ( ) ) { val = process . env [ k ] ; found = k ; break ; } } if ( val == null ) return val ; console . log ( 'Loading configuration for binding "%s" from env variable "%s"' , key , found ) ; try { return JSON . parse ( val ) ; } catch ( err ) { return val ; } } getConfigAsValueOrPromise < ConfigValueType > ( key , configPath ? , resolutionOptions ? ) { const val = this . getFromEnvVars ( key . toString ( ) ) ; if ( val != null ) return val ; return super . getConfigAsValueOrPromise ( key , configPath , resolutionOptions ) ; } } export async function main ( ) { const ctx = new Context ( ) ; ctx . bind ( ContextBindings . CONFIGURATION_RESOLVER ) . toClass ( EnvConfigResolver ) ; ctx . configure ( 'foo' ) . to ( { bar : 'abc' } ) ; const fooConfig = await ctx . getConfig ( 'foo' ) ; console . log ( fooConfig ) ; ctx . configure ( 'bar' ) . to ( 'xyz' ) ; const barConfig = ctx . getConfigSync ( 'bar' ) ; console . log ( barConfig ) ; } if ( require . main === module ) { main ( ) . catch ( err => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; }