All files / lib config.ts

81.81% Statements 9/11
75% Branches 6/8
100% Functions 5/5
80% Lines 8/10

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 491x     1x                     1x               1x 1x                     1x 1x                     5x    
import conf from "config";
import { MongoClientProps } from "./database/index.js";
import { RedisOptions } from "ioredis/built/redis/RedisOptions.js";
import { Duration } from "durr";
 
type DbConfig = MongoClientProps;
type CacheConfig = RedisOptions;
export interface Config {
  readonly databaseConfig: DbConfig;
  readonly cacheConfig: CacheConfig;
  readonly streamExpiration: Duration;
}
 
export function loadConfig(): Config {
  return {
    databaseConfig: loadDbConfig(),
    cacheConfig: loadCacheConfig(),
    streamExpiration: Duration.minutes(2),
  };
}
 
export function loadDbConfig(): DbConfig {
  if (conf.has("mongoDb")) {
    return {
      dbName: conf.get("mongoDb.dbName"),
      username: conf.get("mongoDb.username"),
      password: conf.get("mongoDb.password"),
      port: conf.get("mongoDb.port"),
      host: conf.get("mongoDb.host"),
    };
  } else Ethrow "Missing required database config!";
}
 
export function loadCacheConfig(): CacheConfig {
  if (conf.has("redis")) {
    return {
      port: optional("redis.port"),
      host: optional("redis.host"),
      username: optional("redis.username"),
      password: optional("redis.password"),
      db: optional("redis.db"),
    };
  } else Ethrow "Missing required cache config!";
}
 
function optional<T>(path: string): T | undefined {
  return conf.has(path) ? conf.get<T>(path) : undefined;
}