File size: 825 Bytes
f39e411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2228807
 
 
 
 
 
 
 
 
 
f39e411
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import dotenv from "dotenv";
dotenv.config();

export class EnvValue {
  constructor(public value: string | number | boolean) {}

  toString(): string {
    return String(this.value);
  }
  toNumber(): number {
    return Number(this.value);
  }
  toBoolean(): boolean {
    return this.value === "true";
  }
}

export class Env {
  static get(key: string, defaultValue?: string | number | boolean): EnvValue {
    const value = process.env[key] || defaultValue;

    if (!value) {
      throw new Error(`Environment variable ${key} not found`);
    }

    return new EnvValue(value);
  }

  static getOptional(key: string, defaultValue?: string | number | boolean): EnvValue {
    const value = process.env[key] || defaultValue;

    if (!value) {
      return new EnvValue("");
    }

    return new EnvValue(value);
  }
}