import { StringUtils } from '../utils' ; export class AppConfig { port : number ; logLevel : string ; serveStatic : boolean ; enableHttpRequestLogging : boolean ; constructor ( private environment ) { this . port = this . getIntegerEnvVar ( 'PORT' ) ; this . logLevel = this . getStringEnvVar ( 'LOG_LEVEL' ) ; this . serveStatic = this . getBooleanEnvVar ( 'SERVE_STATIC' ) ; this . enableHttpRequestLogging = this . getBooleanEnvVar ( 'ENABLE_HTTP_REQUEST_LOGGING' , false ) ; } getEnvironment ( ) { return this . environment ; } private getBooleanEnvVar ( variableName , defaultValue = null ) { const value = this . getEnvVar ( variableName , defaultValue ) ; const errorMessage = `Environment Variable ${ variableName } does not contain a valid boolean` ; return StringUtils . parseBoolean ( value , errorMessage ) ; } private getIntegerEnvVar ( variableName , defaultValue = null ) { const value = this . getEnvVar ( variableName , defaultValue ) ; const errorMessage = `Environment Variable ${ variableName } does not contain a valid integer` ; return StringUtils . parseInt ( value , errorMessage ) ; } private getStringEnvVar ( variableName , defaultValue = null ) { return this . getEnvVar ( variableName , defaultValue ) ; } private getEnvVar ( variableName , defaultValue ) { const value = this . environment [ variableName ] || defaultValue ; if ( value == null ) { throw new Error ( `Environment Variable ${ variableName } must be set!` ) ; } return value ; } }