All files / src Env.ts

0% Statements 0/24
0% Branches 0/3
0% Functions 0/5
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import { LogLevel } from "bunyan";
import { SecretString } from "lib";
import { toLogLevel } from "./Logger";
 
export class CommonEnv {
  readonly appName: string;
  readonly logLevel: LogLevel;
  readonly slackLogLevel: LogLevel;
  readonly slackWebhookUrl: string;
  readonly dbUrlForPrisma: string;
  readonly encryptionPassword: SecretString;
 
  constructor(env: NodeJS.ProcessEnv) {
    this.appName = getStringValue(env, "APP_NAME");
    this.logLevel = getLogLevel(env, "LOG_LEVEL");
    this.slackLogLevel = getLogLevel(env, "SLACK_LOG_LEVEL");
    this.slackWebhookUrl = getStringValue(env, "SLACK_WEBHOOK_URL");
    this.dbUrlForPrisma = getStringValue(env, "DB_URL_FOR_PRISMA");
    this.encryptionPassword = new SecretString(
      getStringValue(env, "ENCRYPTION_PASSWORD")
    );
  }
}
 
export class Env {
  readonly loginUrl: string;
  readonly timeoutMs: number;
  readonly screenshotDir: string;
 
  constructor(env: NodeJS.ProcessEnv, prefix: string) {
    this.loginUrl = getStringValue(env, prefix + "_LOGIN_URL");
    this.timeoutMs = getNumberValue(env, prefix + "_TIMEOUT_MS");
    this.screenshotDir = getStringValue(env, prefix + "_SCREENSHOT_DIR");
  }
}
 
function getStringValue(
  env: NodeJS.ProcessEnv,
  key: string,
  required: boolean = true
): string {
  const value = env[key];
  Iif (value === undefined) {
    Iif (required) {
      throw new Error(`environment[${key}] is not found`);
    }
    return "";
  }
  return value;
}
 
function getNumberValue(env: NodeJS.ProcessEnv, key: string): number {
  return Number(getStringValue(env, key));
}
 
function getLogLevel(env: NodeJS.ProcessEnv, key: string): LogLevel {
  const s = getStringValue(env, key);
  try {
    return toLogLevel(s);
  } catch (err) {
    throw new Error(`environment[${key}] is invalid value[${s}]`, {
      cause: err,
    });
  }
}