Env.ts 516 B

123456789101112131415161718192021222324
  1. import { config, DotenvParseOutput } from 'dotenv'
  2. import { injectable } from 'inversify'
  3. @injectable()
  4. export class Env {
  5. private env?: DotenvParseOutput
  6. public load(): void {
  7. const output = config()
  8. this.env = <DotenvParseOutput>output.parsed
  9. }
  10. public get(key: string, optional = false): string {
  11. if (!this.env) {
  12. this.load()
  13. }
  14. if (!process.env[key] && !optional) {
  15. throw new Error(`Environment variable ${key} not set`)
  16. }
  17. return <string>process.env[key]
  18. }
  19. }